Scope and Life Span of Variables

From Wiki
Revision as of 14:18, 1 September 2011 by Sancho (talk | contribs) (Created page with "<section begin=title /><noinclude>{{DISPLAYTITLE:</noinclude>Scope and Life Span of Variables<noinclude>}}</noinclude><section end=title />{{BreadCrumbL10n}} {{Documentation/BA...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search





A variable in OpenOffice.org 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, OpenOffice.org 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 OpenOffice.org 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, OpenOffice.org 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).