-
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?
-
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
-
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?
-
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]
-
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]