Propriedade declaração

Declares the name of a property, and the property procedures used to store and retrieve the value of the property.

[ <attributelist> ] [ Default ] [ accessmodifier ] 
[ propertymodifiers ] [ Shared ] [ Shadows ] [ ReadOnly | WriteOnly ] 
Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslist ]
    [ <attributelist> ] [ accessmodifier ] Get
        [ statements ]
    End Get
    [ <attributelist> ] [ accessmodifier ] Set ( ByVal value As returntype [, parameterlist ] )
        [ statements ]
    End Set
End Property
- or -
[ <attributelist> ] [ Default ] [ accessmodifier ] 
[ propertymodifiers ] [ Shared ] [ Shadows ] [ ReadOnly | WriteOnly ] 
Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslist ]

Parts

Term

Definition

attributelist

Optional. List of attributes that apply to this property or Get or Set procedure. See Attribute List.

Default

Optional. Specifies that this property is the default property for the class or structure on which it is defined. Default properties must accept parameters and can be set and retrieved without specifying the property name. If you declare the property as Default, you cannot use Private on the property or on either of its property procedures.

accessmodifier

Optional on the Property statement and on at most one of the Get and Set statements. Can be one of the following:

See Níveis de acesso em Visual Basic.

propertymodifiers

Optional. Can be one of the following:

Shared

Optional. See Compartilhamento (Visual Basic).

Shadows

Optional. See Shadows (Visual Basic).

ReadOnly

Optional. See ReadOnly (Visual Basic).

WriteOnly

Optional. See WriteOnly (Visual Basic).

name

Required. Name of the property. See Nomes de elementos declarados (Visual Basic).

parameterlist

Optional. List of local variable names representing the parameters of this property, and possible additional parameters of the Set procedure. See Lista de parâmetros (Visual Basic).

returntype

Required if Option Strict is On. Data type of the value returned by this property.

Implements

Optional. Indicates that this property implements one or more properties, each one defined in an interface implemented by this property's containing class or structure. See Implementa Declaração.

implementslist

Required if Implements is supplied. List of properties being implemented.

implementedproperty [ , implementedproperty ... ]

Each implementedproperty has the following syntax and parts:

interface.definedname

PartDescription
interface Required.Name of an interface implemented by this property's containing class or structure.
definedname Required.Name by which the property is defined in interface.

Get

Optional. Necessário se a propriedade está marcada WriteOnly. Starts a Get property procedure that is used to return the value of the property.

statements

Optional. Block of statements to run within the Get or Set procedure.

End Get

Terminates the Get property procedure.

Set

Optional. Necessário se a propriedade está marcada ReadOnly. Starts a Set property procedure that is used to store the value of the property.

End Set

Terminates the Set property procedure.

End Property

Terminates the definition of this property.

Comentários

The Property statement introduces the declaration of a property. A property can have a Get procedure (read only), a Set procedure (write only), or both (read-write). Você pode omitir o Get e Setprocedimento ao usar uma propriedadede auto-implementado. For more information, see Propriedades autoimplementadas (Visual Basic).

Você pode usar Property somente no nível de classe . This means the declaration context for a property must be a class, structure, module, or interface, and cannot be a source file, namespace, procedure, or block. For more information, see Contextos de declaração e níveis de acesso padrão (Visual Basic).

By default, properties use public access. You can adjust a property's access level with an access modifier on the Property statement, and you can optionally adjust one of its property procedures to a more restrictive access level.

Visual Basic passes a parameter to the Set procedure during property assignments. If you do not supply a parameter for Set, the integrated development environment (IDE) uses an implicit parameter named value. This parameter holds the value to be assigned to the property. You typically store this value in a private local variable and return it whenever the Get procedure is called.

Rules

  • Mixed Access Levels. Se você estiver definindo uma leitura-gravar a propriedade, opcionalmente, você pode especificar outro nível de acesso para qualquer um de Get ou o Set procedimento, mas não ambos. If you do this, the procedure access level must be more restrictive than the property's access level. For example, if the property is declared Friend, you can declare the Set procedure Private, but not Public.

    If you are defining a ReadOnly or WriteOnly property, the single property procedure (Get or Set, respectively) represents all of the property. You cannot declare a different access level for such a procedure, because that would set two access levels for the property.

  • Return Type. O Propertydedemonstrativo pode declarar o tipo de dados de que o valor retornado. You can specify any data type or the name of an enumeration, structure, class, or interface.

    If you do not specify returntype, the property returns Object.

  • Implementação. Se esta propriedade usa a Implementspalavra-chave, o que contém uma classe ou estrutura deve ter um Implementsademonstrativo imediatamente seguinte sua Class ou Structuredemonstrativo. The Implements statement must include each interface specified in implementslist. However, the name by which an interface defines the Property (in definedname) does not have to be the same as the name of this property (in name).

Behavior

  • Returning from a Property Procedure. Quando o Get ou Set procedimento retorna para o código de chamada, a execução continua com a demonstrativo após a demonstrativo que invocou o proprietário.

    The Exit Property and Return statements cause an immediate exit from a property procedure. Any number of Exit Property and Return statements can appear anywhere in the procedure, and you can mix Exit Property and Return statements.

  • Valorde retorno. Para retornar um valor de um Get procedimento, você pode atribuir o valor do nome de propriedade -lo ou incluí-lo em um Return demonstrativo. The following example assigns the return value to the property name quoteForTheDay and then uses the Exit Property statement to return.

    Private quoteValue As String = "No quote assigned yet."
    
    ReadOnly Property quoteForTheDay() As String
        Get
            quoteForTheDay = quoteValue
            Exit Property
        End Get
    End Property
    

    If you use Exit Property without assigning a value to name, the Get procedure returns the default value for the property's data type.

    The Return statement at the same time assigns the Get procedure return value and exits the procedure. The following example shows this.

    Private quoteValue As String = "No quote assigned yet."
    
    ReadOnly Property quoteForTheDay() As String
        Get
            Return quoteValue
        End Get
    End Property
    

Exemplo

The following example declares a property in a class.

Class Class1
    ' Define a local variable to store the property value.
    Private propertyValue As String
    ' Define the property.
    Public Property prop1() As String
        Get
            ' The Get property procedure is called when the value
            ' of a property is retrieved.
            Return propertyValue
        End Get
        Set(ByVal value As String)
            ' The Set property procedure is called when the value 
            ' of a property is modified.  The value to be assigned
            ' is passed in the argument to Set.
            propertyValue = value
        End Set
    End Property
End Class

Consulte também

Referência

Declaração Get

Declaração Set (Visual Basic)

Lista de parâmetros (Visual Basic)

Padrão (Visual Basic)

Conceitos

Propriedades autoimplementadas (Visual Basic)

Outros recursos

Objetos e Classes no Visual Basic