Werken met variabelen

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 14:33, 20 January 2013 by DiGro (talk | contribs) (Created page with "{{DISPLAYTITLE:Werken met variabelen}} {{NL/Documentation/BASICGuideTOC/v2 |ShowPrevNext=block |ShowPrevPage=block |PrevPage=NL/Documentation/BASIC Guide/Program Overview |Nex...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Book.png

Impliciete declaratie van variabelen

BASIC-talen zijn ontworpen om makkelijk te kunnen gebruiken. Als een resultaat daarvan maakt Apache OpenOffice BASIC het maken van een variabele door simpel gebruik mogelijk, zonder een expliciete declaratie. Met andere woorden: een variabele bestaat vanaf het moment dat u hem opneemt in uw code. Afhankelijk van de variabelen die reeds aanwezig zijn, declareert het volgende voorbeeld maximaal drie nieuwe variabelen:

a = b + c

Impliciet declareren van variabelen is geen goede manier van programmeren omdat het kan resulteren in de onbedoelde introductie van een nieuwe variabele door, bijvoorbeeld, een typefout. In plaats van het produceren van een foutbericht initialiseert de interpreter de typefout als een nieuwe variabele met de waarde 0. Het kan zeer moeilijk zijn dit type fouten te lokaliseren in uw code.

Expliciete declaratie van variabelen

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