Discussion:
how to determine if array its empty
(too old to reply)
PICCORO McKAY Lenz
2017-07-01 09:33:03 UTC
Permalink
i have

Dim ar As New Variant[]

so how can i determine if the array its empty, i mean does not added any
element.. due that piece of code fails:

If value.dim > 0 Then

If value.count > 0 Then

with a index out of bound exception

Lenz McKAY Gerardo (PICCORO)
http://qgqlochekone.blogspot.com
Gianluigi
2017-07-01 10:09:16 UTC
Permalink
Dim myArray As String[]

If IsNull(myArray) Then Print "Empty"

Regards
Gianluigi
Post by PICCORO McKAY Lenz
i have
Dim ar As New Variant[]
so how can i determine if the array its empty, i mean does not added any
If value.dim > 0 Then
If value.count > 0 Then
with a index out of bound exception
Lenz McKAY Gerardo (PICCORO)
http://qgqlochekone.blogspot.com
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Gianluigi
2017-07-01 10:13:15 UTC
Permalink
or
If IsNull(myArray) Or If myArray.Count = 0 Then Print "Empty"

Regards
Gianluigi
Post by Gianluigi
Dim myArray As String[]
If IsNull(myArray) Then Print "Empty"
Regards
Gianluigi
Post by PICCORO McKAY Lenz
i have
Dim ar As New Variant[]
so how can i determine if the array its empty, i mean does not added any
If value.dim > 0 Then
If value.count > 0 Then
with a index out of bound exception
Lenz McKAY Gerardo (PICCORO)
http://qgqlochekone.blogspot.com
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Cristiano Guadagnino
2017-07-01 12:08:25 UTC
Permalink
Hi Gianluigi!
Post by Gianluigi
or
If IsNull(myArray) Or If myArray.Count = 0 Then Print "Empty"
I have not tried, but I don't think this will work. In an "or" expression
you have to evaluate both members, so if myArray is null the "myArray.Count
= 0" part will generate an out of bounds error.

Cris
Gianluigi
2017-07-01 12:46:47 UTC
Permalink
Hi Cristiano,

Or If is only evaluated if *not* IsNull.

Regards
Gianluigi
Post by Cristiano Guadagnino
Hi Gianluigi!
Post by Gianluigi
or
If IsNull(myArray) Or If myArray.Count = 0 Then Print "Empty"
I have not tried, but I don't think this will work. In an "or" expression
you have to evaluate both members, so if myArray is null the "myArray.Count
= 0" part will generate an out of bounds error.
Cris
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Hans Lehmann
2017-07-01 13:08:33 UTC
Permalink
Hallo,

my idea:

Dim myArray As New String[]

' Existiert das Array und ist die Anzahl der Elemente gleich Null, dann
ist das Array leer
If Not IsNull(myArray) And If myArray.Count = 0 Then Print "Array is empty"

Hans
PICCORO McKAY Lenz
2017-07-01 13:05:10 UTC
Permalink
Post by Cristiano Guadagnino
Hi Gianluigi!
Post by Gianluigi
or
If IsNull(myArray) Or If myArray.Count = 0 Then Print "Empty"
I have not tried, but I don't think this will work. In an "or" expression
you have to evaluate both members, so if myArray is null the "myArray.Count
= 0" part will generate an out of bounds error.
ahhh! that's the trick! thanks!
Post by Cristiano Guadagnino
Cris
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Jussi Lahtinen
2017-07-01 13:28:44 UTC
Permalink
If you add the extra "if", then Gambas will do short-circuit evaluation.
https://en.wikipedia.org/wiki/Short-circuit_evaluation


Jussi
Post by Cristiano Guadagnino
Hi Gianluigi!
Post by Gianluigi
or
If IsNull(myArray) Or If myArray.Count = 0 Then Print "Empty"
I have not tried, but I don't think this will work. In an "or" expression
you have to evaluate both members, so if myArray is null the "myArray.Count
= 0" part will generate an out of bounds error.
Cris
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Hans Lehmann
2017-07-01 13:36:21 UTC
Permalink
Correct?

If (Not IsNull(myArray) And myArray.Count = 0) Then Print "Array is empty"

Hans
Gianluigi
2017-07-01 13:48:06 UTC
Permalink
Hi Hans,
If the array is not instantiated or does not work (first example) or does
error.

Regards
Gianluigi
Post by Hans Lehmann
Correct?
If (Not IsNull(myArray) And myArray.Count = 0) Then Print "Array is empty"
Hans
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Hans Lehmann
2017-07-01 14:15:24 UTC
Permalink
Case 1:
Dim myArray As String[] --> Array not exist.

Case 2:
Dim myArray As New String[] --> Array ist empty.

Case 3:
Dim myArray As New String[] --> Array is not empty!
myArray.Add("Value")

If Not IsNull(myArray) Then
If myArray.Count = 0 Then
Print "Array ist empty."
Else
Print "Array is not empty!"
Endif
Else
Print "Array not exist."
Endif

Hans
Gianluigi
2017-07-01 15:15:10 UTC
Permalink
Ok, very precise,
depends on what you need.
I think at work, if you have to read the array, an:

If IsNull (myArray) Or If myArray.Count = 0 Then Return

It is more practice

Gianluigi
Post by Hans Lehmann
Dim myArray As String[] --> Array not exist.
Dim myArray As New String[] --> Array ist empty.
Dim myArray As New String[] --> Array is not empty!
myArray.Add("Value")
If Not IsNull(myArray) Then
If myArray.Count = 0 Then
Print "Array ist empty."
Else
Print "Array is not empty!"
Endif
Else
Print "Array not exist."
Endif
Hans
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Cristiano Guadagnino
2017-07-01 17:46:31 UTC
Permalink
Thank you Jussi, that's a nice trick I didn't know of!
Actually, I didn't even notice there was an extra "if" in Gianluigi's
example.

Cris
Post by Jussi Lahtinen
If you add the extra "if", then Gambas will do short-circuit evaluation.
https://en.wikipedia.org/wiki/Short-circuit_evaluation
Jussi
Post by Cristiano Guadagnino
Hi Gianluigi!
Post by Gianluigi
or
If IsNull(myArray) Or If myArray.Count = 0 Then Print "Empty"
I have not tried, but I don't think this will work. In an "or" expression
you have to evaluate both members, so if myArray is null the
"myArray.Count
Post by Cristiano Guadagnino
= 0" part will generate an out of bounds error.
Cris
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Continue reading on narkive:
Loading...