OpenOffice.org BASIC Guide - OpenOffice.org wiki
OpenOffice.org BASIC Guide - OpenOffice.org wiki
OpenOffice.org BASIC Guide - OpenOffice.org wiki
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Scope and Life Span of Variables<br />
Scope and Life Span of Variables<br />
A variable in <strong>OpenOffice</strong>.<strong>org</strong> Basic has a limited life span and a limited scope from which it can be read and used<br />
in other program fragments. The amount of time that a variable is retained, as well as where it can be accessed<br />
from, depends on its specified location and type.<br />
Local Variables<br />
Variables that are declared in a function or a procedure are called local variables:<br />
Sub Test<br />
Dim MyInteger As Integer<br />
' ...<br />
End Sub<br />
Local variables only remain valid as long as the function or the procedure is executing, and then are reset to zero.<br />
Each time the function is called, the values generated previously are not available.<br />
To keep the previous values, you must define the variable as Static:<br />
Sub Test<br />
Static MyInteger As Integer<br />
' ...<br />
End Sub<br />
Note – VBA : Unlike VBA, <strong>OpenOffice</strong>.<strong>org</strong> Basic ensures that the name of a local variable is not used<br />
simultaneously as a global and a private variable in the module header. When you port a VBA application to<br />
<strong>OpenOffice</strong>.<strong>org</strong> Basic, you must change any duplicate variable names.<br />
Public Domain Variables<br />
Public domain variables are defined in the header section of a module by the keyword Dim. These variables are<br />
available to all of the modules in their library:<br />
Module A:<br />
Dim A As Integer<br />
Sub Test<br />
Flip<br />
Flop<br />
End Sub<br />
Sub Flip<br />
A = A + 1<br />
End Sub<br />
Module B:<br />
Sub Flop<br />
A = A - 1<br />
End Sub<br />
The value of variable A is not changed by the Test function, but is increased by one in the Flip function and<br />
decreased by one in the Flop function. Both of these changes to the variable are global.<br />
You can also use the keyword Public instead of Dim to declare a public domain variable:<br />
Public A As Integer<br />
A public domain variable is only available so long as the associated macro is executing and then the variable is<br />
reset.<br />
20 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010