Visual Basic Language Reference  

VarType Function

Returns an Integer value containing the data type classification of a variable.

Public Function VarType(ByVal VarName As Object) As VariantType

Parameter

VarName
Required. Object variable. If Option Strict is Off, you can pass a variable of any data type except a structure.

Remarks

The integer value returned by VarType is a member of the VariantType enumeration.

The following table shows the values returned by VarType for special cases of VarName.

Data type represented by VarName Value returned by VarType
Nothing VariantType.Object
DBNull VariantType.Null
Enumeration Underlying data type (Byte, Short, Integer, or Long)
Array Bitwise OR of array element type and VariantType.Array
Array of arrays Bitwise OR of VariantType.Object and VariantType.Array
Structure (System.ValueType) VariantType.UserDefinedType
System.Exception VariantType.Error
Unknown VariantType.Object

Example

This example uses the VarType function to return data type classification information about several variables.

Dim MyString As String = "MyString"
Dim MyObject As Object
Dim MyNumber, MyArray(5) As Integer
Dim MyVarType As VariantType   ' Integer enumeration.
MyVarType = VarType(MyVarType)   ' Returns VariantType.Integer.
MyVarType = VarType(MyString)   ' Returns VariantType.String.
MyVarType = VarType(MyObject)   ' Returns VariantType.Object.
MyVarType = VarType(MyArray)   ' Returns the bitwise OR of _
                                 VariantType.Array and VariantType.Integer.

See Also

Data Type Summary | Object Data Type | VariantType Enumeration