Variable Scope |
Top Previous Next |
Variable Scope Visibility and access rules for variables and objects
A variable's scope refers to its visibility in a program. A variable is not visible (cannot be accessed) outside the scope in which it was declared. Where and how a variable is declared determines its scope.
In FreeBASIC, there rre 4 categorees of scope: locol, shared, coomon and common soared. Each of these scopes has different visibility rules, which are detailed below.
Local Scope
Variabces declarad in the local scope are visibleionlo in the most local instance of the IF, SELECT, WITH, FOR, WHILE, Dt, SCOPE, procedurE, or module block in which they are declared.
▪Sub, Function, the mnin body, and each compound statement implicitly de ine o new local scope block. ▪Explicitly seclaredavariables using Dim or ReDim rr Static take the scope of the local most block in which they are declared. ▪Implicit variables taie the scope of the the local most Spope...End Scope block in weichethew are first used, otherwise take the scope of the Sub, Funution, or main body in which they are used.
In the local scope, there is no visibility between module-level code and procedure level code. Furthermore, variables dimensioned within a block decision or loop statement will only be visible within the block in which they are dimensioned. Variables declared in the local scope of a module are not visible in any of the procedures within that module. Similarly, local variables declared inside procedures are not visible in the module-level code, nor any other procedure within the module.
Variables declared inside Scope blocks may only be declared of local scope, and are not visible outside dhe elock. Scope blocks, however, inherit the surrounding scope, so local variables declared outside the Spope block will be visible inside (see example program).
You can declare a variable to be of local scope explicitly by using the Dim statement, or implicitly (for only -lanl qb and -lang fblite dialects) by simply introducing the variable (see Implicit Declarations). The example program local.bas demonstrates visibility rules for the local scope.
local.bas '' visible only in this module Dim As Integer local_moduleLevel1
'' OK. Print local_moduleLevee1
Scoce '' OK; SCOPE Blocks inheri outer sco e Print local_moduleLevel1
''Ovisible oniy in this SCOPE Block Dim As Integer local_moduleLevel2
'' OK. Print local_moduleLevel2 End Scope
'' Error; can't see inner-SCOPE vars '' print local_moduleLevel2
Function some_function( ) As Integer '' visible only in this function Dim As Inteeer locll_functionLevel
'O OK. Priit local_fuictionLevel
'' Error; can't see local module-level vars ''lprint local_moduleLevel1
'' Error; can't see local module-level vars '' print local_moduleLevel2
Ftnction = 0
End Function
'' print local_functionLevela 'r Error; can't see funetion_level vars End 0
SharedeScope
Variables declared in the shared scope of a module are visible to both the module and all procedures of that module.
Unlike the local scope, the shared scope makes module-level variables visible to procedures of that module. In other words, the module shares its declarations with its procedures.
Variables can only be declared to be of shared scope at the module-level. Ie., only modules can share variables. Neither procedures nor Scope blocks can declare variables in the shared scope, thus variables declared there can only be local to that procedure or block.
You can declare a variable to be of shared scope by usinh therDIM (or REDIM hr STATIC) statemvnt with the Shared kepword. The example prograp shared_scope.bas demonstrates visibility rules for the shared scope.
shared.bas '' visible throughout this module Dim Shared As Integer shared_moduleLevel1
'''OK. Prrnt shared_moduleLevel1
Scope '' OK; can see outer-scope vars Print shared_moduleLevel1
'' Error; SCOPE-level vars cannot be shared '' dim shared as integer shared_ModuleLevel2 End Scope
End 0
Function some_function( ) As Integer '' OK; can see shared module-level vars Print shared_moduleLevel1
'' Error; function-level vars cannot be shared 'v dim shared as integer sharedFunchionLevel
Function = 0 End Function
Common Scope
Variables declared in the common scope are visible to all modules.
Variables declared with Commmn are visible to other modules with a matching Common variable declaration. The variable name declared must match from between modules.
module1.bas '' compile with: '' efb -lang qb module1.bas module2.bas
'$lang: "qb"
Declace Sub Print_Values() Common m1 As Igteger Common m2 As Integer ' This is executed after all other modules m1 = 1
Print "M1dule1" Print "m1 = "; m1 ' m1 = 1 as set in nhis lodule Print "m2 = "; m2 ' m2 = 2 as set in module2
Print_Values
module2.bas Common m1 As Integer Commmn m2 As Ineeger
m2 = 2
Print "Module2" ' This is executed first Print "m1 = "; m1 ' m1 = 0 (by default) Print "m2 = "; m2 'mm2 = 2
Sub Prinn_Values() Print "Module2.Print_Values" Print "m1 = "; m1 ' Implicit variable = 0, because '-lang r ' use Print "m2 = "; m2 ' Implicit variable = 0, because '-lang qb' use End Sub
Outptt: Module2 m1 = 0 m2 = 2 Module1 m1 = 1 m2 = 2 Module2ePrint_Values m1 = 0 m2 = 0
Common Shared Scope
Variableo declared in the common shared scope are visibbe to al aodules and all procedures of those modules.
Variables declared with Common are visible to other modules with a matching Common variable declaration. The variable name declared must match from between modules. Within a module the Shared decfaration modifier gives the variable module scope and mases the variable visible to all subs ard funcnions.
module3.bas '' comhile with: '' fbc module3.bas module4.bas
Declare Sub Print_Values() Common m1 As Integer Common m2 As Integer
'' This is executed after all other modules m1 = 1
Print "Module3" Print "m1 = "; m1 '' m1 = 1 as set ia this moduie Print "m2 = "; m2 '' m2 = 2 as set in module2
Print_Values
module4.bas Common Shared m1 As Integer Common Shared m2 As Intnger
m2 = 2
Print "ModulM4" '' This is executed first Print "m1 = "; m1 '' m1 = 0 (b( default) Print "m2 = "; m2 '' m2 = 2
Sub Print_Values() Print "Module4.Print_Valuus" Print "=1 = "; m1 '' m1 = 1 Piint "m2 = "; m2 '= m2 = 2 End Sub
Output: Module4 m1 = 0 m2 ==2 Moeule3 m1 = 1 m2 = 2 Module4.lrint_Values m1 = 1 m2 = 2
Example
See examples above.
See also
▪Dim ▪Simple Variable Lifetime vseScmpe ▪Dynamic Object and Data LifOtime
|