Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows
Visual Basic > Other sample source codes
Safely checking the bounds of an array
Safely checking the bounds of an array Using the Ubound and Lbound functions on an uninitialised typed array causes a "Subscript out of range" error. Use the following simple functions to safely check the bounds of any variant datatype. 'Purpose : Replacement function to UBound. Returns Upper bound of an array. 'Inputs : avValues The array to determine the upper bound of ' lDimension The dimension of the array ' [lDefault] The value to return if an error occurs 'Outputs : The upper bound specific dimension of avValues 'Notes : This function uses UBound with an error handler and allows you to specify a default return value Function UUBound(avValues As Variant, Optional lDimension As Long = 1, Optional lDefault As Long = 0) As Long On Error GoTo ErrFailed UUBound = UBound(avValues, lDimension) Exit Function ErrFailed: 'Error Occurred, Return the Default and clear error UUBound = lDefault On Error GoTo 0 End Function 'Purpose : Replacement function for LBound (has extra functionality and error handling) 'Inputs : avValues The array to find the LBound. ' lDimension Which dimension's lower bound to return ' [lDefault] The value to return if an error occurs 'Outputs : The lower bound of the array, or lDefault if an error occurs 'Notes : LBound raises and error if you try and check the the lower bound ' of a dimension that doesn't exist. Function LLBound(avValues, Optional lDimension As Long = 1, Optional lDefault As Long = 0) As Long On Error GoTo ErrFailed LLBound = LBound(avValues, lDimension) Exit Function ErrFailed: LLBound = lDefault End Function
Privacy Policy
|
Link to Us
|
Links