Difference between revisions of "Documentation/BASIC Guide/Scope of Variables"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Local Variables)
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:Scope and Life Span of Variables}}
 
 
{{Documentation/BASICGuideTOC/v2
 
{{Documentation/BASICGuideTOC/v2
 
|ShowPrevNext=block
 
|ShowPrevNext=block
Line 7: Line 6:
 
|lang=block
 
|lang=block
 
}}
 
}}
 
+
{{DISPLAYTITLE:Scope and Life Span of Variables}}
 +
__NOTOC__
 
A variable in {{OOo}} Basic has a limited life span and a limited scope from which it can be read and used in other program fragments. The amount of time that a variable is retained, as well as where it can be accessed from, depends on its specified location and type.
 
A variable in {{OOo}} Basic has a limited life span and a limited scope from which it can be read and used in other program fragments. The amount of time that a variable is retained, as well as where it can be accessed from, depends on its specified location and type.
  
Line 14: Line 14:
 
Variables that are declared in a function or a procedure are called local variables:
 
Variables that are declared in a function or a procedure are called local variables:
  
Sub Test
+
<source lang="oobas">
  Dim MyInteger As Integer
+
Sub Test
  ' ...
+
  Dim MyInteger As Integer
End Sub
+
  ' ...
 +
End Sub
 +
</source>
  
 
Local variables only remain valid as long as the function or the procedure is executing, and then are reset to zero. Each time the function is called, the values generated previously are not available.
 
Local variables only remain valid as long as the function or the procedure is executing, and then are reset to zero. Each time the function is called, the values generated previously are not available.
Line 23: Line 25:
 
To keep the previous values, you must define the variable as <tt>Static</tt>:
 
To keep the previous values, you must define the variable as <tt>Static</tt>:
  
Sub Test
+
<source lang="oobas">
  Static MyInteger As Integer
+
Sub Test
  ' ...
+
  Static MyInteger As Integer
End Sub
+
  ' ...
 +
End Sub
 +
</source>
  
{{Documentation/Note|Unlike VBA, {{OOo}} Basic ensures that the name of a local variable is not used simultaneously as a global and a private variable in the module header. When you port a VBA application to {{OOo}} Basic, you must change any duplicate variable names.}}
+
{{Documentation/VBAnote|Unlike VBA, {{OOo}} Basic ensures that the name of a local variable is not used simultaneously as a global and a private variable in the module header. When you port a VBA application to {{OOo}} Basic, you must change any duplicate variable names.}}
  
 
== Public Domain Variables ==
 
== Public Domain Variables ==
Line 35: Line 39:
  
 
Module A:
 
Module A:
Dim A As Integer
+
 
Sub Test
+
<source lang="oobas">
  Flip
+
Dim A As Integer
  Flop
+
Sub Test
End Sub
+
  Flip
+
  Flop
Sub Flip
+
End Sub
  A = A + 1
+
 
End Sub
+
Sub Flip
 +
A = A + 1
 +
End Sub
 +
</source>
  
 
Module B:
 
Module B:
Sub Flop
+
 
  A = A - 1
+
<source lang="oobas">
End Sub
+
Sub Flop
 +
  A = A - 1
 +
End Sub
 +
</source>
  
 
The value of variable <tt>A</tt> is not changed by the <tt>Test</tt> function, but is increased by one in the <tt>Flip</tt> function and decreased by one in the <tt>Flop</tt> function. Both of these changes to the variable are global.
 
The value of variable <tt>A</tt> is not changed by the <tt>Test</tt> function, but is increased by one in the <tt>Flip</tt> function and decreased by one in the <tt>Flop</tt> function. Both of these changes to the variable are global.
Line 54: Line 64:
 
You can also use the keyword <tt>Public</tt> instead of <tt>Dim</tt> to declare a public domain variable:
 
You can also use the keyword <tt>Public</tt> instead of <tt>Dim</tt> to declare a public domain variable:
  
Public A As Integer
+
<source lang="oobas">
 +
Public A As Integer
 +
</source>
  
 
A public domain variable is only available so long as the associated macro is executing and then the variable is reset.
 
A public domain variable is only available so long as the associated macro is executing and then the variable is reset.
Line 62: Line 74:
 
In terms of their function, global variables are similar to public domain variables, except that their values are retained even after the associated macro has executed. Global variables are declared in the header section of a module using the keyword <tt>Global</tt>:  
 
In terms of their function, global variables are similar to public domain variables, except that their values are retained even after the associated macro has executed. Global variables are declared in the header section of a module using the keyword <tt>Global</tt>:  
  
Global A As Integer
+
<source lang="oobas">
 +
Global A As Integer
 +
</source>
  
 
== Private Variables ==
 
== Private Variables ==
Line 68: Line 82:
 
<tt>Private</tt> variables are only available in the module in which they are defined. Use the keyword <tt>Private</tt> to define the variable:
 
<tt>Private</tt> variables are only available in the module in which they are defined. Use the keyword <tt>Private</tt> to define the variable:
  
Private MyInteger As Integer
+
<source lang="oobas">
 +
Private MyInteger As Integer
 +
</source>
  
 
If several modules contain a <tt>Private</tt> variable with the same name, {{OOo}} Basic creates a different variable for each occurrence of the name. In the following example, both module <tt>A</tt> and <tt>B</tt> have a <tt>Private</tt> variable called <tt>C</tt>. The <tt>Test</tt> function first sets the <tt>Private</tt> variable in module <tt>A</tt> and then the <tt>Private</tt> variable in module <tt>B</tt>.
 
If several modules contain a <tt>Private</tt> variable with the same name, {{OOo}} Basic creates a different variable for each occurrence of the name. In the following example, both module <tt>A</tt> and <tt>B</tt> have a <tt>Private</tt> variable called <tt>C</tt>. The <tt>Test</tt> function first sets the <tt>Private</tt> variable in module <tt>A</tt> and then the <tt>Private</tt> variable in module <tt>B</tt>.
  
 
Module A:
 
Module A:
Private C As Integer
 
 
Sub Test
 
  SetModuleA      ' Sets the variable C from module A
 
  SetModuleB      ' Sets the variable C from module B
 
  ShowVarA        ' Shows the variable C from module A (= 10)
 
  ShowVarB        ' Shows the variable C from module B (= 20)
 
End Sub
 
 
Sub SetmoduleeA
 
  C = 10
 
End Sub
 
 
Sub ShowVarA
 
  MsgBox C        ' Shows the variable C from module A.
 
End Sub
 
  
Module B:
+
<source lang="oobas">
Private C As Integer
+
Private C As Integer
 
+
 
Sub SetModuleB
+
Sub Test
  C = 20
+
  SetModuleA      ' Sets the variable C from module A
End Sub
+
  SetModuleB     ' Sets the variable C from module B
 +
  ShowVarA        ' Shows the variable C from module A (= 10)
 +
  ShowVarB        ' Shows the variable C from module B (= 20)
 +
End Sub
 +
 
 +
Sub SetModuleA
 +
  C = 10
 +
End Sub
 +
 
 +
Sub ShowVarA
 +
  MsgBox C        ' Shows the variable C from module A.
 +
End Sub
 +
</source>
 +
 
 +
Module B:
 +
 
 +
<source lang="oobas">
 +
Private C As Integer
 
   
 
   
Sub ShowVarB
+
Sub SetModuleB
  MsgBox C       ' Shows the variable C from module B.
+
  C = 20
End Sub
+
End Sub
  
 +
Sub ShowVarB
 +
  MsgBox C        ' Shows the variable C from module B.
 +
End Sub
 +
</source>
 +
 +
Keep in mind that ShowVarB only shows the expected value of C (20) because Sub Test is keeping it in scope. If the calls to SetModuleB and ShowVarB are independent, e.g. SetModuleB is triggered from one toolbar button and ShowVarB is triggered from another toolbar button, then ShowVarB will display a C value of 0 since module variables are reset after each macro completion.
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Scope of Variables}}
 
{{PDL1}}
 
{{PDL1}}

Revision as of 08:26, 22 October 2009


A variable in Apache OpenOffice Basic has a limited life span and a limited scope from which it can be read and used in other program fragments. The amount of time that a variable is retained, as well as where it can be accessed from, depends on its specified location and type.

Local Variables

Variables that are declared in a function or a procedure are called local variables:

Sub Test
  Dim MyInteger As Integer
  ' ...
End Sub

Local variables only remain valid as long as the function or the procedure is executing, and then are reset to zero. Each time the function is called, the values generated previously are not available.

To keep the previous values, you must define the variable as Static:

Sub Test
  Static MyInteger As Integer
  ' ...
End Sub
Documentation note.png VBA : Unlike VBA, Apache OpenOffice Basic ensures that the name of a local variable is not used simultaneously as a global and a private variable in the module header. When you port a VBA application to Apache OpenOffice Basic, you must change any duplicate variable names.


Public Domain Variables

Public domain variables are defined in the header section of a module by the keyword Dim. These variables are available to all of the modules in their library:

Module A:

Dim A As Integer
Sub Test
  Flip
  Flop
End Sub
 
Sub Flip
 A = A + 1
End Sub

Module B:

Sub Flop
  A = A - 1
End Sub

The value of variable A is not changed by the Test function, but is increased by one in the Flip function and decreased by one in the Flop function. Both of these changes to the variable are global.

You can also use the keyword Public instead of Dim to declare a public domain variable:

Public A As Integer

A public domain variable is only available so long as the associated macro is executing and then the variable is reset.

Global Variables

In terms of their function, global variables are similar to public domain variables, except that their values are retained even after the associated macro has executed. Global variables are declared in the header section of a module using the keyword Global:

Global A As Integer

Private Variables

Private variables are only available in the module in which they are defined. Use the keyword Private to define the variable:

Private MyInteger As Integer

If several modules contain a Private variable with the same name, Apache OpenOffice Basic creates a different variable for each occurrence of the name. In the following example, both module A and B have a Private variable called C. The Test function first sets the Private variable in module A and then the Private variable in module B.

Module A:

Private C As Integer
 
Sub Test
  SetModuleA      ' Sets the variable C from module A
  SetModuleB      ' Sets the variable C from module B
  ShowVarA        ' Shows the variable C from module A (= 10)
  ShowVarB        ' Shows the variable C from module B (= 20)
End Sub
 
Sub SetModuleA
  C = 10
End Sub
 
Sub ShowVarA
  MsgBox C        ' Shows the variable C from module A. 
End Sub

Module B:

 
Private C As Integer
 
Sub SetModuleB
  C = 20
End Sub
 
Sub ShowVarB
  MsgBox C        ' Shows the variable C from module B.
End Sub

Keep in mind that ShowVarB only shows the expected value of C (20) because Sub Test is keeping it in scope. If the calls to SetModuleB and ShowVarB are independent, e.g. SetModuleB is triggered from one toolbar button and ShowVarB is triggered from another toolbar button, then ShowVarB will display a C value of 0 since module variables are reset after each macro completion.


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