Gavin
Gavin

Reputation: 17382

Custom use of indexers []

I want to create an object that will work in a similar way to ASP.Net Session.

Say I call this object mySession, I want to make it so when you do

mySession["Username"] = "Gav"

It will either add it to a database table if it doesnt exist or update it if it does. I can write a method to do this but have no idea how to get it to fire when used with the indexer syntax ([]). I've never built an object that does anything like this with indexers.

Before anyone says anything I know the ASP.Net session can save to database but in this case I need a slightly simpler customized solution.

Any pointers or examples of using indexers in this way would be great.

Thanks

Upvotes: 11

Views: 6250

Answers (5)

Maharaj
Maharaj

Reputation: 332

Following is my slight variation on MSDN example:

public class KeyValueIndexer<K,V>
{
    private Dictionary<K, V> myVal = new Dictionary<K, V>();

    public V this[K k]
    {
        get
        {
            return myVal[k];
        }
        set
        {
            myVal.Add( k, value );
        }
    }
}

Person Class:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
}

Usage:

 static void Main( string[] args )
        {
            KeyValueIndexer<string, object> _keyVal = new KeyValueIndexer<string, object>();
            _keyVal[ "Person" ] = new Person() { FirstName="Jon", LastName="Doe", MiddleName="S" };
            _keyVal[ "MyID" ] = 123;
            Console.WriteLine( "My name is {0} {1}, {2}", ( ( Person ) _keyVal   [ "Person" ] ).FirstName, ( ( Person ) _keyVal[ "Person" ] ).MiddleName, ( ( Person ) _keyVal[ "Person" ] ).LastName );
            Console.WriteLine( "My ID is {0}", ( ( int ) _keyVal[ "MyID" ] ) );
            Console.ReadLine();
        }

Upvotes: 1

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

If you're wanting to use your class to control ASP.NET session state, look to implement the SessionStateStoreProviderBase class and IRequiresSessionState interface. You may then use your session provider by adding this to the system.web section of your web.config:

    <sessionState cookieless="true" regenerateExpiredSessionId="true" mode="Custom" customProvider="MySessionProvider">
        <providers>
            <add name="MySessionProvider" type="MySession.MySessionProvider"/>
        </providers>
    </sessionState>

I've seen this technique used for creating compressed/encrypted session-states.

Upvotes: 0

Rex M
Rex M

Reputation: 144122

It's actually pretty much the same as writing a typical property:

public class MySessionThing
{
    public object this[string key]
    {
        //called when we ask for something = mySession["value"]
        get
        {
            return MyGetData(key);
        }
        //called when we assign mySession["value"] = something
        set
        {
            MySetData(key, value);
        }
    }

    private object MyGetData(string key)
    {
        //lookup and return object
    }

    private void MySetData(string key, object value)
    {
        //store the key/object pair to your repository
    }
}

The only difference is we use the keyword "this" instead of giving it a proper name:

public          object            MyProperty
^access         ^(return) type    ^name
 modifier

public          object            this
^ditto          ^ditto            ^ "name"

Upvotes: 26

Yuliy
Yuliy

Reputation: 17718

Indexers in C# are properties with the name this. Here's an example...

public class Session {
   //...
   public string this[string key]
   {
      get { /* get it from the database */ }
      set { /* store it in the database */ }
   }
}

Upvotes: 4

Daniel Pryden
Daniel Pryden

Reputation: 60957

From the MSDN documentation:

class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)        
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}

Upvotes: 6

Related Questions