How do we find Data Type of a Variable in VBScript?
The data type of a variable can be indentified in two VBScript built-in functions.
- VarType
- TypeName
VarType :
The VarType function returns a value indicating the subtype of a variable.
Syntax : VarType(varname)
varname :The varname argument can be any variable.
TypeName :
The TypeName function returns a string containing the name of the data type of a variable.
Syntax : TypeName(varname)
varname : Required. The name of a variable.
VarType returns a numeric value indicating the sub datatype of a variable.
TypeName directly returns the name of the Sub Datatype of a variable.
VarType
The below table contains return values that indicate respective subtypes.
Return Value
|
Sub Datatype
|
Description
|
0
|
vbEmpty
|
Empty (uninitialized)
|
1
|
vbNull
|
Null (no valid data)
|
2
|
vbInteger
|
Integer
|
3
|
vbLong
|
Long integer
|
4
|
vbSingle
|
Single-precision floating-point number
|
5
|
vbDouble
|
Double-precision floating-point number
|
6
|
vbCurrency
|
Currency
|
7
|
vbDate
|
Date
|
8
|
vbString
|
String
|
9
|
vbObject
|
Automation object
|
10
|
vbError
|
Error
|
11
|
vbBoolean
|
Boolean
|
12
|
vbVariant
|
Variant (used only with arrays of Variants)
|
13
|
vbDataObject
|
A data-access object
|
17
|
vbByte
|
Byte
|
8192
|
vbArray
|
Array
|
Examples:
Dim MyCheck
MyCheck = VarType(300) ' Returns 2.
MyCheck = VarType(#10/19/62#) ' Returns 7.
MyCheck = VarType("VBScript") ' Returns 8.
TypeName directly returns the name of the Sub Datatype of a variable.
TypeName
Sub Datatype
|
Description
|
Byte
|
Byte value
|
Integer
|
Integer value
|
Long
|
Long integer value
|
Single
|
Single-precision floating-point value
|
Double
|
Double-precision floating-point value
|
Currency
|
Currency value
|
Decimal
|
Decimal value
|
Date
|
Date or time value
|
String
|
Character string value
|
Boolean
|
Boolean value; True or False
|
Empty
|
Unitialized
|
Null
|
No valid data
|
<object type>
|
Actual type name of an object
|
Object
|
Generic object
|
Unknown
|
Unknown object type
|
Nothing
|
Object variable that doesn't yet refer to an object instance
|
Error
|
Error
|
Examples:
Dim ArrayVar(4), MyType
NullVar = Null ' Assign Null value.
MyType = TypeName("VBScript") ' Returns "String".
MyType = TypeName(4) ' Returns "Integer".
MyType = TypeName(37.50) ' Returns "Double".
MyType = TypeName(NullVar) ' Returns "Null".
MyType = TypeName(ArrayVar) ' Returns "Variant()".
There are some more VBScript Built-in functions to find whether a variable datatype is specific datatype or not.
IsArray – Returns a Boolean value indicating whether a variable is an array or not.
IsDate – Returns a Boolean value indicating whether an expression can be converted to a date.
IsEmpty – Returns a Boolean value indicating whether a variable has been initialized.
IsNull - Returns a Boolean value that indicates whether an expression contains no valid data (Null).
IsNumeric - Returns a Boolean value indicating whether an expression can be evaluated as a number.
IsObject - Returns a Boolean value indicating whether an expression references a valid Automation object.
0 comments:
Post a Comment