Understanding Variables in VBScript - Initializing and Debugging Variables (Page 3 of 4 )
Now that you’ve seen what variables are and how to name them, it’s time to begin putting them to use in your own scripts. To do that, you first need to learn how to declare, or create them.
There are two terms that you will hear used when talking about variables. You should become familiar with their proper definitions. Declaring a variable means creating its reference in memory. This makes it available for use. Initializing a variable means assigning its first value. In VBScript, variables are often declared and initialized at the same time.
The proper method for declaring a variable in VBScript is to use the Dim statement. The Dim statement allocates memory for the variable.
Dim intNumber
You declare any number of variables in a single statement by separating them with commas.
Dim strName, strAddress, strCity, strState, intZipCode
In VBScript, you do not need to declare the type associated with the variable as you would in VisualBasic.
Dim intNumber As Integer
The use of the As statement in VBScript will result in an "Expected end of statement" error, since the As keyword is not recognized.
Explicit declarations allow you to control the scope of your variables more effectively.
VBScript will automatically create any variable found in an expression. There may be times when you don’t want this to happen. Imagine that you are trying to debug a script where you mistyped a variable name. Your script will most likely not perform as you intended it to, but in most cases, it won’t throw any errors either. Forcing VBScript to only use explicitly declared variables can help you avoid this scenario.
Option Explicit:
Using the Option Explicit statement at the beginning of your script will force the VBScript interpreter to throw an error any time it encounters a variable that was not explicitly declared. In other words, it forces the use of the Dim statement.
Next: Data types >>
More BrainDump Articles
More By Nilpo