Property Statement

Una propiedad, tambi茅n conocida como campo o atributo, caracteriza un objeto o una informaci贸n en concreto. Se pueden utilizar las propiedades para controlar el acceso a los datos. Es habitual incluir instrucciones al definir o leer las propiedades. El c贸digo puede variar de una sencila asignaci贸n a rutinas complejas dependientes del contexto. El uso de los descriptores de acceso Get, Let y Set permiten aplicar coherencia en las propiedades si fuese necesario.

warning

Esta instrucci贸n requiere la utilizaci贸n de Option Compatible antes del c贸digo del programa en un m贸dulo.


Sintaxis:

Property Get Statement diagram


         [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
      

Par谩metros:

nombre: el nombre de la propiedad.

argumento: el valor que se transmitir谩 a la rutina establecedora Property.

note

Los establecedores Property a menudo utilizan un 煤nico argumento. Tambi茅n se aceptan argumentos m煤ltiples.


argument fragment

argument fragment


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

Optional: el argumento no es obligatorio.

ByRef: The argument is passed by reference. ByRef is the default.

ByVal: The argument is passed by value. Its value can be modified by the called routine.

char: car谩cter de declaraci贸n del Tipo.

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

= expression: Specify a default value for the argument, matching its declared type. Optional is necessary for each argument specifying a default value.

ParamArray: Use ParamArray when the number of parameters is undetermined. A typical scenario is that of a Calc user-defined function. Using ParamArray should be limited to the last argument of a routine.

tip

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


warning

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


typename fragment

primitive data types fragment


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

type declaration characters


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

Ejemplos


      Option Compatible
      Sub Main
          ProductName = "Office"
          Print ProductName ' muestra 芦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 ' variable de solo lectura
          Static this As String
          If this = "" Then : Select Case GetGuiType()
              Case 1 : this = ";" ' Windows
              Case 4 : this = ":" ' Linux o macOS
              Case Else : Error 423 ' Property or method not defined: PathDelimiter
          End Select : End If
          PathDelimiter = this
      End Property ' PathDelimiter de solo lectura
      
      Sub Main
          PathDelimiter = "un enunciado" ' no hace nada
      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
      

隆Necesitamos su ayuda!