|
-
Jul 11th, 2011, 03:28 PM
#1
Thread Starter
Frenzied Member
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
-
Jul 11th, 2011, 04:42 PM
#2
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"
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 11th, 2011, 04:48 PM
#3
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.
-
Jul 11th, 2011, 06:26 PM
#4
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
-
Jul 11th, 2011, 06:28 PM
#5
PowerPoster
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
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jul 11th, 2011, 09:17 PM
#6
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
-
Jul 16th, 2011, 12:28 AM
#7
Thread Starter
Frenzied Member
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.
-
Jul 16th, 2011, 11:15 AM
#8
Re: How do I tell if an array is "empty"
 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
Last edited by LaVolpe; Jul 16th, 2011 at 12:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|