VBScript Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
Variable Declaration and "Dim" Statement
This section provides a quick introduction of what is a variable and how variable can be declared explicitly with a Dim statement and implicitly with an assignment statement.
Like many other programming languages, VBScript uses variables to reserve memory to store data and to name that memory location.
A variable must be declared with a name and a specific data type. Since VBScript supports only one data type, Variant, all variables will be declared as Variant by default. Here are some rules on variable declaration:
1. Explicit Declaration - A variable is declared with the "Dim" statement in the following syntax:
Dim variable_name, ...
where "variable_name" is a text label to identify this variable. Multiple variables can be declared with single "Dim" statement. The data type of this variable will be Variant.
Note that, in Visual Basic, you are allowed to specify a specific data type in the Dim statement, because multiple data types are supported in Visual Basic. So the following statement is valid in Visual Basic:
Dim author As String ' Allowed in Visual Basic
But in VBScript, you are not allowed to specify the data type in a Dim statement. So statements below are not valid in VBScript:
Dim author As String ' Not allowed in VBScript Dim buffer As Variant ' Not allowed in VBScript
2. Implicit Declaration - A variable name is used on the left side of an assignment statement without being declared previously. The data type of an implicitly declared variable is "Variant". Here are some examples of variable implicit declaration:
author = "Herong" ' Implicit declaration of "author" buffer = 1/3 ' Implicit declaration of "buffer"
Just in case you want to know, "Dim" is an abbreviation for "Declare in Memory", not a short for "Dimension" (Thanks to Peter for the correction).
Some rules about variable names:
Table of Contents