Results 1 to 8 of 8

Thread: How do I tell if an array is "empty"

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. 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

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  4. #4
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  5. #5
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    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...

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    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.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do I tell if an array is "empty"

    Quote Originally Posted by Ben321 View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width