Variable Scope
 
变量和对象的可见性和访问规则

变量的范围是指其在程序中的可见性。变量在其声明的范围之外是不可见的(不能被访问)。声明变量的位置和方式决定其范围。

在FreeBASIC中,有4种范围:local shared common common shared .这些范围中的每一个具有不同的可见性规则,具体如下。

本地范围


在本地作用域中声明的变量仅在声明它们的IF,FOR,SCOPE,函数或模块的最本地实例中可见。

  • SubFunction,主体和每个复合语句隐式定义一个新的本地作用域。
  • 使用DimReDim显式声明的变量占据其声明的本地最大块的范围。
  • 隐式变量采用最先使用它们的局部最大Scope...End Scope块的范围,否则将使用SubFunction或其主体的范围。

在本地范围内,模块级代码和功能级代码之间没有可见性。此外,在块决策或循环语句中定义的变量将仅在其尺寸标注的块内可见。在模块的本地范围内声明的变量在该模块中的任何函数中都不可见。类似地,在函数内声明的局部变量在模块级代码中也不可见,也不会在模块中显示任何其他函数。

Scope块内声明的变量只能被声明为本地作用域,并且在块外部不可见。Scope块,但是,继承了周围的范围,所以在Scope块之外声明的局部变量将在(see example program )内部可见。

您可以通过使用Dim语句明确地声明变量为本地作用域,或者通过简单地引入变量(see Implicit Declarations )来隐式地声明变量。示例程序local.bas演示了本地作用域的可见性规则。

local.bas
'' visible only in this module
Dim As Integer local_moduleLevel1

'' OK.
Print local_moduleLevel1

Scope
  '' OK; SCOPE Blocks inherit outer scope
  Print local_moduleLevel1
  
  '' visible only 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 Integer local_functionLevel

  '' OK.
  Print local_functionLevel

  '' Error; can't see local module-level vars  
  '' print local_moduleLevel1

  '' Error; can't see local module-level vars
  '' print local_moduleLevel2

  Function = 0

End Function

'' print local_functionLevel                    '' Error; can't see function_level vars
End 0


共享范围


在模块的共享作用域中声明的变量对该模块的模块和所有功能都是可见的。

与本地范围不同,共享范围使模块级变量对该模块的功能可见。换句话说,模块shares 具有其功能的声明。

变量只能在模块级别声明为共享作用域。也就是说,只有模块可以共享变量。函数和Scope块都不能在共享作用域中声明变量,因此在此处声明的变量只能是该函数或块的本地变量。

您可以使用带有Shared关键字的DIM语句来将变量声明为共享作用域。示例程序shared_scope.bas演示共享范围的可见性规则。

shared.bas
'' visible throughout this module
Dim Shared As Integer shared_moduleLevel1

'' OK.
Print 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  
  '' dim shared as integer sharedFunctionLevel

  Function = 0
End Function


共同范围


所有模块都可以看到在公共范围内声明的变量。

使用Common声明的变量对于具有匹配的Common变量声明的其他模块是可见的。声明的变量名称必须与模块之间匹配。

module1.bas
'' compile with:
''    fbc -lang qb module1.bas module2.bas

'$lang: "qb"

Declare Sub Print_Values()
Common m1 As Integer
Common m2 As Integer
                      ' This is executed after all other modules
m1 = 1

Print "Module1"       
Print "m1 = "; m1     ' m1 = 1 as set in this module
Print "m2 = "; m2     ' m2 = 2 as set in module2

Print_Values


module2.bas
Common m1 As Integer
Common m2 As Integer

m2 = 2

Print "Module2"       ' This is executed first
Print "m1 = "; m1     ' m1 = 0 (by default)
Print "m2 = "; m2     ' m2 = 2

Sub Print_Values()
  Print "Module2.Print_Values"
  Print "m1 = "; m1   ' Implicit variable = 0    
  Print "m2 = "; m2   ' Implicit variable = 0  
End Sub


输出:
  Module2
  m1 = 0
  m2 = 2
  Module1
  m1 = 1
  m2 = 2
  Module2.Print_Values
  m1 = 0
  m2 = 0

公共共享范围


在公共共享范围中声明的变量对于所有模块和这些模块的所有功能都是可见的。

使用Common声明的变量对于具有匹配的Common变量声明的其他模块是可见的。声明的变量名称必须与模块之间匹配。在一个模块中,Shared声明修饰符给出了变量模块范围,并使该变量对所有子函数和函数都可见。

module3.bas
'' compile 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 in this module
Print "m2 = "; m2     '' m2 = 2 as set in module2

Print_Values


module4.bas
Common Shared m1 As Integer
Common Shared m2 As Integer

m2 = 2

Print "Module4"       '' This is executed first
Print "m1 = "; m1     '' m1 = 0 (by default)
Print "m2 = "; m2     '' m2 = 2

Sub Print_Values()
  Print "Module4.Print_Values"
  Print "m1 = "; m1   '' m1 = 1    
  Print "m2 = "; m2   '' m2 = 2
End Sub


Output:
  Module4
  m1 = 0
  m2 = 2
  Module3
  m1 = 1
  m2 = 2
  Module4.Print_Values
  m1 = 1
  m2 = 2

例子

见上面的例子。

参考