Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
DynamicObject Class
 TrySetIndex Method
Collapse All/Expand All Collapse All
.NET Framework Class Library
DynamicObject..::.TrySetIndex Method

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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 such as addition or multiplication.

Namespace:  System.Dynamic
Assembly:  System.Core (in System.Core.dll)
Visual Basic (Declaration)
Public Overridable Function TrySetIndex ( _
    binder As SetIndexBinder, _
    indexes As Object(), _
    value As Object _
) As Boolean
Visual Basic (Usage)
Dim instance As DynamicObject
Dim binder As SetIndexBinder
Dim indexes As Object()
Dim value As Object
Dim returnValue As Boolean

returnValue = instance.TrySetIndex(binder, _
    indexes, value)
C#
public virtual bool TrySetIndex(
    SetIndexBinder binder,
    Object[] indexes,
    Object value
)
Visual C++
public:
virtual bool TrySetIndex(
    SetIndexBinder^ binder, 
    array<Object^>^ indexes, 
    Object^ value
)
F#
abstract TrySetIndex : 
        binder:SetIndexBinder * 
        indexes:Object[] * 
        value:Object -> bool 
override TrySetIndex : 
        binder:SetIndexBinder * 
        indexes:Object[] * 
        value:Object -> bool 

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 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 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.

C#
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;

// The class derived from DynamicObject.
public class SampleDynamicObject : DynamicObject
{
    // The inner dictionary to store field names and values.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // Get the property value.
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        return dictionary.TryGetValue(binder.Name, out result);
    }

    // Set the property value.
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }

    // Set the property value by index.
    public override bool TrySetIndex(
        SetIndexBinder binder, object[] indexes, object value)
    {
        int index = (int)indexes[0];

        // If a corresponding property already exists, set the value.
        if (dictionary.ContainsKey("Property" + index))
            dictionary["Property"] = value;
        else
            // If a corresponding property does not exist, create it.
            dictionary.Add("Property" + index, value);
        return true;
    }         

    // Get the property value by index.
    public override bool TryGetIndex(
        GetIndexBinder binder, object[] indexes, out object result)
    {

        int index = (int)indexes[0];
        return dictionary.TryGetValue("Property" + index, out result);         
    }     
}
   
class Program
{
    static void Main(string[] args)
    {
        // Creating a dynamic object.
        dynamic sampleObject = new SampleDynamicObject();

        // Creating Property0. 
        // The TrySetMember method is called.
        sampleObject.Property0 = "Zero";

        // Getting the value by index.
        // The TryGetIndex method is called.
        Console.WriteLine(sampleObject[0]);

        // Setting the property value by index.
        // The TrySetIndex method is called.
        // (This method also creates Property1.)
        sampleObject[1] = 1;
        
        // Getting the Property1 value.
        // The TryGetMember method is called.
        Console.WriteLine(sampleObject.Property1);
       
        // The following statement produces a run-time exception
        // because there is no corresponding property.
        //Console.WriteLine(sampleObject[2]);
        Console.ReadLine();
    }
}

// This code example produces the following output:

// Zero
// 1
Visual Basic
Imports System.Linq.Expressions
Imports System.Dynamic

' The class derived from DynamicObject.
Public Class SampleDynamicObject
    Inherits DynamicObject

    ' The inner dictionary to store field names and values.
    Dim dictionary As New Dictionary(Of String, Object)

    ' Get the property value.
    Public Overrides Function TryGetMember(
        ByVal binder As System.Dynamic.GetMemberBinder, 
        ByRef result As Object) As Boolean

        Return dictionary.TryGetValue(binder.Name, result)

    End Function

    ' Set the property value.
    Public Overrides Function TrySetMember(
        ByVal binder As System.Dynamic.SetMemberBinder, 
        ByVal value As Object) As Boolean

        dictionary(binder.Name) = value
        Return True

    End Function

    ' Set the property value by index.
    Public Overrides Function TrySetIndex(
        ByVal binder As System.Dynamic.SetIndexBinder,
        ByVal indexes() As Object, ByVal value As Object) As Boolean

        Dim index As Integer = CInt(indexes(0))
        ' If a corresponding property already exists, set the value.
        If (dictionary.ContainsKey("Property" & index)) Then
            dictionary("Property") = value
        Else
            ' If a property does not exist, create it.
            dictionary.Add("Property" & index, value)
        End If
        Return True
    End Function

    ' Get the property value by index.
    Public Overrides Function TryGetIndex(
        ByVal binder As System.Dynamic.GetIndexBinder,
        ByVal indexes() As Object, ByRef result As Object) As Boolean

        Dim index = CInt(indexes(0))
        Return dictionary.TryGetValue("Property" & index, result)
    End Function
End Class


Module Module1
    Sub Main()

        ' Creating a dynamic object.
        Dim sampleObject As Object = New SampleDynamicObject()

        ' Creating Property0.
        ' The TrySetMember method is called.
        sampleObject.Property0 = "Zero"

        ' Getting the value by index.
        ' The TryGetIndex method is called.
        Console.WriteLine(sampleObject(0))

        ' Setting the property value by index.
        ' The TrySetIndex method is called.
        ' (This method also creates Property1.)
        sampleObject(1) = 1

        ' Getting the Property1 value.
        ' The TryGetMember method is called.
        Console.WriteLine(sampleObject.Property1)

        ' The following statement produces a run-time exception
        ' because there is no corresponding property.
        ' Console.WriteLine(sampleObject(2))
    End Sub
End Module

' This code example produces the following output:

' Zero
' 1

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker