none
how to invoke multi parameter indexer in c# ? RRS feed

  • Question

  •  


    I am working with Indexer in C#. I have made following Indexer using Indexer overloading and multiparameter Indexer approach.
    Below is my sample code.

    //Indexer
            public string this[string cookieName, string cookieValue]
            {
                
                set
                {
                    SetTripleDESEncryptedCookie(cookieName, cookieValue);
                }
            }
    
            public string this[string cookieName, string cookieValue, DateTime cookieExpirationTime]
            {
    
                set
                {
                    SetTripleDESEncryptedCookie(cookieName, cookieValue,cookieExpirationTime);
                }
            } 
            public string this[string cookieName]
            {
                get {
                    string cookieValue = GetTripleDESEncryptedCookieValue(cookieName);
                    return cookieValue;
                }
            } 

     


    How can I invoke multiparameter Indexer ( public string this[string cookieName, string cookieValue]) from my class?
     I have to pass cookieName and cookieValue to Indexer.

    thanks in advance.

     


    Friday, July 6, 2012 9:59 AM

Answers

  • The same way that you would pass multiple parameters to a method:

    public class F
    {
       public object Indexer[string a, string b] { ... }
    }
    
    var f = new F();
    f["hello", "world"] = new object();

    Bear in mind that multi-parameter indexers are considered poor design style.  There is a code analysis (FxCop) rule that will raise a warning when it sees them.

    Evan

    Friday, July 6, 2012 6:58 PM
  • Don't use 'void' - use the indexer type - 'string'.

    With indexers and properties you still need to specify a return type, even though you may only be coding a 'set'.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Marked as answer by Dhruv Rajpoot Saturday, July 7, 2012 2:58 PM
    Saturday, July 7, 2012 2:28 PM

All replies

  • The same way that you would pass multiple parameters to a method:

    public class F
    {
       public object Indexer[string a, string b] { ... }
    }
    
    var f = new F();
    f["hello", "world"] = new object();

    Bear in mind that multi-parameter indexers are considered poor design style.  There is a code analysis (FxCop) rule that will raise a warning when it sees them.

    Evan

    Friday, July 6, 2012 6:58 PM
  • Thanks :)

    I need one more information. Suppose, I have created one multiparameter Indexer which is having only SET accessor.

     public void this[string cookieName, string cookieValue]
            {
                set
                {
                    SetTripleDESEncryptedCookie(cookieName, cookieValue);
                }
            }

    How can I invoke this indexer ?

    I am trying to invoke this indexer as below

    MyClass obj = new MyClass ();
    
    obj["cookiename","cookievalue"] = null;

    Since Indexer's return type is void, it is giving me error.

    Please Help. :(

    Saturday, July 7, 2012 11:10 AM
  • Don't use 'void' - use the indexer type - 'string'.

    With indexers and properties you still need to specify a return type, even though you may only be coding a 'set'.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Marked as answer by Dhruv Rajpoot Saturday, July 7, 2012 2:58 PM
    Saturday, July 7, 2012 2:28 PM
  • Thanks Dave. :)
    Saturday, July 7, 2012 2:58 PM