This documentation is archived and is not being maintained.

11.6 Member Access Expressions

Visual Studio .NET 2003

A member access expression is used to access a member of an entity. A member access of the form E.I, where E is an expression, a built-in type, or omitted and I is an identifier, is evaluated and classified as follows:

  • If E is omitted, then the expression from the immediately containing With statement is substituted for E and the member access is performed. If there is no containing With statement, a compile-time error occurs.
  • If E is classified as a namespace and I is the name of an accessible member of that namespace, then the result is that member. The result is classified as a namespace or a type depending on the member.
  • If E is a built-in type or an expression classified as a type, and I is the name of an accessible member of E, then E.I is evaluated and classified as follows:
    • If I is the keyword New, then a compile-time error occurs.
    • If I identifies a type, then the result is that type.
    • If I identifies one or more methods, then the result is a method group with no associated instance expression.
    • If I identifies one or more properties, then the result is a property group with no associated instance expression.
    • If I identifies a shared variable, and if the variable is read-only, and the reference occurs outside the shared constructor of the type in which the variable is declared, then the result is the value of the shared variable I in E. Otherwise, the result is the shared variable I in E.
    • If I identifies a shared event, the result is an event access with no associated instance expression.
    • If I identifies a constant, then the result is the value of that constant.
    • If I identifies an enumeration member, then the result is the value of that enumeration member.
    • Otherwise, E.I is an invalid member reference, and a compile-time error occurs.
  • If E is classified as a variable or value, the type of which is T, and I is the name of an accessible member of E, then E.I is evaluated and classified as follows:
    • If I is the keyword New and E is an instance expression (Me, MyBase, or MyClass), then the result is a method group representing the instance constructors of the type of E with an associated instance expression of E. Otherwise, a compile-time error occurs.
    • If I identifies one or more methods, then the result is a method group with an associated instance expression of E.
    • If I identifies one or more properties, then the result is a property group with an associated instance expression of E.
    • If I identifies a shared variable or an instance variable, and if the variable is read-only, and the reference occurs outside a constructor of the class in which the variable is declared appropriate for the kind of variable (shared or instance), then the result is the value of the variable I in the object referenced by E. If T is a reference type, then the result is the variable I in the object referenced by E. Otherwise, if T is a value type and the expression E is classified as a variable, the result is a variable; otherwise the result is a value.
    • If I identifies an event, the result is an event access with an associated instance expression of E.
    • If I identifies a constant, then the result is the value of that constant.
    • If I identifies an enumeration member, then the result is the value of that enumeration member.
    • If T is Object, then the result is a late-bound member lookup classified as a property group with an associated instance expression of E.
  • Otherwise, E.I is an invalid member reference, and a compile-time error occurs.

If the member being accessed is shared, any expression on the left side of the period is superfluous and is not evaluated unless the member access is done late-bound. For example, consider the following code:

Class C
    Public Shared F As Integer = 10
End Class

Module Test
    Public Function ReturnC() As C
        Console.WriteLine("Returning a new instance of C.")
        Return New C()
    End Function

    Public Sub Main()
        Console.WriteLine("The value of F is: " & ReturnC().F)
    End Sub
End Module

It prints The value of F is: 10 because the function ReturnC does not need to be called to provide an instance of C to access the shared member F.

A late-bound member lookup is always classified as a property group, even though at run-time the identifier may resolve to a method group. At run time, the identifier is looked up and classified as a property or a method. If late-bound member lookup fails at run time, a System.MissingMemberException exception is thrown. Because late-bound member lookup is done solely off the run-time type of the associated instance expression, an object's run-time type is never an interface. Therefore, it is impossible to access interface members in a late-bound member access expression.

MemberAccessExpression ::=
   [ [ MemberAccessBase ] . ] IdentifierOrKeyword
MemberAccessBase ::= Expression | BuiltInTypeName

See Also

11.7 Dictionary Member Access | 11.4 Simple Expressions | 11.8 Invocation Expressions | 11.9 Index Expressions | 11.10 New Expressions | 11.11 Cast Expressions | 11.12 Operator Expressions | 11.3 Late-Binding Expressions | 9.2.5.2 Reference Parameters

Show: