Contingut

# Property Statement

A property, also called field or attribute, characterizes a given object or piece of information. Properties can be used to control access to data. It is common use to include instructions at setting or reading time of properties. Code can vary from simple assignment to complex context dependent routines. Using Get, Let or Set accessors enforces properties' consistency when necessary.

This statement requires Option Compatible to be placed before the executable program code in a module.

### Sintaxi:

             [Private | Public] Property Get name[char | As typename]
             End Property

             [Private | Public] Property [Let | Set] name[char] [([Optional [ByRef | ByVal]]value[char | As typename])] [As typename]
             End Property

### Paràmetres:

name: The property name.

argument: el valor que es passarà a la rutina que defineix Property.

Property setters often use a single argument. Multiple arguments are equally accepted.

##### fragment Argument

          {[Optional [ByRef|ByVal]]|ParamArray} argument {{As typename|char}[ = expression]|[()]As Variant}

##### Paràmetres

Optional: l'argument no és obligatori.

ByRef L'argument es passa per referència. ByRef és el predeterminat.

ByVal L'argument es passa per valor. El seu valor es pot modificar per la rutina anomenada.

char: caràcter de declaració del Tipus.

typename: Primitive data type name. Library or module defined types can also be specified.

= expressió Especifiqueu un valor predeterminat per a l'argument que coincideix amb el seu tipus declarat. Opcional és necessari per a cada argument que especifiqui un valor predeterminat.

ParamArray Usa ParamArray quan el nombre de paràmetres no està determinat. Un escenari típic és el d'una funció definida per l'usuari del Calc. L'ús del ParamArray hauria de limitar-se a l'últim argument d'una rutina.

UsingParamArray or = expression require Option Compatible to be placed before the executable program code in a module.

When using Option VBASupport 1, Optional arguments with no default value (= expression) are initialized according to their data type, except if Variant.

##### typename fragment

          {Boolean|Byte|Currency|Date|Double|Integer|Long|Object|Single|String|Variant}

##### char fragment

          { % | & | ! | # | $ | @ }

## Exemples

          Option Compatible
          Sub Main
              ProductName = "Office"
              Print ProductName ' mostra «LibreOffice»
          End Sub

          Private _office As String
          Property Get ProductName As String
              ProductName = _office
          End Property
          Property Let ProductName(value As String)
              _office = "Libre"& value
          End Property

In the absence of Property Let or Property Set, Property Get helps define protected information, which can not be accidently altered by a foreign module:

          Option Compatible
          Public Property Get PathDelimiter As String ' variable només de lectura
              Static this As String
              If this = "" Then : Select Case GetGuiType()
                  Case 1 : this = ";" ' Windows
                  Case 4 : this = ":" ' Linux or macOS
                  Case Else : Error 423 ' Property or method not defined: PathDelimiter
              End Select : End If
              PathDelimiter = this
          End Property ' read-only PathDelimiter

          Sub Main
              PathDelimiter = "un enunciat" ' no fa res
          End Sub

Use Let or Set when handling UNO services or class objects:

          Option Compatible
          Sub Main
              'Set anObject = CreateUnoService( "com.sun.star.frame.Desktop" )
              anObject = CreateUnoService( "com.sun.star.frame.Desktop" )
              Print anObject.SupportedServiceNames(0) ' displays "com.sun.star.frame.Frame"
          End Sub

          Property Get anObject As Object
              Set anObject = _obj
          End Property

          Private _obj As Object

          'Property Set anObject(value As Object)
              'Set _obj = value.CurrentFrame
          'End Property
          Property Let anObject(value As Object)
              Set _obj = value.CurrentFrame
          End Property

Temes relacionats

Conceptes bàsics quant a les subrutines

Ús de procediments, funcions i propietats

Function Statement

Sub Statement

End, Exit statements

Opció ClassModule expressió

Expressió compatible amb opcions

Ens cal la vostra ajuda!