locked
C# Indexed Property Accessors RRS feed

  • Question

  • So is there no way what so ever to create a named indexed property accessor in c#?

    VB:
    property Value(Key as String) as Object
      get
      end get
      set
      end set
    end property


    And if so, how does the .Net framework get around all the other built in objects that have Named Indexed property accessors in them, because I know there are quite a few that have other properties than just Item() which have an Index parameter.

    Thanks
    Jaeden "Sifo Dyas" al'Raec Ruiner
    Wednesday, March 4, 2009 5:20 PM

Answers

  • C# does not allow the creation of named indexers, other than the one named "Item".  It's a language limitation.  You can only create multiple indexers by use of differing method signatures.  They will all be named "Item" and have different signatures.

    public class MyClass

    {

        private Dictionary<string, object> _innerDictionary = new Dictionary<string, object>();

     

        public object this[string key]

        {

            get

            {

                if (_innerDictionary.ContainsKey(key))

                    return _innerDictionary[key];

                return null;

            }

            set

            {

                _innerDictionary[key] = value;

            }

        }

     

        public object this[string key, string key2]

        {

            get

            {

                return null;

            }

            set

            {

                // intentionally left blank... example.

            }

        }

    } 


    David Morton - http://blog.davemorton.net/
    • Marked as answer by JaedenRuiner Wednesday, March 4, 2009 6:14 PM
    Wednesday, March 4, 2009 6:09 PM
    Moderator

All replies

  • These are recognized in C# under their actual method names.  C# will, in those cases, disallow the indexer bracket access, and allow access to the method via the get_Value and set_Value methods, unless the property is declared as Default in VB. 

    Take the following, for example:

        Public Class Class1  
            Property Value(ByVal Key As StringAs Object 
                Get 
                    Return New Object()  
                End Get 
                Set(ByVal value As Object)  
     
                End Set 
            End Property 
     
            Default Property Item(ByVal Key As StringAs Object 
                Get 
                    Return New Object()  
                End Get 
                Set(ByVal value As Object)  
     
                End Set 
            End Property 
        End Class 
     

    In C#, you would access this as follows:

    Class1 c1 = new Class1();  
    c1["blah"] = "jim"// calls the setter for Item, because Item is marked as Default in the VB code.   
    c1.set_Value("blah""jim");  // calls the Value's setter.   

    Note that this would be set_Item and get_Item if the Item property wasn't declared as Default.
    David Morton - http://blog.davemorton.net/
    Wednesday, March 4, 2009 5:48 PM
    Moderator
  • Alright, if that is the case, then how can I define it is C# from during creation of the class:

     Public Class Class1   
            Property Value(ByVal Key As StringAs Object  
                Get  
                    Return New Object()   
                End Get  
                Set(ByVal value As Object)   
      
                End Set  
            End Property  
      
            Default Property Item(ByVal Key As StringAs Object  
                Get  
                    Return New Object()   
                End Get  
                Set(ByVal value As Object)   
      
                End Set  
            End Property  
        End Class  

    public class class1 { 
      public object this[string key] { 
        get { return MyArray[key]; } 
      } 
     
      public string get_Value(string key) { 
        return ((MyObject) this[key]).Value; 
      } 
     
      public void set_Value(string key, string value) { 
        ((MyObject)this[key]).Value = value; 
      } 

    with this the code:
    class1 c1 = new Class1();
    c1.Value["x"];

    Value is unrecognized...
    Thanks
    Jaeden "Sifo Dyas" al'Raec Ruiner

    public class Class1 { 
       public object this[string key] { 
         get { return MyArray[key]; } 
       } 
     
       public string get_Value(string key) { 
          return (MyObject)this[key].Value; 
       } 
     
       public void get_Value(string key, string value) { 
          this[key].Value = value; 
       } 
     
     
       

    Wednesday, March 4, 2009 6:00 PM
  • C# does not allow the creation of named indexers, other than the one named "Item".  It's a language limitation.  You can only create multiple indexers by use of differing method signatures.  They will all be named "Item" and have different signatures.

    public class MyClass

    {

        private Dictionary<string, object> _innerDictionary = new Dictionary<string, object>();

     

        public object this[string key]

        {

            get

            {

                if (_innerDictionary.ContainsKey(key))

                    return _innerDictionary[key];

                return null;

            }

            set

            {

                _innerDictionary[key] = value;

            }

        }

     

        public object this[string key, string key2]

        {

            get

            {

                return null;

            }

            set

            {

                // intentionally left blank... example.

            }

        }

    } 


    David Morton - http://blog.davemorton.net/
    • Marked as answer by JaedenRuiner Wednesday, March 4, 2009 6:14 PM
    Wednesday, March 4, 2009 6:09 PM
    Moderator