|
-
Feb 20th, 2002, 05:23 AM
#1
Thread Starter
Lively Member
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:
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
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?
-
Feb 20th, 2002, 05:45 AM
#2
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.
.
-
Feb 20th, 2002, 05:58 AM
#3
Hyperactive Member
Try using something like this.
If it returns 0 the array is empty.
VB Code:
Public Function getArraySize(inArray())
getArraySize = 0
On Error Resume Next
getArraySize = UBound(inArray) + 1
End Function
-
Feb 20th, 2002, 05:58 AM
#4
Thread Starter
Lively Member
Oh well.. better go with the error-trap then 
Thanks anyway
-
Feb 20th, 2002, 06:00 AM
#5
Thread Starter
Lively Member
Oh.. didn't see that last reply.
Since it solves my problem it is more than good enough. Thanks.
-
Feb 20th, 2002, 06:02 AM
#6
Hyperactive Member
This is better, make one for each data type you want size arrays in.
VB Code:
Public Function getIntegerArraySize(inArray() As Integer) As Integer
getArraySize = 0
On Error Resume Next
getArraySize = UBound(inArray) + 1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|