-
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.
-
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
-
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 ...
-
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
-
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...
-
I suppose if MS gave VB templates and template functions this sort of thing would be a lot easier.
-
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
-
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
-
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.