Dim Command

Purpose

declares a variable of any kind.

Syntax

[Local | Global | Static] Dim varname[([subscripts])] [As [New] type] [, varname[([subscripts])]] [As [New] type]

Description

In GFA-BASIC 32 it is mandatory to declare variables before they can be used. Dim is the general command to declare a variable. Others are Global, Local, and Static. A variable declared with Dim has local scope inside a subroutine and global scope when it is declared in the main part of the program. Optionally, Global, Local, and Static may be used together with Dim, but when used these commands may do without the Dim part. The following is allowed:

Dim a%, b&, s$, v

Global Dim a As Long, b As Short, s As String, v As Variant

Global a As Long, b As Short, s As String, v

If you don't specify a data type or object type, and there is no Deftype statement, the variable is Variant by default.

Arrays can have up to 7 dimensions. The subscripts argument uses the following syntax:

[lower To] upper [, [lower ..] upper] . . .

When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.

You can also use the Dim statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to re-declare a dimension for an array variable whose size was explicitly specified in a Dim statement, an error occurs.

If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can be used only on the following object types: Picture, Font, StdPicture, StdFont, Collection, DisAsm, CommDlg and ImageList.

Example

Dim Names(9)                ' Declare an array with 10 elements.

Dim Names(1 To 9)           ' Declare an array with 9 elements.

Dim Names(0 .. 9, 0 .. 1)   ' Declare an array with 2 dimensions.

Dim Names()                 ' Declare a dynamic array

Dim MyVar, MyNum            ' Declare two variables

Dim dis As New DisAsm       ' Declare a new instance of the object

Remarks

Allowed are Types in arrays, arrays in Types, or Ocx-array‘s.

A local array doesn't need to be erased (Erase) at the end of a subroutine, this is done automatically.

If an array is dimensioned under Option Base 0 and then ReDim'ed under Option Base 1, the array retains its original starting element of 0; the same happens the other way around.

See Also

Global, Local, Static, Dim(), IndexCount, LBound, UBound, Erase, ReDim, Clr

Boolean, Byte, Card, Short, Word, Int16, Long, Int, Integer, Int32, Int64, Large, Single, Double, Currency, Date, Handle, String, Variant, Object

{Created by Sjouke Hamstra; Last updated: 30/09/2014 by James Gaite}