Verwendung von Prozeduren, Funktionen und Eigenschaften

Im Folgenden ist die grundsätzliche Verwendung von Prozeduren, Funktionen und Eigenschaften in LibreOffice Basic beschrieben.

note

When you create a new module, LibreOffice Basic automatically inserts a Sub called "Main". This default name has nothing to do with the order or the starting point of a LibreOffice Basic project. You can also safely rename this Subroutine.


note

Für die Namen Ihrer öffentlichen Variablen, Unterprogramme, Funktionen und Eigenschaften gelten einige Einschränkungen. Sie dürfen nicht denselben Namen wie eines der Module der gleichen Bibliothek verwenden.


Procedures (Subroutines) functions (Function) and properties (Property) help you maintaining a structured overview by separating a program into logical pieces.

Ein Vorteil von Prozeduren, Funktionen und Eigenschaften ist, dass Sie einen einmal entwickelten Programmcode, der Task-Komponenten enthält, in einem anderen Projekt wiederverwenden können.

Übergabe von Variablen an Prozeduren, Funktionen oder Eigenschaften

Variablen können an Prozeduren, Funktionen oder Eigenschaften übergeben werden. Die Funktion oder Eigenschaft Sub muss deklariert werden, um Parameter zu erwarten:


  Sub SubName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...)
      ' Ihr Code kommt hier hin
  End Sub

Das Sub wird mit der folgenden Syntax aufgerufen:


  SubName(Value1, Value2,...)

Die an Sub übertragenen Parameter müssen zu denen passen, die in der Deklaration von Sub angegeben sind.

Das gleiche Verfahren gilt für eine Funktion. Darüber hinaus geben Funktionen immer ein Funktionsergebnis zurück. Das Ergebnis einer Funktion wird definiert, indem der Rückgabewert dem Funktionsnamen zugewiesen wird:


  Function FunctionName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...) As TYPENAME
      ' your code goes here
      FunctionName=Result
  End Function

The Function is called using the following syntax:


  Variable=FunctionName(Parameter1, Parameter2,...)

Properties combine the syntax of procedures and functions. A property usually requires up to one parameter.


  Private _IsApproved As TYPENAME
  Property Get IsApproved As TYPENAME
      ' your code goes here
      IsApproved = some_computation
  End Property
  Property Let IsApproved(value As TYPENAME)
      ' your code goes here
      _IsApproved = computed_value
  End Property

The Property is called using the following syntax:


  var = IsApproved
  IsApproved = some_value
tip

You can also use the fully qualified name to call a procedure, function or property:
Library.Module.Macro()
For example, to call the Autotext macro from the Gimmicks library, use the following command:
Gimmicks.AutoText.Main()


Übergeben von Variablen als Wert oder Referenz

Parameters can be passed to a procedure, a function or a property either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a Sub, a Function or a Property gets the parameter and can read and modify its value.

If you want to pass a parameter by value insert the key word ByVal in front of the parameter when you call a Sub, a Function or a Property, for example:


  Function ReadOnlyParms(ByVal p2, ByVal p2)
      ' your code goes here
  End Function
  result = ReadOnlyParms(parm1, parm2)

In this case, the original content of the parameter will not be modified by the Function since it only gets the value and not the parameter itself.

Defining Optional Parameters

Functions, procedures or properties can be defined with optional parameters, for example:


  Sub Rounding(number, Optional decimals, Optional format)
      ' your code goes here
  End Sub

Gültigkeitsbereich von Variablen

A variable defined within a Sub, a Function or a Property, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a Sub, a Function or a Property is exited.

Declaring Variables Outside a Sub a Function or a Property


Global VarName As TYPENAME

The variable is valid as long as the LibreOffice session lasts.


Public VarName As TYPENAME

Die Variable hat in allen Modulen Gültigkeit.


Private VarName As TYPENAME

Die Variable hat nur im Modul Gültigkeit.


Dim VarName As TYPENAME

Die Variable hat nur im Modul Gültigkeit.

Beispiele für private Variablen

Enforce private variables to be private across modules by setting CompatibilityMode(True).


  ' ***** Module1 *****
  Private myText As String
  Sub initMyText
      meinText = "Hallo"
      Print "In Modul 1 : ", meinText
  End Sub
   
  ' ***** Module2 *****
  'Option Explicit
  Sub demoBug
      CompatibilityMode( True )
      initMyText
      ' Gibt jetzt einen leeren String zurück
      ' (oder löst einen Fehler für Option Explicit aus)
      Print "Jetzt in Modul 2 : ", meinText
  End Sub

Saving Variable Content after Exiting a Sub a Function or a Property


  Static VarName As TYPENAME

The variable retains its value until the next time the a Function, Sub or Property is entered. The declaration must exist inside a Sub, a Function or a Property.

Specifying the Return Value Type of a Function or a Property

As with variables, include a type-declaration character after the function name, or the type indicated by As and the corresponding data type at the end of the parameter list to define the type of the function or property's return value, for example:


  Function WordCount(WordText As String) As Integer

Bitte unterstützen Sie uns!