Previous Topic

Next Topic

Dim statement

Declares variables and allocates storage space.

Syntax

Dim name [As type] [, name [As type] ... ]
or
Private name [As type] [, name [As type] ... ]
or
Array form: Dim name[ [lower To] upper ] [As type]

Dim statement syntax has these named arguments:

Parameter

Description

Varname

Name of the variable. Follows standard variable naming conventions.

Subscripts

Optional. Dimensions of an array variable. Only single dimension arrays are supported. The subscripts argument uses the following syntax:

[lower To] upper or just upper Examples:
Dim A[5] As Number ' indices 1..5
Dim B[0 To 9] As Number ' indices 0..9

When not explicitly stated in lower, the lower bound of an array is 1.

Type

Data type of the variable. Can be Boolean, Number, Date, String or Object.

Usage

Use the Dim statement to declare the data type of a variable. For example, the following statement declares a variable as a Number:

Dim NumberOfEmployees AS Number

You can also use a Dim statement to declare a variable as an Object. The following declares a variable for a new instance of a form (this is commonly used in automation and scripting):

Dim Form1 AS Object

You can declare multiple variables in one line. The following will declare three string variables:

Dim Str1 as String, Str2 as String, Str3 as String

If you don't specify a data type or object type, the variable is Variant by default. The following will declare three variables, but only the last will be created as a String type. The other two will be of type Variant.

Dim Str1, Str2, Str3 as String

You can also mix types on a single line, for example:

Dim Str1 as String, Num 1 as Number, Obj1 as Object

See Also

MaxBasic Statements

Book Contents

Book Index