Working with Variables

From Apache OpenOffice Wiki
Jump to: navigation, search


Implicit Variable Declaration

Basic languages are designed to be easy to use. As a result, Apache OpenOffice Basic enables the creation of a variable through simple usage and without an explicit declaration. In other words, a variable exists from the moment that you include it in your code. Depending on the variables that are already present, the following example declares up to three new variables:

a = b + c

Declaring variables implicitly is not good programming practice because it can result in the inadvertent introduction of a new variable through, for example, a typing error. Instead of producing an error message, the interpreter initializes the typing error as a new variable with a value of 0. It can be very difficult to locate errors of this kind in your code.

Explicit Variable Declaration

To prevent errors caused by an implicit declaration of variables, Apache OpenOffice Basic provides a switch called:

Option Explicit

This must be listed in the first program line of each module and ensures that an error message is issued if one of the variables used is not declared. The Option Explicit switch should be included in all Basic modules.

In its simplest form, the command for an explicit declaration of a variable is as follows:

Dim MyVar

This example declares a variable with the name MyVar and the type variant. A variant is a universal variable that can record all conceivable values, including strings, whole numbers, floating point figures, and Boolean values. Here are a few examples of Variant variables:

MyVar = "Hello World"      ' Assignment of a string
MyVar = 1                  ' Assignment of a whole number
MyVar = 1.0                ' Assignment of a floating point number
MyVar = True               ' Assignment of a Boolean value

The variables declared in the previous example can even be used for different variable types in the same program. Although this provides considerable flexibility, it is best to restrict a variable to one variable type. When Apache OpenOffice Basic encounters an incorrectly defined variable type in a particular context, an error message is generated.

Use the following style when you make a type-bound variable declaration:

Dim MyVar As Integer   ' Declaration of a variable of the integer type

The variable is declared as an integer type and can record whole number values. You can also use the following style to declare an integer type variable:

Dim MyVar%          ' Declaration of a variable of the integer type

The Dim instruction can record several variable declarations:

Dim MyVar1, MyVar2

If you want to assign the variables to a permanent type, you must make separate assignments for each variable:

Dim MyVar1 As Integer, MyVar2 As Integer

If you do not declare the type for a variable, Apache OpenOffice Basic assigns the variable a variant type. For example, in the following variable declaration, MyVar1 becomes a variant and MyVar2 becomes an integer:

Dim MyVar1, MyVar2 As Integer

The following sections list the variable types that are available in Apache OpenOffice Basic and describe how they can be used and declared.


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