Results 1 to 5 of 5

Thread: Empty array check question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    8
    If I have dimensioned an array:

    Dim strArray() as String

    But not yet redimed or assigned anything to the array how can I check to see if the array is empty before, for example, examining it in a FOR/NEXT loop.

    IsEmpty(strArray) returns False.
    IsNull(strArray) returns False.
    Ubound(strArray) or Lbound(strArray) causes an error.

    Any ideas?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    This will do it
    Code:
        Dim bCreated as Boolean
    
        On Error Resume Next
        'See if the array has been created...
        MyArray(0) = MyArray(0)
        bCreated = (Err = 0)
        If Not bCreated Then
            '...and if it hasn't then dimension it
            Err.Clear
            Redim MyArray(5)
        End If

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    8
    Thanks for your reply but I thought of this type of thing already.

    I'm trying to avoid writing On Error Resume Next type code, because that is not really good error handling.

    Any other ideas?

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    The sample I posted really is the best way to do it. Don't worry about error checking. Just do this:
    Code:
        Dim bCreated as Boolean
    
        On Error Resume Next
        'See if the array has been created...
        MyArray(0) = MyArray(0)
        bCreated = (Err = 0)
        If Not bCreated Then
            '...and if it hasn't then dimension it
            Err.Clear
            Redim MyArray(5)
        End If
    
        ' This turns off error checking and so the rest of
        ' the sub will behave just like it always did
        'On Error Goto 0
    
        ' The rest of your code
    
        End Sub
    [Edited by MartinLiss on 04-13-2000 at 07:46 AM]

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Cool

    Try this one:
    Code:
    Private Sub Form_Load()
        Dim a()
        MsgBox isIDA(a)
        ReDim a(1)
        MsgBox isIDA(a)
    End Sub
    
    Function isIDA(anarr)
        isIDA = True
        On Error Resume Next
        a = LBound(anarr)
        If Err Then isIDA = False
    End Function
    isIDA=isInitialized dynamic array

    [Edited by kedaman on 04-13-2000 at 12:15 PM]
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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