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

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

Variable Declaration and Assignment Statement

Variable Declaration and "Dim" Statement

 Assigning Values to Variables - "=" Statement

 Empty - The Default Value of a Variable

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)


 Full Version in PDF/EPUB