VBScript Data Types
Microsoft® Windows® 2000 Scripting Guide
VBScript is a typeless language. This means that variables cannot be restricted to a single data type. VBScript does not allow you to specify in advance that a particular variable can hold only a particular kind of data. Instead, VBScript uses a single kind of variable, known as a variant, which can store any kind of data.
By contrast, a programming language such as C++ is considered strongly typed because you must specify in advance the type of data that can be stored in a variable. Your code will generate an error if you attempt to store any other kind of data in that variable. If you specify that a variable can only contain numeric data, the program will crash if you try to store alphabetic data in that variable.
Variants can make writing scripts easier; you can declare and use variables without considering the type of data being stored in them. However, variants can introduce a new set of problems into a script if you do not understand the process of type coercion.
Working with Type Coercion
Scripting languages appear typeless only to the script writer. Internally, scripting languages must still work with data types. For example, when presented with a simple statement such as c = a + b
, the scripting language must derive typed values for both a and b. In other words, it must take those two variants and create typed values such as integer or string. After it has derived the typed values, the scripting language can then perform the operation.
The process of deriving typed values is known as type coercion because the variant value is "coerced" into temporarily storing a new, typed, value. Type coercion is based on specific rules about how and when to coerce variants into different data types, and, for the most part, VBScript handles type conversions without any problem.
However, type coercion can lead to trouble, as shown in the following:
intFirstNumber = InputBox("Please enter the first number:")
intSecondNumber = InputBox("Please enter the second number:")
intTotal = intFirstNumber + intSecondNumber
Wscript.Echo intTotal
If you run the script and enter 4 as the first number and 2 as the second number, the computer echoes back "42" as the answer to 4 + 2, rather than the expected answer of 6.
This is because the addition operator is valid both for numbers and for strings. VBScript is given two values (4 and 2), with no way to know which data type these values represent. With no other information available, VBScript uses type coercion to convert the two variables to string data.
By contrast, the following script snippet returns the correct value (2) if you enter the numbers 4 and 2. This is because the division operator works only with numbers, so VBScript uses type coercion to correctly convert the two values to numeric data.
intFirstNumber = InputBox("Please enter the first number:")
intSecondNumber = InputBox("Please enter the second number:")
intTotal = intFirstNumber / intSecondNumber
Wscript.Echo intTotal
To avoid the problems that can occur with type coercion, explicitly declare data types when performing operations, a process referred to as casting type values. For example, the following script uses the VBScript function CInt (convert to integer) to convert the input variables to integers before adding them:
intFirstNumber = CInt(InputBox("Please enter the first number:"))
intSecondNumber = CInt(InputBox("Please enter the second number:"))
intTotal = intFirstNumber + intSecondNumber
Wscript.Echo intTotal
Table 2.6 lists the type conversion functions used in VBScript.
Table 2.6 VBScript Type Conversion Functions
Function |
Description |
---|---|
CBool |
Converts any nonzero value to True and 0 (zero) to False. |
CByte |
Converts an expression to a Byte value. |
CCur |
Converts an expression to a Currency value. |
CDate |
Converts an expression to a Date value. |
CDbl |
Converts an expression to a Double value. |
CInt |
Converts an expression to an Integer value. If the fractional part of the expression is .5, CInt will round the value to the nearest even number. For example, 3.5 will be rounded to 4, and 6.5 will be rounded to 6. |
CLng |
Converts an expression to a Long value. |
CSng |
Converts an expression to a Single value. |
CStr |
Converts an expression to a String value. |
Working with Empty Variables and Null Variables
Understanding the difference between an Empty variable and a Null variable can make an equally big difference in the success or failure of your scripts. An Empty variable is a variable that has not been initialized. For example, in a statement such as Dim curBonus
, the variable is considered "empty" until you set curBonus equal to a value. An Empty variable is coerced into having the value 0 when used as a number and coerced into having the value "" (zero-length text string) when used as a string.
By contrast, a Null variable is a variable that has not had a valid value assigned to it. Typically, Null variables are derived from database operations. Suppose you query a database, retrieve the current bonus field for a particular employee, and assign that value to the variable curBonus. If no bonus has been assigned, the value of curBonus will be Null. Note that while curBonus could be 0, you do not know for sure if it is 0. You cannot assume that the value is 0; in this case, the user might actually have a $5,000 bonus, but this value has not been entered into the database yet. This is why VBScript draws a distinction between Empty variables and Null variables.
The difference between an Empty variable and a Null variable is apparent when doing mathematical operations. For example, in the following script snippet, the value of curBonus is set to Empty, and then curBonus is added to curBaseSalary (50,000). The net result: 50,000 + 0 = 50,000.
curBonus = Empty
curBaseSalary = 50000
curTotalCompensation = curBaseSalary + curBonus
Wscript.Echo curTotalCompensation
In the revised version of this script, the same operation is performed, this time setting curBonus to Null. When you run the calculation, however, you do not get 50,000 (that is, 50,000 + 0). Instead, the answer is null. Anytime a Null variable is used in a mathematical equation, the end result is always null. This is because you do not know the actual value of a Null variable. You cannot substitute 0 because you do not know whether the value of that variable really is 0. Because you do not know the value, you do not know what the result would be if you used that value in a calculation. Thus, the answer has to be Null. (Think of Null as meaning, "I do not know.")
curBonus = Null
curBaseSalary = 50000
curTotalCompensation = curBaseSalary + curBonus
Wscript.Echo curTotalCompensation
Null values can be a problem when working with databases and when using ADSI to retrieve data from Active Directory. Fortunately, you can use the IsNull method to ensure that a variable is not Null. For example, the following script checks the value of the variable curBonus. If curBonus is Null, the value 0 is explicitly assigned to the variable. This allows the variable to be used in the calculation. Alternatively, you can choose not to perform the calculation but instead echo a message like, "No bonus information available for this employee."
curBonus = Null
curBaseSalary = 50000
If IsNull(curBonus) Then
CurBonus = 0
End If
curTotalCompensation = curBaseSalary + curBonus
Wscript.Echo curTotalCompensation