21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Global Variables<br />

Scope and Life Span of Variables<br />

In terms of their function, global variables are similar to public domain variables, except that their values are<br />

retained even after the associated macro has executed. Global variables are declared in the header section of a<br />

module using the keyword Global:<br />

Global A As Integer<br />

Private Variables<br />

Private variables are only available in the module in which they are defined. Use the keyword Private to<br />

define the variable:<br />

Private MyInteger As Integer<br />

If several modules contain a Private variable with the same name, <strong>OpenOffice</strong>.<strong>org</strong> Basic creates a different<br />

variable for each occurrence of the name. In the following example, both module A and B have a Private<br />

variable called C. The Test function first sets the Private variable in module A and then the Private variable in<br />

module B.<br />

Module A:<br />

Private C As Integer<br />

Sub Test<br />

SetModuleA ' Sets the variable C from module A<br />

SetModuleB ' Sets the variable C from module B<br />

ShowVarA ' Shows the variable C from module A (= 10)<br />

ShowVarB ' Shows the variable C from module B (= 20)<br />

End Sub<br />

Sub SetModuleA<br />

C = 10<br />

End Sub<br />

Sub ShowVarA<br />

MsgBox C ' Shows the variable C from module A.<br />

End Sub<br />

Module B:<br />

Private C As Integer<br />

Sub SetModuleB<br />

C = 20<br />

End Sub<br />

Sub ShowVarB<br />

MsgBox C ' Shows the variable C from module B.<br />

End Sub<br />

Keep in mind that ShowVarB only shows the expected value of C (20) because Sub Test is keeping it in scope. If<br />

the calls to SetModuleB and ShowVarB are independent, e.g. SetModuleB is triggered from one toolbar button<br />

and ShowVarB is triggered from another toolbar button, then ShowVarB will display a C value of 0 since module<br />

variables are reset after each macro completion.<br />

Constants<br />

Constants are values which may be used but not changed by the program.<br />

Defining Constants<br />

In <strong>OpenOffice</strong>.<strong>org</strong> Basic, use the keyword Const to declare a constant.<br />

Chapter 2 · The Language of <strong>OpenOffice</strong>.<strong>org</strong> <strong>BASIC</strong> 21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!