UBound and Un Dimensioned Array
Is there a way if i can check if an Dynamic Array has been dimensioned?
Say i have :
Dim myArray() as String
Now if i do :
ubound(myArray), i will get an error as the array has yet not bin dimensioned. I dont want to use "On Error Go To .." to catch the error. Is there a builtin function Similar to IsArray()?
Thanks in advance...
Re: UBound and Un Dimensioned Array
There isn't any built in functions, so your best bet is to check for error.
Another way could be to redim your array initially Redim MyArray(0) and assign some dummy value to that only element. Then all you need to is check if UBound is still = 0 and if yes - check for that dummy value so you'd know what to do.
Re: UBound and Un Dimensioned Array
I originally saw something like this posted by MartinLiss
It actually works !!!
VB Code:
Option Explicit
Private Sub Form_Load()
Dim B() As Byte
Debug.Print "Just dimmed: " & IsDimensioned(B)
ReDim B(10)
Debug.Print "After Redim: " & IsDimensioned(B)
Erase B
Debug.Print "After Erase: " & IsDimensioned(B)
End Sub
Public Function IsDimensioned(vArray() As Byte) As Boolean
IsDimensioned = Not (Not (vArray))
End Function
Re: UBound and Un Dimensioned Array
I just noticed in your original post the array is "As String", so just change my code from "As Byte" to "As String" and it should work the same
Re: UBound and Un Dimensioned Array
There's also another way of doing it.
VB Code:
Dim MyArray() As String
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number = 9 Then
Err.Clear
MsgBox "Array not dimensioned"
End If
On Error GoTo MyNormalErrorRoutine
Re: UBound and Un Dimensioned Array
Quote:
Originally Posted by CVMichael
I just noticed in your original post the array is "As String", so just change my code from "As Byte" to "As String" and it should work the same
That looks interesting although it would require me writing separate function for each data type.
Martin thanks for your code, i was trying to avoid catching error after it has occured since my code will be in executed every few milisecond and that might effect performance. I will test both and see which one is faster.
Thanks all for the replies.
Danial
Re: UBound and Un Dimensioned Array
Quote:
Originally Posted by Danial
That looks interesting although it would require me writing separate function for each data type.
Martin thanks for your code, i was trying to avoid catching error after it has occured since my code will be in executed every few milisecond and that might effect performance. I will test both and see which one is faster.
Thanks all for the replies.
Danial
I tested it and this
VB Code:
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number = 9 Then
Err.Clear
End If
takes almost twice as long as this.
VB Code:
IsDimensioned B
Public Function IsDimensioned(vArray() As String) As Boolean
IsDimensioned = Not (Not (vArray))
End Function
takes almost twice as long as the other approach.
Re: UBound and Un Dimensioned Array
That's because you have a MsgBox in there, Martin.
Change it to intUbound = UBound(MyArray) and it should be faster.
Re: UBound and Un Dimensioned Array
Quote:
Originally Posted by RhinoBull
That's because you have a MsgBox in there, Martin.
Change it to intUbound = UBound(MyArray) and it should be faster.
You're right. I changed it to x = UBound(MyArray) where x is a Long and it was faster, but it was still almost 50% slower than the Function approach.
Re: UBound and Un Dimensioned Array
Actually I just did very similar test and using Error handler is slower and it's noticable without even using GetTickCount (also I did it in the IDE):
both methods were looping 1,000,000 times
1. error handler
1/5/2005 11:35:04 AM
1/5/2005 11:35:08 AM
2. your function (no time at all :thumb: ) - will keep it handy for myself.
1/5/2005 11:35:15 AM
1/5/2005 11:35:15 AM
Re: UBound and Un Dimensioned Array
Thanks Martin & Rhino for testing. I think I will go with the function approach.
Thanks to all again.