Name
Dim Statement
Syntax
Dimvarname
[([subscripts
])],varname
[([subscripts
])]
-
varname
Use: Required
Your chosen name for the variable.
-
subscripts
Use: Optional
Dimensions of an array variable.
Description
Declares and allocates storage space in memory for variables. The
Dim
statement is used either at the start of a
procedure or the start of a module or scripting block. In the first
case, the variable declared using Dim
is local to
the procedure. In the second, it’s available throughout the
module.
Rules at a Glance
You can declare multiple variables in a single
Dim
statement, as long as you use a comma to delimit them.When variables are first initialized with the
Dim
statement, they have a value of Empty. In addition, the following statements will both evaluate toTrue
:vVar = 0 ' True expression vVar = "" ' True expression
To declare array variables, use the following syntax:
- Fixed length, single dimension
Dim
arrayname
(
upper
)
Example:
Dim
myArray(10)
- Fixed length, multidimension
Dim
arrayname
(
upper
,
upper
,
...)
Example:
Dim
MyArray(20,30)
- Variable length, single or multidimension
Dim
arrayname
( )
Example:
Dim
myArray( )
You can declare a multidimensional array with up to 60 dimensions.
Variable-length arrays can be resized using the
ReDim
statement. Fixed-length arrays can’t be resized.
Example
The example shows how to use the Dim
statement to
define a variable that receives an array returned by a function:
<HTML> <HEAD> <TITLE>Returning an Array from a Subroutine</TITLE> <SCRIPT ...
Get VBScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.