Results 1 to 9 of 9

Thread: Array Dimensions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    20

    Arrow

    I'm used to using a basic script which allows me to use a function called ArrayDims() to determine the dimensions of an array. This is very helpful for routines that operate on dynamic arrays, because if i know that the array has been dimensioned i can begin stepping through it to do whatever needs to be done. If it hasn't been dimensioned, then i can skip that step. Is there any way in standard VB to check this? I keep having my code crash because the UBound returns an error if the array has not been dimensioned.

    Specifically, this is what i'm talking about. I load values into an array from a database, redimensioning the array as needed. If the table i'm reading from has no records, however, the array stays undimensioned. Next i perform some sort of operation on the items i just read in by stepping through the array (for i = 0 to UBound). In the VB script i've been using, i would simply check arraydims first, and if the array had values i would perform the operation. In VB i can't use that command though, so the code crashes!

    Any help would be appreciated.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You could use this fake Ubound method, when your for next loops from 0 to -1 it skips it.
    Code:
    Property Get UUbound(Uarray)
        UUbound = -1
        On Error Resume Next
        UUbound = UBound(Uarray)
    End Property
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Unfortunately, VB does not have a command like that, so you have to do something like what Kedaman is suggesting. Here's my two cents worth of code as well:
    Code:
    ' A function called "IsDimensioned":
    
    Private Function IsDimensioned(TheArray() As Variant) As Boolean
    
        Dim intUB            As Integer
    
        On Error GoTo NotDimensioned
    
        intUB = UBound(TheArray)
    
        IsDimensioned = True
        Exit Function
    
    NotDimensioned:
        IsDimensioned = False
    
    Exit Function
    To use the function, you could have something like:
    Code:
    If IsDimensioned(MyArray) Then ...





    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    heres another one:

    Code:
    Function IsArrayEmpty(varArray As Variant) As Boolean
        ' Determines whether an array contains any elements.
        ' Returns False if it does contain elements, True
        ' if it does not.
    
        Dim lngUBound As Long
        
        On Error Resume Next
        ' If the array is empty, an error occurs when you
        ' check the array's bounds.
        lngUBound = UBound(varArray)
        If Err.Number <> 0 Then
            IsArrayEmpty = True
        Else
            IsArrayEmpty = False
        End If
    End Function

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    20

    Array Dimensions

    I did in fact end up just using an On Error statement. However, the functions that were recommended didn't seem to work, due to the fact that i'm using various arrays of user defined types. I kept getting the "only objects defined in a public object module may be converted to variants" or something like that. No big deal though, i solved the problem... Thanks...

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I suppose if MS gave VB templates and template functions this sort of thing would be a lot easier.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    I think the easiest way to find out if an array is dimensioned is to do something like this:
    Code:
    Public Function IsArrayDimensioned(pArray() As Variant) As Boolean
        On Error Resume Next
    
        IsArrayDimensioned = IsNumeric(UBound(pArray))
    End Function

  8. #8
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610
    Is there a way to pass an array of an user defined type to one of the above functions? It seems like varient will not except this.

    Thanks
    This space for rent...

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's a real problem, you can't pass a type array as a variant, the only way is to pass the array as an array of that UDT, and that means you have to make functions for each array. This isn't a good idea so you could as well just use inline errorhandling at the point you want to check it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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