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

From Apache OpenOffice Wiki
Jump to: navigation, search
(FINAL VERSION FOR L10N)
m
 
(6 intermediate revisions by 3 users not shown)
Line 10: Line 10:
 
==Implicit Variable Declaration ==
 
==Implicit Variable Declaration ==
  
Basic languages are designed to be easy to use. As a result, {{OOo}} 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:
+
Basic languages are designed to be easy to use. As a result, {{AOo}} 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
 
  a = b + c
Line 18: Line 18:
 
==Explicit Variable Declaration ==
 
==Explicit Variable Declaration ==
  
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, {{AOo}} Basic provides a switch called:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Option Explicit
 
Option Explicit
</source>
+
</syntaxhighlight>
  
 
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 28: 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:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim MyVar
 
Dim MyVar
</source>
+
</syntaxhighlight>
  
 
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:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
MyVar = "Hello World"      ' Assignment of a string
 
MyVar = "Hello World"      ' Assignment of a string
 
MyVar = 1                  ' Assignment of a whole number
 
MyVar = 1                  ' Assignment of a whole number
 
MyVar = 1.0                ' Assignment of a floating point number
 
MyVar = 1.0                ' Assignment of a floating point number
 
MyVar = True              ' Assignment of a Boolean value
 
MyVar = True              ' Assignment of a Boolean value
</source>
+
</syntaxhighlight>
  
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 {{AOo}} 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:
 
Use the following style when you make a type-bound variable declaration:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim MyVar As Integer  ' Declaration of a variable of the integer type
 
Dim MyVar As Integer  ' Declaration of a variable of the integer type
</source>
+
</syntaxhighlight>
  
 
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:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim MyVar%          ' Declaration of a variable of the integer type
 
Dim MyVar%          ' Declaration of a variable of the integer type
</source>
+
</syntaxhighlight>
  
 
The Dim instruction can record several variable declarations:
 
The Dim instruction can record several variable declarations:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim MyVar1, MyVar2
 
Dim MyVar1, MyVar2
</source>
+
</syntaxhighlight>
  
 
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:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim MyVar1 As Integer, MyVar2 As Integer
 
Dim MyVar1 As Integer, MyVar2 As Integer
</source>
+
</syntaxhighlight>
  
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, {{AOo}} 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:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim MyVar1, MyVar2 As Integer
 
Dim MyVar1, MyVar2 As Integer
</source>
+
</syntaxhighlight>
 
+
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 {{AOo}} Basic and describe how they can be used and declared.
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Working With Variables}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 10:58, 30 January 2021


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