Difference between revisions of "Documentation/BASIC Guide/Program Overview"

From Apache OpenOffice Wiki
Jump to: navigation, search
(25 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{DISPLAYTITLE:An Overview of an {{OOo}} Basic Program}}
 
{{Documentation/BASICGuideTOC/v2
 
{{Documentation/BASICGuideTOC/v2
 
|ShowPrevNext=block
 
|ShowPrevNext=block
 
|ShowPrevPage=block
 
|ShowPrevPage=block
 
|PrevPage=Documentation/BASIC Guide/Language
 
|PrevPage=Documentation/BASIC Guide/Language
|NextPage=Documentation/BASIC Guide/
+
|NextPage=Documentation/BASIC Guide/Working With Variables
 
|lang=block
 
|lang=block
}}<!-- {{DISPLAYTITLE:{{OOo}} BASIC Programming Guide}} -->
+
}}
 
+
== An Overview of a {{OOo}} Basic Program ==
+
{{OOo}} Basic is an interpreter language. Unlike C++ or Delphi, the {{OOo}} Basic compiler does not create executable or self-extracting files that are capable of running automatically. Instead, you execute an {{OOo}} Basic program inside {{OOo}}. The code is first checked for obvious errors and then executed line by line.
{{OOo}} Basic is an interpreter language. Unlike C++ or Turbo Pascal, the {{OOo}} compiler does not create executable or self-extracting files that are capable of running automatically. Instead, you can execute a {{OOo}} Basic program by pressing a button. The code is first checked for obvious errors and then executed line by line.
+
  
 
=== Program Lines ===
 
=== Program Lines ===
Line 20: Line 20:
 
  (Expression7 * Expression8)
 
  (Expression7 * Expression8)
  
{{Documentation/Note|The underscore must always be the last character in a linked line and cannot be followed by a space or a tab, otherwise the code generates an error.}}
+
{{Note|The underscore must always be the last character in a linked line and cannot be followed by a space or a tab or a comment, otherwise the code generates an error.
 +
}}
  
In addition to linking individual lines, {{OOo}} Basic, you can use colons to divide one line into several sections so that there is enough space for several expressions. The assignments
+
In addition to linking individual lines, in {{OOo}} Basic you can use colons to divide one line into several sections, so that there is enough space for several expressions. The assignments
  
 
  a = 1  
 
  a = 1  
Line 34: Line 35:
 
=== Comments ===
 
=== Comments ===
  
In addition to the program code to be executed, a {{OOo}} Basic program can also contain comments that explain the individual parts of the program and provide important information that can be helpful at a later point.
+
In addition to the program code to be executed, an {{OOo}} Basic program can also contain comments that explain the individual parts of the program and provide important information that can be helpful at a later point.
  
 
{{OOo}} Basic provides two methods for inserting comments in the program code:
 
{{OOo}} Basic provides two methods for inserting comments in the program code:
  
* All characters that follow an apostrophe are treated as comments:
+
<ul>
 +
<li>All characters that follow an apostrophe are treated as comments:
  
Dim A    ' This is a comment for variable A
+
<source lang="oobas">
+
Dim A    ' This is a comment for variable A
* The keyword <tt>Rem</tt>, followed by the comment:
+
</source></li>
 +
<li>The keyword <tt>Rem</tt>, followed by the comment:
  
Rem This comment is introduced by the keyword Rem.  
+
<source lang="oobas">
 +
Rem This comment is introduced by the keyword Rem.  
 +
</source></li></ul>
  
 
A comment usually includes all characters up to the end of the line. {{OOo}} Basic then interprets the following line as a regular instruction again. If comments cover several lines, each line must be identified as a comment:
 
A comment usually includes all characters up to the end of the line. {{OOo}} Basic then interprets the following line as a regular instruction again. If comments cover several lines, each line must be identified as a comment:
  
Dim B    ' This comment for variable B is relatively long
+
<source lang="oobas">
          ' and stretches over several lines. The
+
Dim B    ' This comment for variable B is relatively long
          ' comment character must therefore be repeated  
+
          ' and stretches over several lines. The
          ' in each line.
+
          ' comment character must therefore be repeated  
 +
          ' in each line.
 +
</source>
  
 
=== Markers ===
 
=== Markers ===
Line 63: Line 70:
 
* No distinction is made between uppercase and lowercase characters. The <tt>OneTestVariable</tt> marker, for example, defines the same variable as <tt>onetestVariable</tt> and <tt>ONETESTVARIABLE</tt>.<br/> There is, however, one exception to this rule: a distinction is made between uppercase and lowercase characters for UNO-API constants. More information about UNO is presented in [[Documentation/BASIC Guide/API Intro|Introduction to the {{OOo}} API]].
 
* No distinction is made between uppercase and lowercase characters. The <tt>OneTestVariable</tt> marker, for example, defines the same variable as <tt>onetestVariable</tt> and <tt>ONETESTVARIABLE</tt>.<br/> There is, however, one exception to this rule: a distinction is made between uppercase and lowercase characters for UNO-API constants. More information about UNO is presented in [[Documentation/BASIC Guide/API Intro|Introduction to the {{OOo}} API]].
  
{{Documentation/Note|The rules for constructing markers are different in {{OOo}} Basic than in VBA. For example, {{OOo}} Basic only allows special characters in markers when using Option Compatible, since they can cause problems in international projects.}}
+
 
 +
{{Warn|The names of the Basic modules (by default Module1, Module2, etc) are known by Basic on a public [[Documentation/BASIC_Guide/Scope_of_Variables|scope]]. You must avoid to have a marker of public [[Documentation/BASIC_Guide/Scope_of_Variables|scope]] with the same name as one of the modules of the library.
 +
 
 +
Example : Suppose that in your Basic library you have a module named PrintDoc. You must not declare, in any module of your library, a Public variable or a Public constant, or a Sub, or a Function, named PrintDoc.
 +
 
 +
If you do have such collision, Basic may produce strange runtime error messages, or your Sub may not work.}}
 +
 
 +
{{Documentation/VBAnote|The rules for constructing markers are different in {{OOo}} Basic than in VBA. For example, {{OOo}} Basic only allows special characters in markers when using <tt>Option Compatible</tt>, since they can cause problems in international projects.}}
  
 
Here are a few examples of correct and incorrect markers:
 
Here are a few examples of correct and incorrect markers:
  
Surname      ' Correct  
+
<source lang="oobas">
Surname5    ' Correct (number 5 is not the first digit)
+
Surname      ' Correct  
First Name  ' Incorrect (spaces are not permitted)
+
Surname5    ' Correct (number 5 is not the first digit)
DéjàVu      ' Incorrect (letters such as é, à are not permitted)
+
First Name  ' Incorrect (spaces are not permitted)
5Surnames    ' Incorrect (the first character must not be a number)
+
DéjàVu      ' Incorrect (letters such as é, à are not permitted)
First,Name  ' Incorrect (commas and full stops are not permitted)
+
5Surnames    ' Incorrect (the first character must not be a number)
 +
First,Name  ' Incorrect (commas and full stops are not permitted)
 +
</source>
 +
 
 +
Enclosing a variable name in square brackets allows names that might otherwise be disallowed; for example, spaces.
 +
 
 +
<source lang="oobas">
 +
  Dim [First Name] As String  'Space accepted in square brackets
 +
  Dim [DéjàVu] As Integer    'Special characters in square brackets
 +
  [First Name] = "Andrew"
 +
  [DéjàVu] = 2
 +
</source>
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Program Overview}}
 +
{{PDL1}}

Revision as of 07:17, 12 July 2018


Apache OpenOffice Basic is an interpreter language. Unlike C++ or Delphi, the Apache OpenOffice Basic compiler does not create executable or self-extracting files that are capable of running automatically. Instead, you execute an Apache OpenOffice Basic program inside Apache OpenOffice. The code is first checked for obvious errors and then executed line by line.

Program Lines

The Basic interpreter's line-oriented execution produces one of the key differences between Basic and other programming languages. Whereas the position of hard line breaks in the source code of Java, C++, or Delphi programs is irrelevant, each line in a Basic program forms a self-contained unit. Function calls, mathematical expressions, and other linguistic elements, such as function and loop headers, must be completed on the same line that they begin on.

If there is not enough space, or if this results in long lines, then several lines can be linked together by adding underscores _. The following example shows how four lines of a mathematical expression can be linked:

LongExpression = (Expression1 * Expression2) + _
(Expression3 * Expression4) + _ 
(Expression5 * Expression6) + _
(Expression7 * Expression8)
Documentation note.png The underscore must always be the last character in a linked line and cannot be followed by a space or a tab or a comment, otherwise the code generates an error.

In addition to linking individual lines, in Apache OpenOffice Basic you can use colons to divide one line into several sections, so that there is enough space for several expressions. The assignments

a = 1 
a = a + 1 
a = a + 1 

can be written as follows:

a = 1  :  a = a + 1  :  a = a + 1

Comments

In addition to the program code to be executed, an Apache OpenOffice Basic program can also contain comments that explain the individual parts of the program and provide important information that can be helpful at a later point.

Apache OpenOffice Basic provides two methods for inserting comments in the program code:

  • All characters that follow an apostrophe are treated as comments:
    Dim A    ' This is a comment for variable A
  • The keyword Rem, followed by the comment:
    Rem This comment is introduced by the keyword Rem.

A comment usually includes all characters up to the end of the line. Apache OpenOffice Basic then interprets the following line as a regular instruction again. If comments cover several lines, each line must be identified as a comment:

Dim B     ' This comment for variable B is relatively long
          ' and stretches over several lines. The
          ' comment character must therefore be repeated 
          ' in each line.

Markers

A Apache OpenOffice Basic program can contain dozens, hundreds, or even thousands of markers, which are names for variables, constants, functions, and so on. When you select a name for a marker, the following rules apply:

  • Markers can only contain Latin letters, numbers, and underscores (_).
  • The first character of a marker must be a letter or an underscore.
  • Markers cannot contain special characters, such as ä â î ß.
  • The maximum length of a marker is 255 characters.
  • No distinction is made between uppercase and lowercase characters. The OneTestVariable marker, for example, defines the same variable as onetestVariable and ONETESTVARIABLE.
    There is, however, one exception to this rule: a distinction is made between uppercase and lowercase characters for UNO-API constants. More information about UNO is presented in Introduction to the Apache OpenOffice API.


Documentation caution.png The names of the Basic modules (by default Module1, Module2, etc) are known by Basic on a public scope. You must avoid to have a marker of public scope with the same name as one of the modules of the library.

Example : Suppose that in your Basic library you have a module named PrintDoc. You must not declare, in any module of your library, a Public variable or a Public constant, or a Sub, or a Function, named PrintDoc.

If you do have such collision, Basic may produce strange runtime error messages, or your Sub may not work.

Documentation note.png VBA : The rules for constructing markers are different in Apache OpenOffice Basic than in VBA. For example, Apache OpenOffice Basic only allows special characters in markers when using Option Compatible, since they can cause problems in international projects.


Here are a few examples of correct and incorrect markers:

Surname      ' Correct 
Surname5     ' Correct (number 5 is not the first digit)
First Name   ' Incorrect (spaces are not permitted)
DéjàVu       ' Incorrect (letters such as é, à are not permitted)
5Surnames    ' Incorrect (the first character must not be a number)
First,Name   ' Incorrect (commas and full stops are not permitted)

Enclosing a variable name in square brackets allows names that might otherwise be disallowed; for example, spaces.

  Dim [First Name] As String  'Space accepted in square brackets
  Dim [DéjàVu] As Integer     'Special characters in square brackets
  [First Name] = "Andrew"
  [DéjàVu] = 2


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