VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"Dim x()" - Declaring Array Variables

This section provides an introduction and examples on how to declare array variables with fixed sizes or dynamic sizes.

VBScript supports 2 ways to declare an array variable:

1. Explicit Declaration with Fixed Size - Using "Dim" statement with size included as:

   Dim array_name(upper_bound)

where "upper_bound" is positive integer specifying the upper bound of the element index. Array declared in this way has a fixed size (number of elements). By default, the size of an array is the upper bound plus 1, because index starts from 0. This size can not be changed.

2. Explicit Declaration with Dynamic Size - Using "Dim" and "ReDim" statements as shown in the following syntax:

   Dim array_name()
   ... 
   ReDim [Preserve] variable_name(upper_bound)
   ...

The "Dim" statement declares an array variable without any upper bound. The "ReDim" statement resets the upper bound to a new value. The optional key word "Preserve" specifies that all old elements must be preserved when resetting the array size. Array declared in this way can be resized at any time.

VBScript offers some other useful functions and statements for you to work with arrays:

  • IsArray(variable_name) - Returns "True" if the specified value is an array.
  • UBound(array_variable) - Returns the upper bound of the specified array.
  • LBound(array_variable) - Returns the lower bound of the specified array.
  • "For Each" - Loops through all elements in an array.
  • "Erase" - Removes all values from an array.

See sections below for details and examples on those functions and statements.

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

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

Array Data Type and Related Statements

 What Is an Array?

"Dim x()" - Declaring Array Variables

 "x(i)" - Accessing Array Elements with Indexes

 "Dim x(n)" - Fixed-Size Array Example

 "Dim x()" - Dynamic-Size Array Example

 "For Each" Statement Example

 "Erase" Statement - Removing All Elements in an Array

 Data Type "Variant()" - Array of Variant Values

 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)

 References

 Printable Copy - PDF Version

"Dim x()" - Declaring Array Variables - Updated in 2015, by Dr. Herong Yang