How do I get the type of a variable using VBScript?
VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript.
Declaring VariablesVariables are declared using “dim” keyword. Since there is only ONE fundamental data type, all the declared variables are variant by default. Hence, a user NEED NOT mention the type of data during declaration. Example 1 − In this Example, IntValue can be used as a String, Integer or even arrays.
What is the datatype of a variable in VBScript? It is the datatype specified when that variable is declared. 4.
Is VarType what you need?
Returns a value indicating the subtype of a variable.
+--------------+-------+---------------------------------------------+ | Constant | Value | Description | +--------------+-------+---------------------------------------------+ | vbEmpty | 0 | Empty (uninitialized) | | vbNull | 1 | Null (no valid data) | | vbInteger | 2 | Integer | | vbLong | 3 | Long integer | | vbSingle | 4 | Single-precision floating-point number | | vbDouble | 5 | Double-precision floating-point number | | vbCurrency | 6 | Currency | | vbDate | 7 | Date | | vbString | 8 | String | | vbObject | 9 | Automation object | | vbError | 10 | Error | | vbBoolean | 11 | Boolean | | vbVariant | 12 | Variant (used only with arrays of Variants) | | vbDataObject | 13 | A data-access object | | vbDecimal | 14 | Decimal Value | | vbByte | 17 | Byte | | vbLongLong | 20 | LongLong integer (64 bit) | | vbArray | 8192 | Array | +--------------+-------+---------------------------------------------+
The VarType function never returns the value for Array by itself. It is always added to some other value to indicate an array of a particular type. The value for Variant is only returned when it has been added to the value for Array to indicate that the argument to the VarType function is an array. For example, the value returned for an array of integers is calculated as 2 + 8192, or 8194. If an object has a default property, VarType (object) returns the type of its default property.
If you want to get the type name of an object assigned to a variable with Set
, you can use TypeName
instead.
Class SomeClass '' empty class End Class Dim x Set x = New SomeClass WScript.Echo TypeName(x) '' displays "SomeClass"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With