Compartilhar via


Declaração Set (Visual Basic)

Declares a Set property procedure used to assign a value to a property.

[ <attributelist> ] [ accessmodifier ] Set (ByVal value [ As datatype ])
    [ statements ]
End Set

Parts

  • attributelist
    Optional. See Attribute List.

  • accessmodifier
    Optional on at most one of the Get and Set statements in this property. Can be one of the following:

    See Níveis de acesso em Visual Basic.

  • value
    Required. Parameter containing the new value for the property.

  • datatype
    Required if Option Strict is On. Data type of the value parameter. The data type specified must be the same as the data type of the property where this Set statement is declared.

  • statements
    Optional. One or more statements that run when the Set property procedure is called.

  • End Set
    Required. Terminates the definition of the Set property procedure.

Comentários

Every property must have a Set property procedure unless the property is marked ReadOnly. The Set procedure is used to set the value of the property.

Visual Basic automatically calls a property's Set procedure when an assignment statement provides a value to be stored in the property.

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. The 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.

The body of the property declaration can contain only the property's Get and Set procedures between the Propriedade declaração and the End Property statement. It cannot store anything other than those procedures. In particular, it cannot store the property's current value. You must store this value outside the property, because if you store it inside either of the property procedures, the other property procedure cannot access it. The usual approach is to store the value in a Private (Visual Basic) variable declared at the same level as the property. You must define a Set procedure inside the property to which it applies.

The Set procedure defaults to the access level of its containing property unless you use accessmodifier in the Set statement.

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 WriteOnly property, the Set procedure represents the entire property. You cannot declare a different access level for Set, because that would set two access levels for the property.

Behavior

  • Returning from a Property Procedure. Quando o Set procedimento retorna para o código de chamada, a execução continua após a demonstrativo que forneceu o valor a ser armazenado.

    Set property procedures can return using either the Instrução Return (Visual Basic) or the Declaração Saída (Visual Basic).

    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.

Exemplo

The following example uses the Set statement to set the value of a property.

Class propClass
    Private propVal As Integer
    Property prop1() As Integer
        Get
            Return propVal
        End Get
        Set(ByVal value As Integer)
            propVal = value
        End Set
    End Property
End Class

Consulte também

Referência

Declaração Get

Propriedade declaração

Instrução Sub (Visual Basic)

Conceitos

Procedimentos de propriedade (Visual Basic)