11.9 Index Expressions
An index expression results in an array element or reclassifies a property group into a property access. An index expression consists of, in order, an expression, an opening parenthesis, an index argument list, and a closing parenthesis. The target of the index expression must be classified as either a property group or a value. An index expression is processed as follows:
- If the target expression is classified as a value and if its type is not an array type, Object, or System.Array, the type must have a default property. The index is performed on a property group that represents all of the default properties of the type (which may have different names). Although it is not valid to declare a parameterless default property in Visual Basic .NET, other languages may allow declaring such a property. Consequently, indexing a property with no arguments is allowed.
- If the expression results in a value of an array type, the number of arguments in the argument list must be the same as the rank of the array type and may not include named arguments. If any of the indexes are invalid at run time, a System.IndexOutOfRangeException exception is thrown. Each expression must be implicitly convertible to type Integer. The result of the index expression is the variable at the specified index and is classified as a variable.
- If the expression is classified as a property group, overload resolution is used to determine whether one of the properties is applicable to the index argument list. If the property group only contains one property that has a Get accessor and if that accessor takes no arguments, then the property group is interpreted as an index expression with an empty argument list. The result is used as the target of the current index expression. If no properties are applicable, then a compile-time error occurs. Otherwise, the expression results in a property access with the associated instance expression (if any) of the property group.
- If the expression is classified as a late-bound property group or as a value whose type is Object or System.Array, the processing of the index expression is deferred until run time and the indexing is late bound. The expression results in a late-bound property access typed as Object. The associated instance expression is either the target expression, if it is a value, or the associated instance expression of the property group. At run time the expression is processed as follows:
- If the expression is classified as a late-bound property group, the expression may result in either a method group, a property group, or a value (if the member is an instance or shared variable). If the result is a method group or property group, overload resolution is applied to the group to determine the correct method for the argument list. If overload resolution fails, a System.Reflection.AmbiguousMatchException exception is thrown. Then the result is processed either as a property access or as an invocation and the result is returned. If the invocation is of a subroutine, the result is Nothing.
- If the run-time type of the target expression is an array type or System.Array, the result of the index expression is the value of the variable at the specified index.
- Otherwise, the run-time type of the expression must have a default property and the index is performed on the property group that represents all of the default properties on the type. If the type has no default property, then a System.MissingMemberException exception is thrown.
Because late-bound overload resolution is done on the run-time type of the arguments, it is possible that an expression might produce different results based on whether it is evaluated at compile time or run time. The following example illustrates this difference:
Imports System Class Base End Class Class Derived Inherits Base End Class Module Test Sub F(ByVal b As Base) Console.WriteLine("F(Base)") End Sub Sub F(ByVal d As Derived) Console.WriteLine("F(Derived)") End Sub Sub Main() Dim b As Base = New Derived() Dim o As Object = b F(b) F(o) End Sub End Module
This code displays:
IndexExpression ::= Expression ( [ ArgumentList ] )
11.4 Simple Expressions | 11.8 Invocation Expressions | 11.6 Member Access Expressions | 11.10 New Expressions | 11.11 Cast Expressions | 11.12 Operator Expressions | 11.3 Late-Binding Expressions | 220.127.116.11 Reference Parameters