Local Command

Purpose

Declares local variables in a subroutine or main program.

Syntax

Local [Dim] varname[()] [As [New] type] [ = value], …

Local type varname1 [ = value], varname2 [ = value], …

Local varname1$ [ = value], varname2% [ = value], …

varname: name of variable

type: Optional. Data type of the variable; may be Byte, Boolean, Card, Short, Word, Integer, Long, Large, Currency, Single, Double, Date, String, (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined.

Description

Local declares local variables. When used in the main program, the variable's scope is limited to the main part and isn't known in subroutines. In this respect, Dim and Local differ. Dim declares local variables as well, but when declared in the main program they are considered global.

The New keyword enables implicit creation of a few GFA-BASIC 32 objects, like DisAsm, Collection, StdFont, Font, StdPicture, Picture, CommDlg, and ImageList. 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't be used to declare variables of any intrinsic data type.

Variables declared using the Global (or Public) statement are available to all procedures in the program.

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

Variables can be initialized while they are declared.

When a variable isn't explicitly initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it were a separate variable.

Example

OpenW 1

AutoRedraw = 1

Global a%, x%, i%

a% = 0

For i% = 1 To 10

a% += i%

test (a%)

Next i%

Print a% // Prints; 205

 

Procedure test(ByRef a%)

Local i%

For i% = 1 To 5

a% += i%

Next i%

EndProc

The For...Next loop counter i% is defined both as a global and a local variable.

Remarks

See Global for a more detailed description.

Known Issues

When using local arrays, you may get a memory leak problem. This stems from the fact that the compiler forgets to add destruction code for local arrays when an explicit local declaration of a string variable is absent. As a workaround, in any procedure, function or sub which declares a local array, add a local string variable dummy$ if none other is present.

See Also

Global, Dim, Static

{Created by Sjouke Hamstra; Last updated: 12/10/2014 by James Gaite}