Dim (statement)

Syntax

Dim name [(<subscripts>)] [As [New] type] [,name [(<subscripts>)] [As [New] type]]...

Description

Declares a list of local variables and their corresponding types and sizes.

Comments

If a type-declaration character is used when specifying name (such as %, @, &, $, or !), the optional [As type] expression is not allowed. For example, the following are allowed:

  Dim Temperature As Integer
  Dim Temperature%

 

The subscripts parameter allows the declaration of dynamic and fixed arrays. The subscripts parameter uses the following syntax:

  [lower to] upper [,[lower to] upper]...

 

The lower and upper parameters are integers specifying the lower and upper bounds of the array. If lower is not specified, then the lower bound as specified by Option Base is used (or 1 if no Option Base statement has been encountered). The Basic Control Engine supports a maximum of 60 array dimensions.

 

The total size of an array (not counting space for strings) is limited to 64K.

 

Dynamic arrays are declared by not specifying any bounds:

  Dim a()

 

The type parameter specifies the type of the data item being declared. It can be any of the following data types: String, Integer, Long, Single, Double, Currency, Object, data object, built-in data type, or any user-defined data type.

 

A Dim statement within a subroutine or function declares variables local to that subroutine or function. If the Dim statement appears outside of any subroutine or function declaration, then that variable has the same scope as variables declared with the Private statement.

 

Fixed-Length Strings

Fixed-length strings are declared by adding a length to the String type-declaration character:

  Dim name As String * length

Where length is a literal number specifying the string's length.

 

Implicit Variable Declaration

If the Basic Control Engine encounters a variable that has not been explicitly declared with Dim, then the variable will be implicitly declared using the specified type-declaration character (#, %, @, $, or &). If the variable appears without a type-declaration character, then the first letter is matched against any pending Def Type statements, using the specified type if found. If no DefType statement has been encountered corresponding to the first letter of the variable name, then Variant is used.

 

Creating New Objects

The optional New keyword is used to declare a new instance of the specified data object. This keyword can only be used with data object types. Furthermore, this keyword cannot be used when declaring arrays.

At runtime, the application or extension that defines that object type is notified that a new object is being defined. The application responds by creating a new physical object (within the appropriate context) and returning a reference to that object, which is immediately assigned to the variable being declared.

When that variable goes out of scope (That is, the Sub or Function procedure in which the variable is declared ends), the application is notified. The application then performs some appropriate action, such as destroying the physical object.

 

Initial Values

All declared variables are given initial values, as described in the following table:

 

Data Type

Initial Value

 

Integer

0

 

Long

0

 

Double

0.0

 

Single

0.0

 

Date

December 31, 1899 00:00:00

 

Currency

0.0

 

Boolean

False

 

Object

Nothing

 

Variant

Empty

 

String

"" (zero-length string)

 

User-defined type

Each element of the structure is given an initial value, as described above.

 

Arrays

Each element of the array is given an initial value, as described above

 

Naming Conventions

Variable names must follow these naming rules:

  1. Must start with a letter.

  2. May contain letters, digits, and the underscore character (_); punctuation is not allowed. The exclamation point (!) can appear within the name as long as it is not the last character, in which case it is interpreted as a type-declaration character.

  3. The last character of the name can be any of the following type-declaration characters: #, @, %, !, &, and $.

  4. Must not exceed 80 characters in length.

  5. Cannot be a reserved word.

Example

The following examples use the Dim statement to declare various variable types.

Sub Main()

  Dim i As Integer
  Dim l&                      'long
  Dim s As Single
  Dim d#                      'double
  Dim c$                      'string
  Dim MyArray(10) As Integer      '10 element integer array
  Dim MyStrings$(2,10)          '2-10 element string arrays
  Dim Filenames$(5 To 10)          '6 element string array
  Dim Values(1 To 10,100 To 200)  '111 element variant array
End Sub

See Also

Redim (statement); Public (statement); Private (statement); Option Base (statement).

More information

D