Understanding Variables in VBScript - Data types (Page 4 of 4 )
As I mentioned previously, there are multiple data types in VBScript. They can be divided into two major categories: numeric and non-numeric. As their name implies, numeric variables are variables whose value is recognized as a number. Non-numeric types are the opposite and include strings, Booleans, and empty variables among others.
You can determine a variable’s data subtype by using VBScript’s VarType or TypeName function. The table below lists the return values for the VarType function and also serves as a list of the available data subtypes in VBScript.
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 |
vbByte | 17 | Byte |
vbArray | 8192 | Array |
The TypeName function works similarly, but instead it returns the variable type as a string. You can see an example of both in the example below.
Dim MyCheck
MyCheck = VarType(300) ' Returns 2.
MyCheck = VarType(#10/19/62#) ' Returns 7.
MyCheck = VarType("VBScript") ' Returns 8.
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()".
VBScript provides a number of other functions that can be used to determine the type and state of your variables. These functions return Boolean values according to the table below.
IsArray | Returns True if a variable references an array. |
IsDate | Returns True if a value is a date or can be converted to a date. |
IsEmpty | Returns True if a variable is uninitialized. |
IsNull | Returns True if a variable is initialized but does not contain valid data. |
IsNumeric | Returns True if a variable’s value can be interpreted as a number. |
IsObject | Returns True if a variable represents an object. |
This article is designed to be an introduction to using variables in VBScript. At this point, you should be able to create and implement variables in VBScript, and also be aware of their types and states. In future articles, I will demonstrate how to work with specific data types as well as how to convert between them.
Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |