Bereik en levensduur van variabelen
Een variabele in Apache OpenOffice BASIC heeft een beperkte levensduur en een beperkt bereik met betrekking tot waaruit hij kan worden gelezen en gebruikt in andere programma-onderdelen. De tijd dat een variabele wordt behouden, evenals van waar hij kan worden benaderd, is afhankelijk van zijn gespecificeerde lokatie en type.
Lokale variabelen
Variabelen die zijn gedeclareerd in een functie of een procedure worden lokale variabelen genoemd:
Sub Test Dim MyInteger As Integer ' ... End Sub
Lokale variabelen zijn slechts geldig zo lang als de functie of procedure wordt uitgevoerd, en worden dan opnieuw ingesteld op nul. Elke keer als de functie wordt aangeroepen zijn de eerder gegenereerde waarden niet meer beschikbaar.
Om de voorgaande waarden te behouden moet u de variabele definiëren als Static:
Sub Test Static MyInteger As Integer ' ... End Sub
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). |