|
-
Jan 4th, 2005, 09:51 PM
#1
-
Jan 4th, 2005, 10:26 PM
#2
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.
-
Jan 4th, 2005, 11:45 PM
#3
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
-
Jan 4th, 2005, 11:47 PM
#4
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
-
Jan 5th, 2005, 01:24 AM
#5
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
-
Jan 5th, 2005, 06:12 AM
#6
Re: UBound and Un Dimensioned Array
 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
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Jan 5th, 2005, 11:07 AM
#7
Re: UBound and Un Dimensioned Array
 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.
-
Jan 5th, 2005, 11:13 AM
#8
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.
-
Jan 5th, 2005, 11:33 AM
#9
Re: UBound and Un Dimensioned Array
 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.
-
Jan 5th, 2005, 11:38 AM
#10
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 ) - will keep it handy for myself.
1/5/2005 11:35:15 AM
1/5/2005 11:35:15 AM
-
Jan 5th, 2005, 07:28 PM
#11
Re: UBound and Un Dimensioned Array
Thanks Martin & Rhino for testing. I think I will go with the function approach.
Thanks to all again.
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
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
|