Last active
July 31, 2021 15:32
-
-
Save jim-oflaherty-jr-qalocate-com/797fc70c0ed1d94843f4b1888ff94860 to your computer and use it in GitHub Desktop.
Refactored version of CodeReview ArrayVariantSize function to simplify naming strategy...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Const SIZE_NOT_ARRAY As Long = -1 | |
Public Const SIZE_EMPTY As Long = 0 | |
'Return Value: | |
' -1 - Not an Array | |
' 0 - Empty | |
' > 0 - Defined | |
Public Function size( _ | |
ByVal value As Variant _ | |
, Optional ByVal dimensionOneBased As Long = 1 _ | |
) As Long | |
Dim result As Long: result = SIZE_NOT_ARRAY 'Default to not an Array | |
Dim lowerBound As Long | |
Dim upperBound As Long | |
On Error GoTo NormalExit | |
If (IsArray(value) = True) Then | |
result = SIZE_EMPTY 'Move default to Empty | |
lowerBound = LBound(value, dimensionOneBased) 'Possibly generates error | |
upperBound = UBound(value, dimensionOneBased) 'Possibly generates error | |
If (lowerBound < upperBound) Then | |
result = upperBound - lowerBound + 1 'Size greater than 1 | |
Else | |
If (lowerBound = upperBound) Then | |
result = 1 'Size equal to 1 | |
End If | |
End If | |
End If | |
NormalExit: | |
size = result | |
End Function | |
Public Function isEmpty( _ | |
ByVal value As Variant _ | |
, Optional ByVal dimensionOneBased As Long = 1 _ | |
) As Boolean | |
isEmpty = size(value, dimensionOneBased) = 0 | |
End Function | |
Public Function isDefined( _ | |
ByVal value As Variant _ | |
, Optional ByVal dimensionOneBased As Long = 1 _ | |
) As Boolean | |
isDefined = size(value, dimensionOneBased) > 0 | |
End Function | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment