Click to See Complete Forum and Search --> : Arrays


Doga Arinir
February 15th, 2000, 12:40 PM
Is there a possiblity to check wether a dynamic array is initialized or not. (the empty function does not work)

Example:

option explicit
public h() as integer


private sub tes

redim h(1 to 10)
if h "is initialized" then
msgbox "Hello"

erase h
if h "is initialized" then
msgbox "Nope" 'this mustn't occur!!!




end sub

Cakkie
February 15th, 2000, 12:53 PM
What exactly do you want to aceive?

Do you want that your array is empty after the redim, or do you want the array to hold it's previous values?

If you use redim, all the values are erased, and the array will be empty, also, whenever you declare an array (using dim or redim) the array is 'initialized', that is, the array will be empty (unless specified else) and you will be able to address an element of the array (unlike in java).

Hope this makes it a bit more clear to you.

Tom Cannaerts
slisse@planetinternet.be

The best way to escape a problem, is to solve it.

Doga Arinir
February 15th, 2000, 04:02 PM
Back to our example:

Let us think that the dynamic array f can vary like that.
.
.
redim f(1 to 10)
call test()
.
.
erase f
'any attempt like f(1) = 234 ends up in an error message
'so i want my my sub routine to check wether f
'is "dimensioned/initialized" or erased!
call test



option explicit
private f() as integer


private sub test()


end sub

Johnny101
February 15th, 2000, 04:49 PM
this is closest i can do to what you're looking for:


option Explicit
Dim s() as string

private Sub Form_DblClick()
Erase s
call tester
End Sub

private Sub Form_Load()
ReDim s(1 to 10)
Call tester
End Sub

Sub tester()
on error GoTo handler
If Not UBound(s) then
MsgBox "valid array"
else
MsgBox "invalid array"
End If

Exit Sub

handler:
If Err.Number = 9 then 'subscript out of range
MsgBox "the array is empty/non-initialized."
else
MsgBox error
End If

End Sub




You can easily tell if the array is just empty by checking the ubound property (0 means there are no elements in the array).

Hope this helps,
John


John Pirkey
MCSD
www.ShallowWaterSystems.com