Previous Topic

Next Topic

Dim statement

Declares variables and allocates storage space.

This topic describes the Dim statement with MaxBasic VB compatibility selected, which is the default and is recommended. See MaxBasic VB Compatibility.

Syntax

Dim name [As type] [= value] [, name [As type] ... ]
or
Private name [As type] [= value] [, name [As type] ... ]
or
Array form: Dim name( [lower To] upper ) [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. Round or square brackets can be used. An array declared with one kind of bracket can be used with the other. 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. Integer, Long, LongLong, Byte, Single, Double, Currency and Decimal are also accepted, and declare a Number.

Value

Optional. Initial value of the variable. Not available for a Public variable or an array, or when several names share one type.

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 give a variable an initial value when you declare it. The value must suit the data type, as in an assignment:

Dim Speed As Number = 60

Dim Message As String = "Hello World"

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

Names listed before one type all take that type. The following also declares three string variables:

Dim Str1, Str2, Str3 as String

If you don't specify a data type or object type, the variable is Variant by default.

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

Dim Str1 as String, Num1 as Number, Obj1 as Object

When VB compatibility is not selected

When MaxBasic VB compatibility is not selected on System Settings - General tab, and the script has no Option VBCompatibility statement, only the last name before a type takes that type. The other names are Variant. The following declares Str3 as a String, and Str1 and Str2 as Variant:

Dim Str1, Str2, Str3 as String

See Also

MaxBasic Statements

Book Contents

Book Index