Results 1 to 11 of 11

Thread: UBound and Un Dimensioned Array

  1. #1

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Resolved 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...
    Last edited by Danial; Jan 5th, 2005 at 06:12 AM.
    [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 :

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: UBound and Un Dimensioned Array

    I originally saw something like this posted by MartinLiss
    It actually works !!!
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim B() As Byte
    5.    
    6.     Debug.Print "Just dimmed: " & IsDimensioned(B)
    7.    
    8.     ReDim B(10)
    9.    
    10.     Debug.Print "After Redim: " & IsDimensioned(B)
    11.    
    12.     Erase B
    13.    
    14.     Debug.Print "After Erase: " & IsDimensioned(B)
    15. End Sub
    16.  
    17. Public Function IsDimensioned(vArray() As Byte) As Boolean
    18.     IsDimensioned = Not (Not (vArray))
    19. End Function

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: UBound and Un Dimensioned Array

    There's also another way of doing it.

    VB Code:
    1. Dim MyArray() As String
    2.    
    3.     On Error Resume Next
    4.     MsgBox UBound(MyArray)
    5.     If Err.Number = 9 Then
    6.         Err.Clear
    7.         MsgBox "Array not dimensioned"
    8.     End If
    9.     On Error GoTo MyNormalErrorRoutine

  6. #6

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    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
    [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 :

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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:
    1. On Error Resume Next
    2.         MsgBox UBound(MyArray)
    3.         If Err.Number = 9 Then
    4.             Err.Clear
    5.         End If

    takes almost twice as long as this.

    VB Code:
    1. IsDimensioned B
    2.  
    3. Public Function IsDimensioned(vArray() As String) As Boolean
    4.     IsDimensioned = Not (Not (vArray))
    5. End Function



    takes almost twice as long as the other approach.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  11. #11

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    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
  •  



Click Here to Expand Forum to Full Width