VBScript » Statements » Dim

Version: 1.0

Syntax:
Dim

The Dim statement allows you to explicitly declare one or more new variables and to allocate storage (memory) space.

While you do not have to use Dim to create new variables in VBScript, the wise programmer prefers to use Dim. In fact, many programmer purposely include the Option Explicit statement in all of their VBScript programs which mandates that all variables be explicitly declared.

Examples

Code:
<% Dim myvariable %>
<% Dim OneVar, TwoVar, ThreeVar, FourVar, MoreVar, EvenMoreVar %>
Language(s): VBScript
Code:
<% Dim SixElementArray(5) %>
Explanation:

Dim can also be used to create static (fixed) and dynamic arrays.

A static array has the number of elements declared by the Dim statement. However, you must remember that the elements in an array are numbered starting at zero. Consider this Dim declaration. It creates a static array containing six elements that are numbered 0, 1, 2, 3, 4 and 5.

Language(s): VBScript
Code:
<%
Dim SomeArray()
...
ReDim SomeArray(22)
...
ReDim SomeArray(500)
%>
Explanation:

A dynamic array is declared using empty parentheses. At a later point in your program, you can use the ReDim statement to declare the number of dimensions and elements. In fact, you can redeclare a dynamic array as many times as you desire.

Language(s): VBScript
Code:
<% Dim ThreeDimensionArray(22, 14, 200)%>
Explanation:

Arrays can also have up to sixty dimensions. For example, this code creates a three dimensional array. The first dimension has 23 elements, the second dimension has 15 elements and the third dimension has 201 elements. Therefore, there are a total of 23x15x201 = 69345 elements in this array.

The number of dimensions that you can create is limited by the available memory. If you exceed the available memory while declaring an array, you will get an error message.

Language(s): VBScript

See Also: