SIGN UP MEMBER LOGIN:    
ARTICLE

Use Customized Indexers

Posted by Jaish Mathews Articles | How do I March 14, 2006
Many of you are aware of indexers and its properties. For those unaware of it want to know that indexers are used to represent objects using indexes or even using keys. I.e. we can represent an object of a class by the way an array is using.
Reader Level:
Download Files:
 

Many of you are aware of indexers and its properties. For those unaware of it want to know that indexers are used to represent objects using indexes or even using keys. i.e. we can represent an object of a class by the way an array is using. But this is the default behavior of an indexer i.e. its return type is an integer and indexed objects are representing using integer type indexes. But we can override theses default items, as return type may be an object and representing indexed objects using string type keys rather than integer type indexes. I tried to explain here with some sort of coding.

Start a new C# web project in VS.NET and add a class Nations to it. Click the ClassView button, as any thing you want to add in a class type is very easy in VS.NET by making the class view of the project and just right click on that particular class. So after right click the class select add -> add indexer menu and your default indexer will added to the class. But change the definition of that indexer like below.

/// <summary>

/// Overrides the default indexer prototype

/// </summary>

public Nations this[string nationName]

{

          get

          {

                   return (Nations)nationsObjects[nationName]; 

          }

          set

          {

                    if (nationsObjects.Contains(nationName))

                   {

                             nationsObjects.Remove(nationName);

                   }

                   nationsObjects.Add(nationName,(Nations) value); 

          }

}

Basically I changed the return type of the indexer to "Nation" type and changed the default integer type indexes to a string type key. So now indexer will active by the statement <object>[<key>] compare to the previous status of <object>[<index>]. Each indexed objects want to be stored and will use later and more over this storing want to be performed using a string type key value. For that we want a ListDictionary in to which all indexed objects will keep. The statement nationsObjects.Add(nationName,(Nations) value); is used for this stored precess and the (Nations)nationsObjects[nationName]; statement in side the get will return a particular indexed object on the basis of a key value. Ok this is the main activities in the class.

In the front end I first create a normal object using the statement Nations n = new Nations(); and then the real thing happens.  Look this statement n["india"] = new Nations(); got it? Nothing here I indexed the object "n" using a key "india" and assign it to a type of Nations. Now you can use n ["india"] like any other object to access the resources of the type Nations. The difference is that we can represent this particular object with a key ("india"). In this way you can create more objects with different keys and perform various operations. Look at the sample code along with this article, as you may get clearer picture on customized indexers.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor