Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Variables are named locations within computer memory that can be used to store data. Most scripting languages allow implicit declaration of variables, enabling you to use a variable without formally declaring your intention to use that variable. For example, you can run the following script without encountering an error of any kind, even though the first line in the script assigns a value to the variable sngDegreesCelsius. Even though this variable has not been declared (meaning that VBScript has not been informed of its existence), the value 11 is assigned to the variable.
sngDegreesCelsius = 11 sngDegreesFahrenheit = ConvertToFahrenheit(sngDegreesCelsius) Wscript.Echo sngDegreesFahrenheit Function ConvertToFahrenheit(ByVal sngDegreesCelsius) ConvertToFahrenheit = (sngDegreesCelsius * (9/5)) + 32 End Function
Implicit variable declaration can make writing scripts faster and easier; at the same time, however, it can lead to subtle errors that are difficult to diagnose and fix.
To illustrate, the previous script converts 11 Celsius to Fahrenheit (51.8 ). The following script should do the same thing, but it echoes the value 32 instead.
sngDegreesCelsius = 11 sngDegreesFahrenheit = ConvertToFahrenheit(sngDegreesCelsius) Wscript.Echo sngDegreesFahrenheit Function ConvertToFahrenheit(ByVal sngDegreesCelsius) ConvertToFahrenheit = (sngDegresCelsius * (9/5)) + 32 End Function
The preceding script provides an incorrect answer because of a typographical error. Instead of typing sngDegreesCelsius in line 6, the script writer typed sngDegresCelsius, leaving out one of the two e s in Degrees. As a result, the equation uses the value of sngDegresCelsius instead of sngDegreesCelsius. Because sngDegresCelsius has never been assigned a value, it is treated as an Empty variable with the value 0. Consequently, 0 is multiplied by 9/5, resulting in 0. The script then adds 32 to that 0 and returns an incorrect answer.
Mistakes like this can be difficult to catch. The syntax is correct, so no error message will be generated. You expected to get a numeric value other than 11, and you got one. When embedded inside a much larger script, this typographical error can be very difficult to find and correct.
To help avoid problems like this, you can explicitly declare all your variables. When explicit variable declaration is in effect, any variables that are not specifically declared in the script will result in a run-time error.
For example, in the following script the explicit declaration of variables is forced by the use of the VBScript Option Explicit statement, and each variable is then declared using a Dim statement:
Option Explicit Dim sngDegreesCelsius Dim sngDegreesFahrenehit sngDegreesCelsius = 11 sngDegreesFahrenheit = ConvertToFahrenheit(sngDegreesCelsius) Wscript.Echo sngDegreesFahrenheit Function ConvertToFahrenheit(ByVal sngDegreesCelsius) ConvertToFahrenheit = (sngDegresCelsius * (9/5)) + 32 End Function
When the preceding script runs, the scripting host encounters an undeclared variable. As a result, the script halts execution and displays an error message similar to this:
C:\Scripts\TempConvert.vbs(10, 5) Microsoft VBScript runtime error: Variable is undefined: 'sngDegresCelsius'
To declare variables in VBScript:
Option Explicit Dim intFirstNumber ' First number in our simple equation Dim intSecondNumber ' Second number in our simple equation Dim intTotal ' Sum of intFirstNumber and intSecondNumber
Send us your feedback | « Previous | Next » |