Q(uick)BASIC Statement: STATIC
Quick View
STATIC
A BASIC declaration that makes simple variables or arrays local to a procedure (or a DEF FN function) and preserves the variable values between procedure calls
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- SHARED variable[()] [AS type] [,variable[()] [AS type]]…
- STATIC variable[()] [AS type] [,variable[()] [AS type]]…
Description/Parameter(s)
- SHARED gives procedures access to module-level variables.
- STATIC makes a variable local to a function or procedure and preserves its value between calls.
variable | The name of the module-level variable to share or variable to make static. Variable names can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.). |
AS type | Declares the data type of the variable (INTEGER, LONG, SINGLE, DOUBLE, STRING, or a user-defined type). |
Example
The program REMLINE.BAS illustrates using the SHARED and STATIC statements. To view or run this program, load REMLINE.BAS using the Open command from the File menu.
See also:
Syntax
- STATIC variablelist
Description/Parameter(s)
A STATIC statement variablelist has the following syntax:
variable[()] [AS type] [,variable[( )][AS type]]…
The variablelist takes the following arguments:
Argument | Description |
variable | Either a variable name or an array name. |
AS type | Declares the type of variable. The type argument can be INTEGER, LONG, SINGLE, DOUBLE, STRING, or a user-defined type. |
The STATIC statement can appear only in a SUB, FUNCTION, or DEF FN function.
Earlier versions of BASIC required the number of dimensions in parentheses after an array name. In QuickBASIC, the number of dimensions is optional.
Variables declared in a STATIC statement override variables of the same name shared by DIM or COMMON statements in the module-level code. Variables in a STATIC statement also override global constants of the same name.
Usually, variables used in DEF FN functions are global to the module; however, you can use the STATIC statement inside a DEF FN statement to declare a variable as local to only that function.
Note: | The STATIC attribute on SUB and FUNCTION statements declares the default for variables to be STATIC. Variables having the same name as variables shared by the module-level code are still shared. In contrast, the STATIC statement makes specific variables STATIC and overrides any variables shared by the module-level code. The $STATIC metacommand affects how memory is allocated for arrays. |
Example
STAT_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the STATIC statement. To look at the program in the View window and, optionally, to run it, load the program using the File menu's Open Program command.
The program searches for every occurrence of a certain string expression in a file and replaces that string with another string. The name of the file with these changes is the old filename with the extension .NEW. The program also prints the number of substitutions and the number of lines changed.
See also:
Syntax
- STATIC variable[()] [AS type] [,variable[()] [AS type]]…
Description/Parameter(s)
variable | The variable the procedure will use. It is either an array name followed by (), or a variable name. |
AS type | Declares the variable's type (see Details for more information about the AS type clause). |
- AS type declares the type of the variable. The type may be INTEGER, LONG, SINGLE, DOUBLE, STRING (for variable-length strings), STRING * length (for fixed-length strings), CURRENCY, or a user- defined type.
Usage Notes
- STATIC is a BASIC declaration that makes simple variables or arrays local to a procedure (or a DEF FNfunction) and preserves the variable values between procedure calls
- The STATIC statement can appear only in a SUB procedure, FUNCTION procedure, or DEF FNfunction.
- Earlier versions of BASIC required the number of dimensions in parentheses after an array name. The number of dimensions in BASIC is now optional.
- Variables declared in a STATIC statement override variables of the same name shared by DIM or COMMON statements in the module-level code. Variables in a STATIC statement also override global constants of the same name.
- Usually, variables used in DEF FNfunctions are global to the module; however, you can use the STATIC statement inside a DEF FN statement to declare a variable as local to only that function.
- The differences between the STATIC statement, the STATIC attribute, and the $STATIC metacommand are:
Statement/Attribute | Differences |
STATIC attribute on SUB and FUNCTION statements |
Declares the default for variables to be static. Variables having the same name as variables shared by the module-level code are still shared. |
STATIC statement | Makes specific variables static and overrides any variables shared by the module-level code. |
$STATIC metacommand | Affects how memory is allocated for arrays. |
Example
This example uses the STATIC statement to preserve the value of a variable between calls to the SUB procedure in which the variable is used. The program searches for every occurrence of a certain string expression in a file and replaces it with another string.
INPUT "Name of file"; F1$
INPUT "String to replace"; Old$
INPUT "Replace with"; Nw$
Rep = 0: Num = 0
M = LEN(Old$)
OPEN F1$ FOR INPUT AS #1
CALL Extension
OPEN F2$ FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
LINE INPUT #1, Temp$
CALL Search
PRINT #2, Temp$
LOOP
CLOSE
PRINT "There were "; Rep; " substitutions in "; Num; " lines."
PRINT "Substitutions are in file "; F2$
END
SUB Extension STATIC
SHARED F1$, F2$
Mark = INSTR(F1$, ".")
IF Mark = 0 THEN
F2$ = F1$ + ".NEW"
ELSE
F2$ = LEFT$(F1$, Mark - 1) + ".NEW"
END IF
END SUB
SUB Search STATIC
SHARED Temp$, Old$, Nw$, Rep, Num, M
STATIC R
Mark = INSTR(Temp$, Old$)
WHILE Mark
Part1$ = LEFT$(Temp$, Mark - 1)
Part2$ = MID$(Temp$, Mark + M)
Temp$ = Part1$ + Nw$ + Part2$
R = R + 1
Mark = INSTR(Temp$, Old$)
WEND
IF Rep = R THEN
EXIT SUB
ELSE
Rep = R
Num = Num + 1
END IF
END SUB
See also: