VBcoders Browse New Submit Contact Sign In

No account? Register free

Forgot password?

Safe UBound and LBound

Kristian S. Stangeland  (8 Submissions)   VB function enhancement   VB Script   Advanced   Wed 3rd February 2021

Ever wanted to use LBound and UBound to get arrays boundaries without jumping over error message when the array is empty? These functions will replace the ordinary LBound and UBound procedures so you don’t need to worry about errors. I've also included a way to get the dimensions of an array. Just paste the following code into a module, and the problem is solved.

Inputs
SafeUBound and SafeLBound: [Address to the array], [What dimension you want to obtain] ArrayDims: [Address to the array]

Assumes
You obtain the address to an array by passing it to the VarPtrArray API call. So if you want to get the boundaries of an array called aTmp, you need to call the functions like this: lLowBound = SafeLBound(VarPtrArray(aTmp)) lHighBound = SafeUBound(VarPtrArray(aTmp)) lDimensions = ArrayDims(VarPtrArray(aTmp)) When dealing with string arrays that isn't allocated at design time, you *must* add the value 4 to the lpArray-paramenter: lLowBound = SafeLBound(VarPtrArray(aString) + 4)

Returns
As expected from the ordinary functions, except that they will return -1 when the array is empty.

Side Effects
Since the return value is minus when the array is empty it's a big chance you will get problems with minus dimensioned arrays, but who use them anyway?

API Declarations
Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Rate Safe UBound and LBound (4(4 Vote))
Safe UBound and LBound.bas

Safe UBound and LBound Comments

No comments yet — be the first to post one!

Post a Comment

0/1000 characters