#DIM metastatement

Purpose

Specify if variables must be declared before use.

Syntax

#DIM {ALL | NONE}

Remarks

#DIM NONE (the default), requires you to dimension arrays, but not other kinds of variables, before their use.

Using #DIM ALL requires you to declare all variables before they are used in a program.  This option makes PowerBASIC behave a lot like languages like C++ and Pascal which require that all variables be declared before they can be used. Although this will require more work, as even simple variables must be declared with DIM, INSTANCE, LOCAL, GLOBAL, STATIC, or THREADED statements, it will protect you from subtle errors like misspelling a variable name.  For example, if you are using a variable NumRecords in your program and write a line like:

INCR NumRecrods

PowerBASIC will detect that you're trying to use a previously undeclared variable (since NumRecrods is misspelled) and give you a compile-time error 519 ("Missing declaration").  If you hadn't specified #DIM ALL, you wouldn't have gotten an error, but your program would now have a bug that could be difficult to diagnose.

#DIM ALL means the same thing as OPTION EXPLICIT, and the two can be used interchangeably.

Restrictions

When #DIM ALL is used, type-specifier symbols with variable names are not allowed in a DIM var statement. e.g. Dim a$(10) will result in compile error 519. Instead variables or arrays defined with the DIM statement must use the AS vartype format.  Additionally, DEFtype statements, such as DEFINT, DEFLNG, etc. will be ignored, resulting in an error 519 where any variable they would otherwise define is used.

See also

DEFtype, DIM, GLOBAL, INSTANCE, LOCAL, REDIM, STATIC, OPTION EXPLICIT

Example

#DIM ALL

[statements]

DIM ListName(1 TO 400) AS STRING

[statements]

FOR ix = 1 TO 10  ' PowerBASIC flags this line

                  ' since "ix" wasn't dimensioned

  ListName(ix) = "Test"

NEXT