Using Procedures, Functions and Properties

A continuació es descriu l'ús bàsic dels procediments, les funcions i les propietats del LibreOffice BASIC.

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

S’apliquen algunes restriccions als noms de les vostres variables públiques, subrutines, funcions i propietats. No heu d’utilitzar el mateix nom que un dels mòduls de la mateixa biblioteca.


Les funcions de procediments ( Sub ) ( Function ) i propietats ( Property ) vos ajuden a mantindre una visió general estructurada separant un programa en peces lògiques.

Un dels avantatges dels procediments, funcions i propietats és que, un cop hagueu desenvolupat un codi de programa que continga components de la tasca, podeu utilitzar aquest codi en un altre projecte.

Pas de variables a procediments, funcions o propietats

Les variables es poden passar a procediments, funcions o propietats. La Sub Funció o Propietat s'ha de declarar per esperar paràmetres:


  Sub SubName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...)
      ' el vostre codi va ací
  End Sub

La Sub es crida mitjançant la sintaxi següent:


  [Call] SubName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...)

Els paràmetres que es passen a una Sub s'han d'ajustar als especificats a la declaració Sub.

El mateix procés s'aplica a una funció . A més, les funcions sempre retornen un resultat de funció. El resultat d’una funció es defineix assignant el valor de retorn al nom de la funció:


  Function FunctionName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...) As TYPENAME
      ' el vostre codi va ací
      FunctionName=Result
  End Function

La Function es crida mitjançant la sintaxi següent:


  Variable = FunctionName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...)

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
      ' el vostre codi va ací
      IsApproved = some_computation
  End Property
  Property Let IsApproved(value As TYPENAME)
      ' el vostre codi va ací
      _IsApproved = computed_value
  End Property

La Property es crida mitjançant la sintaxi següent:


  var = IsApproved
  IsApproved = some_value
tip

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


Pas de variables per valor o referència

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)
      ' el vostre codi va ací
  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.

Definició de paràmetres opcionals

Les funcions, procediments o propietats es poden definir amb paràmetres opcionals, per exemple:


  Sub Rounding(number, Optional decimals, Optional format)
      ' el vostre codi va ací
  End Sub

Positional or Keyword Arguments

When you call a function or a subroutine, you may pass its arguments by position or by name. Passing by position means just listing the arguments in the order in which the parameters are defined in the function or subroutine. Passing by name requires you to prefix the argument with the name of the corresponding parameter followed by a colon and an equal sign (:=). Keyword arguments may appear in any order. Refer to Basic Replace() function for such examples.

When needing to pass less parameters, use keywords arguments. Passing values for fewer parameters by position requires to supply values for all parameters before them, optional or not. This ensures that the values are in the correct positions. If you pass the parameters by name - using keyword arguments - you may omit all other intermediate arguments.

Àmbit de variables

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.

Declaració de variables fora d'un Sub una funció o una propietat


Global VarName As TYPENAME

La variable és vàlida mentre duri la sessió del LibreOffice.


Public VarName As TYPENAME

La variable és vàlida a tots els mòduls.


Private VarName As TYPENAME

La variable només és vàlida en este mòdul.


Dim VarName As TYPENAME

La variable només és vàlida en este mòdul.

Exemple de variables privades

Forceu que les variables privades siguen privades entre mòduls establint CompatibilityMode(True).


  ' ***** Module1 *****
  Private myText As String
  Sub initMyText
      myText = "Hola"
      Print "al mòdul1 : ", myText
  End Sub
   
  ' ***** Module2 *****
  'Option Explicit
  Sub demoBug
      CompatibilityMode( True )
      initMyText
      ' Torna una cadena buida
      ' (o genera un error per a Option Explicit)
      Print "Ara al mòdul2 : ", myText
  End Sub

Com guardar el contingut d'una variable després d'eixir d'una Sub, una Function o una 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

Ens cal la vostra ajuda!