Data Types (VBScript)

Variables in VBScript are variants by default. Because of this, variables can be coerced into any data type at any time. For example, the following code is completely legal in VBScript:

Dim MyVariant

MyVariant = 53
MyVariant = "Hello World"

In this example, MyVariant is first coerced into an integer, and then it is coerced into a string. Below is a complete list of available Data Types in VBScript.

Data Type Coercion Function Test Function VarType() return value TypeName() return value
Empty n/a IsEmpty() 0 (vbEmpty) Empty
Null n/a IsNull() 1 (vbNull) Null
Integer CInt() isNumeric() 2 (vbInteger) Integer
Long CLng() isNumeric() 3 (vbLong) Long
Single CSng() isNumeric() 4 (vbSingle) Single
Double CDbl() isNumeric() 5 (vbDouble) Double
Currency CCur() isNumeric() 6 (vbCurrency) Currency
Date CDate() isDate() 7 (vbDate) Date
String CStr() n/a 8 (vbString) String
Object n/a isObject() 9 (vbObject) Object
Error n/a n/a 10 (vbError) Error
Boolean CBool() n/a 11 (vbBoolean) Boolean
Variant CVar() n/a 12 (vbVariant) Variant
Decimal n/a isNumeric() 14 (vbDecimal) Decimal
Byte CByte() isNumeric() 17 (vbByte) Byte
Array n/a isArray() 8192 (vbArray) Array

The Coercion Function in the second column of this table is the function that you would use to force your variable to become a certain data type. For example, if I wanted to store the number 12 as a string, I could use the CStr() function as follows:

Dim MyString

MyString = CStr(12)

The test function in the third column returns a true or false value stating whether or not the provided variable is of that data type.

Both VarType() and TypeName() from the forth and fifth columns of the table are used to determine what data type a particular variable is. The only difference is that VarType() returns an integer (which can then be checked using the constant in parenthesis) and TypeName returns a String. As an example, the following two scripts have the same output.

Dim MyString
MyString = "Hello world"

Dim MyString
MyString = "Hello World"

If VarType(MyString) = vbString Then
End If
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License