21.08.2013 Views

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Working With Variables 8<br />

Working With Variables<br />

Implicit Variable Declaration<br />

Basic languages are designed to be easy to use. As a result, <strong>OpenOffice</strong>.<strong>org</strong> Basic enables<br />

the creation of a variable through simple usage and without an explicit declaration. In other<br />

words, a variable exists from the moment that you include it in your code. Depending on the<br />

variables that are already present, the following example declares up to three new<br />

variables:<br />

a = b + c<br />

Declaring variables implicitly is not good programming practice because it can result in the<br />

inadvertent introduction of a new variable through, for example, a typing error. Instead of<br />

producing an error message, the interpreter initializes the typing error as a new variable<br />

with a value of 0. It can be very difficult to locate errors of this kind in your code.<br />

Explicit Variable Declaration<br />

To prevent errors caused by an implicit declaration of variables, <strong>OpenOffice</strong>.<strong>org</strong> Basic<br />

provides a switch called:<br />

Option Explicit<br />

This must be listed in the first program line of each module and ensures that an error<br />

message is issued if one of the variables used is not declared. The Option Explicit switch<br />

should be included in all Basic modules.<br />

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

Dim MyVar<br />

This example declares a variable with the name MyVar and the type variant. A variant is a<br />

universal variable that can record all conceivable values, including strings, whole numbers,<br />

floating point figures, and Boolean values. Here are a few examples of Variant variables:<br />

MyVar = "Hello World" ' Assignment of a string<br />

MyVar = 1 ' Assignment of a whole number<br />

MyVar = 1.0 ' Assignment of a floating point number<br />

MyVar = True ' Assignment of a Boolean value<br />

The variables declared in the previous example can even be used for different variable<br />

types in the same program. Although this provides considerable flexibility, it is best to<br />

restrict a variable to one variable type. When <strong>OpenOffice</strong>.<strong>org</strong> Basic encounters an

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

Saved successfully!

Ooh no, something went wrong!