RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic 6
Expertise: Advanced
Aug 24, 2016

Validating Dynamic Arrays

When you have a dynamic array that has no dimensions defined yet in VB, trying to get its lower or upper bound raises an error (#9 - Subscript out of range). There are a number of ways to check whether a variable contains a valid array or not, but the function below should fit the bill:

Public Function IsValidArray(ByVal Variable As Variant) As Boolean
Dim Result As Boolean
Dim Bol()  As Boolean
Dim Itg()  As Integer
Dim Lng()  As Long
Dim Cur()  As Currency
Dim Sng()  As Single
Dim Dbl()  As Double
Dim Dat()  As Date
Dim Stn()  As String
    If Not IsArray(Variable) Then Exit Function
    Select Case VarType(Variable) Xor vbArray
        Case vbBoolean
            Bol() = Variable
            Result = Not Not Bol()
        Case vbByte
            Result = LenB(CStr(Variable)) <> 0
        Case vbInteger
            Itg() = Variable
            Result = Not Not Itg()

        Case vbLong
            Lng() = Variable
            Result = Not Not Lng()
        Case vbCurrency
            Cur() = Variable
            Result = Not Not Cur()
        Case vbSingle
            Sng() = Variable
            Result = Not Not Sng()
        Case vbDouble
            Dbl() = Variable
            Result = Not Not Dbl()
        Case vbDate
            Dat() = Variable
            Result = Not Not Dat()
        Case vbString
            Stn() = Variable
            Result = Not Not Stn()
        Case vbError, vbDecimal, vbVariant, vbObject, vbUserDefinedType
            Result = LBound(Variable) <= UBound(Variable)      
        End Select
    IsValidArray = Result
End Function 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date