Export (0) Print
Expand All

9.7.1 Get Accessor Declarations

Visual Studio .NET 2003

A Get accessor (getter) is declared by using a property Get declaration. A property Get declaration consists of the keyword Get followed by a statement block. Given a property named P, a Get accessor declaration implicitly declares a method with the name get_P with the same modifiers, type, and parameter list as the property. If the type contains a declaration with that name, a compile-time error results.

A special local variable, which is implicitly declared in the Get accessor body's declaration space with the same name as the property, represents the return value of the property. The local variable has special name resolution semantics when used in expressions. If the local variable is used in a context that expects an expression that is classified as a method group, such as an invocation expression, then the name resolves to the function rather than to the local variable. For example:

ReadOnly Property F(ByVal i As Integer) As Integer
    Get
        If i = 0 Then
            F = 1    ' Sets the return value.
        Else
            F = F(i – 1) ' Recursive call.
        End If
    End Get
End Property

The use of parentheses can cause ambiguous situations (such as F(1) where F is a property whose type is a one-dimensional array). In all ambiguous situations, the name resolves to the property rather than the local variable. For example:

ReadOnly Property F(ByVal i As Integer) As Integer()
    Get
        If i = 0 Then
            F = new Integer(3) { 1, 2, 3 }
        Else
            F = F(i – 1) ' Recursive call, not index.
        End If
    End Get
End Property

When control flow leaves the Get accessor body, the value of the local variable is passed back to the invocation expression. Because invoking a Get accessor is conceptually equivalent to reading the value of a variable, it is considered bad programming style for Get accessors to have observable side effects, as illustrated in the following example:

Class Counter
    Private Value As Integer

    Public ReadOnly Property NextValue() As Integer
        Get
            Value += 1
            Return Value
        End Get
    End Property
End Class

The value of the NextValue property depends on the number of times the property has previously been accessed. Thus, accessing the property produces an observable side effect, and the property should instead be implemented as a method.

The "no side effects" convention for Get accessors does not mean that Get accessors should always be written to simply return values stored in variables. Indeed, Get accessors often compute the value of a property by accessing multiple variables or invoking methods. However, a properly designed Get accessor performs no actions that cause observable changes in the state of the object.

PropertyGetDeclaration ::=
   [ Attributes ] Get LineTerminator
   [ Block ]
   End Get

See Also

9.7.2 Set Accessor Declarations | 9.7.3 Default Properties |9.7 Properties | Setting and Retrieving Properties (Visual Basic Language Concepts)

Show:
© 2014 Microsoft