|
-
Aug 11th, 2000, 05:24 AM
#1
Thread Starter
Junior Member
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.
-
Aug 11th, 2000, 06:37 AM
#2
transcendental analytic
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.
-
Aug 11th, 2000, 08:51 AM
#3
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.
-
Aug 11th, 2000, 08:56 AM
#4
Addicted Member
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
-
Aug 12th, 2000, 06:11 PM
#5
Thread Starter
Junior Member
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...
-
Aug 13th, 2000, 11:05 AM
#6
Monday Morning Lunatic
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
-
Aug 13th, 2000, 11:49 AM
#7
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
-
Oct 16th, 2000, 11:55 AM
#8
Fanatic Member
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
-
Oct 16th, 2000, 12:08 PM
#9
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|