Difference between revisions of "Documentation/BASIC Guide/Working With Variables"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 20: Line 20:
 
To prevent errors caused by an implicit declaration of variables, {{OOo}} Basic provides a switch called:
 
To prevent errors caused by an implicit declaration of variables, {{OOo}} Basic provides a switch called:
  
Option Explicit
+
<source lang="oobas">
 +
Option Explicit
 +
</source>
  
 
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 <tt>Option Explicit</tt> switch should be included in all Basic modules.
 
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 <tt>Option Explicit</tt> switch should be included in all Basic modules.
Line 26: Line 28:
 
In its simplest form, the command for an explicit declaration of a variable is as follows:
 
In its simplest form, the command for an explicit declaration of a variable is as follows:
  
Dim MyVar
+
<source lang="oobas">
 +
Dim MyVar
 +
</source>
  
 
This example declares a variable with the name <tt>MyVar</tt> and the type <tt>variant</tt>. 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:
 
This example declares a variable with the name <tt>MyVar</tt> and the type <tt>variant</tt>. 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
+
<source lang="oobas">
MyVar = 1                  ' Assignment of a whole number
+
MyVar = "Hello World"      ' Assignment of a string
MyVar = 1.0                ' Assignment of a floating point number
+
MyVar = 1                  ' Assignment of a whole number
MyVar = True              ' Assignment of a Boolean value
+
MyVar = 1.0                ' Assignment of a floating point number
 +
MyVar = True              ' Assignment of a Boolean value
 +
</source>
  
 
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 {{OOo}} Basic encounters an incorrectly defined variable type in a particular context, an error message is generated.
 
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 {{OOo}} Basic encounters an incorrectly defined variable type in a particular context, an error message is generated.
Line 39: Line 45:
 
Use the following style when you make a type-bound variable declaration:
 
Use the following style when you make a type-bound variable declaration:
  
Dim MyVar As Integer  ' Declaration of a variable of the integer type
+
<source lang="oobas">
 +
Dim MyVar As Integer  ' Declaration of a variable of the integer type
 +
</source>
  
 
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:
 
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
+
<source lang="oobas">
 +
Dim MyVar%          ' Declaration of a variable of the integer type
 +
</source>
  
 
The Dim instruction can record several variable declarations:
 
The Dim instruction can record several variable declarations:
  
Dim MyVar1, MyVar2
+
<source lang="oobas">
 +
Dim MyVar1, MyVar2
 +
</source>
  
 
If you want to assign the variables to a permanent type, you must make separate assignments for each variable:
 
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
+
<source lang="oobas">
 +
Dim MyVar1 As Integer, MyVar2 As Integer
 +
</source>
  
 
If you do not declare the type for a variable, {{OOo}} Basic assigns the variable a variant type. For example, in the following variable declaration, <tt>MyVar1</tt> becomes a variant and <tt>MyVar2</tt> becomes an integer:
 
If you do not declare the type for a variable, {{OOo}} Basic assigns the variable a variant type. For example, in the following variable declaration, <tt>MyVar1</tt> becomes a variant and <tt>MyVar2</tt> becomes an integer:
  
Dim MyVar1, MyVar2 As Integer
+
<source lang="oobas">
 +
Dim MyVar1, MyVar2 As Integer
 +
</source>
  
 
The following sections list the variable types that are available in {{OOo}} Basic and describe how they can be used and declared.
 
The following sections list the variable types that are available in {{OOo}} Basic and describe how they can be used and declared.
  
 
{{PDL1}}
 
{{PDL1}}

Revision as of 12:54, 2 April 2008


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