Compartilhar via


Declaração Get

Declares a Get property procedure used to retrieve the value of a property.

[ <attributelist> ] [ accessmodifier ] Get()
    [ statements ]
End Get

Parts

Term

Definition

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.

statements

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

End Get

Required. Terminates the definition of the Get property procedure.

Comentários

Every property must have a Get property procedure unless the property is marked WriteOnly. The Get procedure is used to return the current value of the property.

Visual Basic automatically calls a property's Get procedure when an expression requests the property's value.

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 Get procedure inside the property to which it applies.

The Get procedure defaults to the access level of its containing property unless you use accessmodifier in the Get 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 Get procedure Private, but not Public.

    If you are defining a ReadOnly property, the Get procedure represents the entire property. You cannot declare a different access level for Get, because that would set two access levels for the property.

  • Return Type. O Propriedade declaração pode declarar o tipo de dados do valor ele retorna. The Get procedure automatically returns that data type. You can specify any data type or the name of an enumeration, structure, class, or interface.

    If the Property statement does not specify returntype, the procedure returns Object.

Behavior

  • Returning from a Procedure. Quando o Get procedimento retorna para o código de chamada, a execução continua na demonstrativo que solicitou o valor da propriedade .

    Get property procedures can return a value using either the Instrução Return (Visual Basic) or by assigning the return value to the property name. For more information, see "Return Value" in Instrução Function (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.

  • 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 Instrução Return (Visual Basic). The Return statement simultaneously assigns the Get procedure return value and exits the procedure.

    If you use Exit Property without assigning a value to the property name, the Get procedure returns the default value for the property's data type. For more information, see "Return Value" in Instrução Function (Visual Basic).

    The following example illustrates two ways the read-only property quoteForTheDay can return the value held in the private variable quoteValue.

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

Exemplo

The following example uses the Get statement to return the value of a property.

Class propClass
    ' Define a private local variable to store the property value.
    Private currentTime As String
    ' Define the read-only property.
    Public ReadOnly Property dateAndTime() As String
        Get
            ' The Get procedure is called automatically when the
            ' value of the property is retrieved.
            currentTime = CStr(Now)
            ' Return the date and time As a string.
            Return currentTime
        End Get
    End Property
End Class

Consulte também

Tarefas

Passo a passo: Definindo classes (Visual Basic)

Referência

Declaração Set (Visual Basic)

Propriedade declaração

Declaração Saída (Visual Basic)

Outros recursos

Objetos e Classes no Visual Basic