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.

warning

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


Syntaxis:

Diagram Eigenschappen instructie Get


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

Property Set Statement diagram


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

Parameters:

name: The property name.

argument: Value to be passed to the Property setter routine.

note

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


fragment argument

fragment argument


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

Optioneel: Het argument is niet verplicht.

ByRef: Het argument wordt op referentie doorgegeven. ByRef is de standaardwaarde.

ByVal: De waarde wordt meegegeven. Deze waarde kan door de aangeroepen routine worden gewijzigd.

char: Het teken voor de declaratie van het type.

typename: Naam van het primitieve datatype. U kunt ook types specificeren die in bibliotheken en modules zijn gedefinieerd.

= expression: Specificeer een standaardwaarde die overeenkomt met het type. Als u een standaardwaarde voor een argument specificeert, dan is het logisch dat het argument optioneel is.

ParamArray: Gebruik ParamArray als het aantal parameters niet bekend is. Een voorbeeld hiervan is een door de gebruiker gedefinieerde functie in Calc. Als het argument ParamArray gebruikt wordt dan zou het altijd het laatste genoemde argument van de routine moeten zijn.

tip

Bij het gebruik van ParamArray of = expression is het verplicht dat Option Compatible wordt gebruikt in de code van de module voor het uitvoerbare gedeelte van de code.


warning

Bij het gebruik van Option VBASupport 1, Optional worden argumenten zonder standaardwaarde (= expression) geïnitialiseerd op grond van hun gegevenstype, m.u.v het type Variant.


fragment typename

fragment primitieve gegevenstypes


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

Tekens voor declaratie type


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

Examples


      Option Compatible
      Sub Main
          ProductName = "Office"
          Print ProductName ' displays "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
      
tip

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 ' Read-only variable
          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 = "a sentence" ' does nothing
      End Sub
      
note

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