Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Safest way to determine UpperBound of Dynamic Array?

Status
Not open for further replies.

rubbernilly

Programmer
Sep 20, 2005
447
US
In the case of a dynamic array, if it never gets filled with data, then the statements...

Code:
Dim MyArray() as String, i as integer
i = UBound(MyArray)

...will produce an error.

How can I safely test for the ubound? I've thought about doing the following:

Code:
Dim MyArray() as String
Dim MyTest() as string

'code that could potentially fill MyArray with data
MyArray = Split(sAddressString, ";")

MyTest = MyArray

Redim preserve MyTest(0)
If MyTest(0) = "" or IsNull(MyTest(0) Then
   'nothing was in MyArray
End If

But that is a bit cumbersome.

Then there is the option of changing the error handling just before a variable gets assigned to the upper bound, but that can't be the best way...

Code:
Dim MyArray() as string, i as Integer

On Error Resume Next
i = UBound(MyArray)

If i = 0 then
   'myarray has no data
End if

Is there a better way to handle this?
 
...or what about ...

if len(join(myarray,",")) >0 then
for a = 0 to ubound(myarray)
'code
next a
end if

??
 
Check out this thread222-836312, I found JohnYingling's interesting, though brute force/on error resume next is perhaps more commonly used? Some other suggestions also in the thread.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top