How do I tell if an array is "empty"
I mean if I do Dim Array() As String but I don't follow that with Redim Array(somevalue) then it is empty, but if the Redim occurs (or not) automatically at some point based on external input, how do I check if the Array is empty? If it is empty, then Ubound will make an error (and error handling can be tricky), so I prefer to avoid the error to start with. Is there something I can do similar to the pseudocode below?
Code:
If IsNotEmpty(Array()) Then u=UBound(Array()) Else RunThisCodeInstead
Re: How do I tell if an array is "empty"
if you search, in this forum, you should find many examples and explanation as to why this works
vb Code:
If Not Not myarr Then MsgBox UBound(myarr) Else MsgBox "array not initialised"
Re: How do I tell if an array is "empty"
It is worth doing that search that westconn mentions there is a bizarre caveat with the above when it is performed in the IDE.
Re: How do I tell if an array is "empty"
Ben
I don't fully understand your issue, but instead of ...
Code:
Dim MyArray() As String
.. would it help if you do this ..
Code:
Dim MyArray(1) As String
The latter will of course create MyArray(0) and MyArray (1),
but at least you could quickly check if they are empty or not.
You could still ReDim at a later point in time.
Spoo
Re: How do I tell if an array is "empty"
Code:
If Item1 = 0 Then
End
Else
If Item1 <> 0 Then
MsgBox .....
End If
End If
Re: How do I tell if an array is "empty"
Once upon the time, in other threads, I really love the simplicity of "Not Not myarr" but it really a "bizarre caveat".
Other people use CopyMemory() and other API functions.
My new universal function:
Code:
Function IsArrayInitialized(arr As Variant) As Boolean
If Not IsArray(arr) Then Err.Raise 13
On Error Resume Next
IsArrayInitialized = (LBound(arr) <= UBound(arr))
End Function
Code:
Sub TestIt()
Dim ar1() As String
Dim ar2(3) As Long
Dim aVar As Variant
Debug.Print "Case 1: "; IsArrayInitialized(ar1) '-- False
Debug.Print "Case 2: "; IsArrayInitialized(ar2) '-- True
ReDim ar1(5)
Debug.Print "Case 3: "; IsArrayInitialized(ar1) '-- True
Erase ar1
Debug.Print "Case 4: "; IsArrayInitialized(ar1) '-- False
Erase ar2
Debug.Print "Case 5: "; IsArrayInitialized(ar2) '-- True
ar1 = Split("") '-- LBound(ar) = 0, UBound(ar) = -1
'-- should be treated as not initialized because ar(0) will raise "subscript out of range"
'-- Err#13 with (Not Not ar1)
Debug.Print "Case 6: "; IsArrayInitialized(ar1) '-- False
aVar = Array(1, 2, 3) '-- Err#13 with (Not Not aVar)
Debug.Print "Case 7: "; IsArrayInitialized(aVar) '-- True
aVar = Empty
Debug.Print "Case 8: "; IsArrayInitialized(aVar) '-- Err#13 raised: not an array
End Sub
Re: How do I tell if an array is "empty"
would cbool() work instead of not not? It seems the purpose of not not is to convert to a boolean value.
Re: How do I tell if an array is "empty"
Quote:
Originally Posted by
Ben321
would cbool() work instead of not not? It seems the purpose of not not is to convert to a boolean value.
No, CBool(myArray()) will error regardless if it is filled or not.
By using Not myArray(), VB for whatever reasons, passes the SafeArray pointer (and NOT XORs it with -1). If myArray() is not initialized, you will get -1 (0 pointer value XOR -1) and using the 2nd NOT, turns -1 to 0 (-1 Xor -1 = 0) which we translate as False. If myArray() is filled we get a valid pointer XOR'd with -1. So, I believe these two should return the same values:
Not Not myArray()
Not myArray() XOR -1&
FYI: Not Not 1234 is same value as (1234 Xor -1) Xor -1. Not 1234 same as 1234 Xor -1
Using Not Not SomeValue, the 2 Nots cancel each other out and SomeValue is the result
We use Not Not myArray() to get the SafeArray pointer. There are other ways of getting this pointer using APIs. And bottom line, if the pointer is zero, then the array is not initialized.
Two final caveats.
1. Not Not method works for String arrays. No APIs, I know of, work for these without use of a Type Library (TLB)
2. It is possible for an array to be initialized incorrectly, on purpose,
so it looks like this: myArray(0 to -1).
VB does this to indicate an invalid array: myByteArray() = StrConv(vbNullString, vbFromUnicode)
Though the array is initialized, attempting to access its elements will error. Anhn's post #6, checks for this special case