Q(uick)BASIC Statement: SHARED
Quick View
SHARED
A statement that gives a SUB or FUNCTION procedure access to variables declared at the module level without passing them as parameters
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
- SHARED variable [AS type] [,variable [AS type]]…
Description/Parameter(s)
The argument variable is either an array name followed by () or a variable name. The AS clause can be used to indicate the variable's type. The type argument can be INTEGER, LONG, SINGLE, DOUBLE, STRING, fixed-length string (STRING * length), or a user-defined type.
By using either the SHARED statement in a SUB or FUNCTION procedure, or the SHARED attribute with COMMON or DIM in the module-level code, you can use variables in a procedure without passing them as parameters. The SHARED attribute shares variables among all procedures in a module, while the SHARED statement shares variables between a single procedure and the module-level code.
Note: | The SHARED statement only shares variables within a single compiled module. It does not share variables with programs in the Quick library or with procedures compiled separately and linked to the program. The SHARED statement only shares variables between the module-level code and a SUB or FUNCTION in the same module. |
The SHARED statement can appear only in a SUB or FUNCTION.
Example
SHARE_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the SHARED 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 calls a subprogram named CONVERT that converts the input decimal number to its string representation in the given new base. The string N$ is shared by the subprogram and the main program.
Syntax
- SHARED variable[()] [AS type] [,variable[()] [AS type]]…
Description/Parameter(s)
- 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
- By using either the SHARED statement in a SUB or FUNCTION procedure, or the SHARED attribute with COMMON or DIM in the module-level code, you can use variables in a procedure without passing them as parameters.
- The SHARED attribute used with COMMON or DIM shares variables among all procedures in a module, while the SHARED statement shares variables between a single procedure and the module-level code.
Important
- The SHARED statement shares variables only within a single compiled module. It does not share variables with programs in the Quick library or with procedures compiled separately and linked to the program. The SHARED statement shares variables only between the module-level code and a SUB or FUNCTION procedure in the same module.
- The SHARED statement can appear only in a SUB or FUNCTION procedure.
Example
This example uses the SHARED statement to share a string variable between the main program and a SUB procedure. The program converts a decimal number to its string representation in a new base.
DEFINT A-Z
DO
INPUT "Decimal number (input number <= 0 to quit): ", Decimal
IF Decimal <= 0 THEN EXIT DO
INPUT "New base: ", Newbase
N$ = ""
PRINT Decimal; "base 10 equals ";
DO WHILE Decimal > 0
CALL Convert(Decimal, Newbase)
Decimal = Decimal \ Newbase
LOOP
PRINT N$; " base"; Newbase
PRINT
LOOP
SUB Convert (D, Nb) STATIC
SHARED N$
'Take the remainder to find the value of the current digit.
R = D MOD Nb
'If the digit is less than ten, return a digit (0-9).
'Otherwise, return a letter (A-Z).
IF R < 10 THEN Digit$ = CHR$(R + 48) ELSE Digit$ = CHR$(R + 55)
N$ = RIGHT$(Digit$, 1) + N$
END SUB
See also: