DynamicObject::TrySetIndex Method (SetIndexBinder^, array<Object^>^, Object^)

 

Provides the implementation for operations that set a value by index. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations that access objects by a specified index.

Namespace:   System.Dynamic
Assembly:  System.Core (in System.Core.dll)

public:
virtual bool TrySetIndex(
	SetIndexBinder^ binder,
	array<Object^>^ indexes,
	Object^ value
)

Parameters

binder
Type: System.Dynamic::SetIndexBinder^

Provides information about the operation.

indexes
Type: array<System::Object^>^

The indexes that are used in the operation. For example, for the sampleObject[3] = 10 operation in C# (sampleObject(3) = 10 in Visual Basic), where sampleObject is derived from the DynamicObject class, indexes[0] is equal to 3.

value
Type: System::Object^

The value to set to the object that has the specified index. For example, for the sampleObject[3] = 10 operation in C# (sampleObject(3) = 10 in Visual Basic), where sampleObject is derived from the DynamicObject class, value is equal to 10.

Return Value

Type: System::Boolean

true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.

Classes derived from the DynamicObject class can override this method to specify how operations that access an object by index should be performed for a dynamic object. When the method is not overridden, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)

If this method is overridden, it is automatically invoked when you have an operation like sampleObject[3] = 10 in C# or sampleObject(3) = 10 in Visual Basic, where sampleObject is derived from the DynamicObject class.

Assume that you want to create an object in which properties can be accessed either by names such as Property0, Property1, and so on, or by index, so that, for example, sampleObject.Property0 is equivalent to sampleObject[0] in C# or sampleObject(0) in Visual Basic.

The following code example demonstrates the SampleDynamicObject class, which is derived from the DynamicObject class. The SampleDynamicObject class contains an object of the Dictionary<string, object> type (Dictionary(Of String, Object) in Visual Basic) to store the key-value pairs. SampleDynamicObject overrides the TrySetIndex and TryGetIndex methods to enable access by index. It overrides the TrySetMember and TryGetMember methods to enable access by property name.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: