Difference between revisions of "Documentation/DevGuide/Basic/Variable Scopes"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
 
(3 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
  {{DISPLAYTITLE:Variable Scopes}}
 
  {{DISPLAYTITLE:Variable Scopes}}
 
Some aspects of scoping in Basic depend on the library structure. This section describes which variables declared in a Basic source code module are seen from what libraries or modules. Generally, only variables declared outside Subs are affected by this issue. Variables declared inside Subs are local to the Sub and not accessible from outside of the Sub. For example:  
 
Some aspects of scoping in Basic depend on the library structure. This section describes which variables declared in a Basic source code module are seen from what libraries or modules. Generally, only variables declared outside Subs are affected by this issue. Variables declared inside Subs are local to the Sub and not accessible from outside of the Sub. For example:  
 
+
<syntaxhighlight lang="oobas">
 
   Option Explicit      ' Forces declaration of variables
 
   Option Explicit      ' Forces declaration of variables
 
    
 
    
Line 20: Line 20:
 
       a% = 42          ' Runtime Error "Variable not defined"
 
       a% = 42          ' Runtime Error "Variable not defined"
 
   End Sub
 
   End Sub
 
+
</syntaxhighlight>
Variables can also be declared outside of Subs. Then their scope includes at least the module they are declared in. To declare variables outside of the Subs, the commands <code>Private</code>, <code>Public/Dim</code> and <code>Global</code> are used.
+
Variables can also be declared outside of Subs. Then their scope includes at least the module they are declared in. To declare variables outside the Subs, the commands <code>Private</code>, <code>Public/Dim</code> and <code>Global</code> are used.
  
 
The <code>Private</code> command is used to declare variables that can only be used locally in a module. If the same variable is declared as <code>Private</code> in two different modules, they are used independently in each module. For example:
 
The <code>Private</code> command is used to declare variables that can only be used locally in a module. If the same variable is declared as <code>Private</code> in two different modules, they are used independently in each module. For example:
  
 
Library Standard, Module1:
 
Library Standard, Module1:
 
+
<syntaxhighlight lang="oobas">
 
   Private x As Double
 
   Private x As Double
 
    
 
    
Line 36: Line 36:
 
       Module2_ShowX    ' Displays the x of Module2
 
       Module2_ShowX    ' Displays the x of Module2
 
   End Sub
 
   End Sub
 
+
</syntaxhighlight>
 
Library Standard, Module2:
 
Library Standard, Module2:
 
+
<syntaxhighlight lang="oobas">
 
   Private x As Double
 
   Private x As Double
 
    
 
    
Line 48: Line 48:
 
       MsgBox x        ' Displays the x of Module2
 
       MsgBox x        ' Displays the x of Module2
 
   End Sub
 
   End Sub
 
+
</syntaxhighlight>
 
When <code>Main</code> in Module1 is executed, <code>47.11</code> is displayed (<code>x</code> of Module1) and then <code>47.12</code> (<code>x</code> of Module2).
 
When <code>Main</code> in Module1 is executed, <code>47.11</code> is displayed (<code>x</code> of Module1) and then <code>47.12</code> (<code>x</code> of Module2).
  
 
The <code>Public</code> and <code>Dim</code> commands declare variables that can also be accessed from outside the module. They are identical in this context. Variables declared with <code>Public</code> and <code>Dim</code> can be accessed from all modules that belong to the same library container. For example, based on the library structure shown in Illustration 12.39: Sample module structure, any variable declared with <code>Public</code> and <code>Dim</code> in the Application Basic Modules Standard/Module1, Standard/Module2, Library1/Module1, Library1/Module2 can also be accessed from all of these modules, therefore the library container represents the logical root scope.
 
The <code>Public</code> and <code>Dim</code> commands declare variables that can also be accessed from outside the module. They are identical in this context. Variables declared with <code>Public</code> and <code>Dim</code> can be accessed from all modules that belong to the same library container. For example, based on the library structure shown in Illustration 12.39: Sample module structure, any variable declared with <code>Public</code> and <code>Dim</code> in the Application Basic Modules Standard/Module1, Standard/Module2, Library1/Module1, Library1/Module2 can also be accessed from all of these modules, therefore the library container represents the logical root scope.
 +
 +
 +
{{Warn|The names of the Basic modules (by default Module1, Module2, etc) are known by Basic on a public scope. You must avoid to have a variable or routine of public scope with the same name as one of the modules of the library.
 +
 +
Example : Suppose that in your Basic library you have a module named PrintDoc. You must not declare, in any module of your library, a Public variable or a Public constant, or a Sub, or a Function, named PrintDoc.
 +
 +
If you do have such collision, Basic may produce strange runtime error messages, or your Sub may not work.}}
  
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]
 
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]

Latest revision as of 21:16, 20 December 2020



Some aspects of scoping in Basic depend on the library structure. This section describes which variables declared in a Basic source code module are seen from what libraries or modules. Generally, only variables declared outside Subs are affected by this issue. Variables declared inside Subs are local to the Sub and not accessible from outside of the Sub. For example:

  Option Explicit      ' Forces declaration of variables
 
  Sub Main
      Dim a%
      a% = 42          ' Ok
      NotMain()
  End Sub
 
  Sub NotMain
      a% = 42          ' Runtime Error "Variable not defined"
  End Sub

Variables can also be declared outside of Subs. Then their scope includes at least the module they are declared in. To declare variables outside the Subs, the commands Private, Public/Dim and Global are used.

The Private command is used to declare variables that can only be used locally in a module. If the same variable is declared as Private in two different modules, they are used independently in each module. For example:

Library Standard, Module1:

  Private x As Double
 
  Sub Main
      x = 47.11        ' Initialize x of Module1
      Module2_InitX    ' Initialize x of Module2
 
      MsgBox x         ' Displays the x of Module1
      Module2_ShowX    ' Displays the x of Module2
  End Sub

Library Standard, Module2:

  Private x As Double
 
  Sub Module2_InitX
      x = 47.12        ' Initialize x of Module2
  End Sub
 
  Sub Module2_ShowX
      MsgBox x         ' Displays the x of Module2
  End Sub

When Main in Module1 is executed, 47.11 is displayed (x of Module1) and then 47.12 (x of Module2).

The Public and Dim commands declare variables that can also be accessed from outside the module. They are identical in this context. Variables declared with Public and Dim can be accessed from all modules that belong to the same library container. For example, based on the library structure shown in Illustration 12.39: Sample module structure, any variable declared with Public and Dim in the Application Basic Modules Standard/Module1, Standard/Module2, Library1/Module1, Library1/Module2 can also be accessed from all of these modules, therefore the library container represents the logical root scope.


Documentation caution.png The names of the Basic modules (by default Module1, Module2, etc) are known by Basic on a public scope. You must avoid to have a variable or routine of public scope with the same name as one of the modules of the library.

Example : Suppose that in your Basic library you have a module named PrintDoc. You must not declare, in any module of your library, a Public variable or a Public constant, or a Sub, or a Function, named PrintDoc.

If you do have such collision, Basic may produce strange runtime error messages, or your Sub may not work.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages