Results 1 to 6 of 6

Thread: Handling empty arrays

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    74

    Handling empty arrays

    How can I handle a dynamic array that contains no records at all.
    (i.e has not been redimensioned)

    That is, an array defined as

    VB Code:
    1. Dim str_Array() as String

    is supposed to get populated with data from a database. Sometimes there are no data.. and thus.. no redimensioning of the array will take place.

    The problem is that

    VB Code:
    1. Ubound(str_Array)

    returns a subscript-out-of-range-message. I could rewrite the code so that this situation does not occur, but that means more work for me.

    Suggestions?

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    There are two ways to do this. Maintain a counter that keeps track of how many elements have been added to the array. It will start with zero, and will be incremented or decremented with each addition to or removal from the array.

    Or modify your code to trap the error 'Subscript Out Of Range' as it will indicate that the array is empty.

    There was quite a fierce discussion on this topic a few weeks ago. You can search for that thread, you will find some interesting info.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Try using something like this.
    If it returns 0 the array is empty.
    VB Code:
    1. Public Function getArraySize(inArray())
    2.    
    3.     getArraySize = 0
    4.     On Error Resume Next
    5.     getArraySize = UBound(inArray) + 1
    6.    
    7. End Function

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    74
    Oh well.. better go with the error-trap then
    Thanks anyway

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    74
    Oh.. didn't see that last reply.

    Since it solves my problem it is more than good enough. Thanks.

  6. #6
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    This is better, make one for each data type you want size arrays in.
    VB Code:
    1. Public Function getIntegerArraySize(inArray() As Integer) As Integer
    2.    
    3.     getArraySize = 0
    4.     On Error Resume Next
    5.     getArraySize = UBound(inArray) + 1
    6.    
    7. End Function

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