Get Statement - Visual Basic (original) (raw)

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

Syntax

[ <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: - Protected- Friend- Private- Protected Friend See Access levels in 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.

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 Property Statement 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 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

Behavior

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  

Example

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

See also