Indexers

Thursday, 23 March 2006 10:03 by admin

I had just a run-in-review with this feature when my friend asked me how to enable your class to contain “angle bars []” to return a value from his class, much like an array. Well, the proper way of doing this is to use Indexers in your class.

Indexers works internally like a property. You need to specify its get and set routines to retrieve and give values to your indexed class. Here’s a sample to give you a clearer image on what I’m saying:

class IndexerClass{
    private int[] arr = new int[100];
    public int this[int index]   // Indexer declaration {
        get {
            // Check the index limits.
            if (index < 0 || index >= 100) {
                return 0;
            } else {
                return arr[index];
            }
        } set {
            if (!(index < 0 || index >= 100)) {
                arr[index] = value;
            }
        }
    }
}

class MainClass {
    static void Main() {
        IndexerClass test = new IndexerClass();
        // Call the indexer to initialize the elements #3 and #5.
        test[3] = 256;
        test[5] = 1024;
        for (int i = 0; i <= 10; i++) {
            System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
        }
    }
}

Note: sample taken from the MSDN documentation.

The highlighted text in the sample shows you how you declare an Index in your class. It’s pretty quite self-explanatory. For reasons of to be able to make this blog longer, I’ll explain some bits on the code.

The first you’ll notice is the use of the this keyword. The this keyword indicates the current class that you are in, you must use the this keyword to specify a property-like index in your class. The second is the angle brackets, here you’ll specify the index itself. In the example, it uses an integer as an index. Well, virtually you can specify any type that you want inside your index. It can be a string or a double. The third part that you’ll notice is the get and set routine. As I explained earlier it works just like a property. The get routine specifies whenever the user gets the value of your index. The set routine enables you to trap whenever the user gives a value to your index. In your set routine, you would notice another keyword, the value keyword. This variable contains the value that the user is giving to your indexed class.

indexerClass[0] = “test” // the value variable would contain “test”

Well I guess that’s all the essential part of indexed class.

You might be wondering on what practical use indexed class would be. Well, for starters, it would be very useful if you would have dynamic properties. Instead of exposing a getValue() function, its more readable and easier if you’ll provide an index to your class. A perfect example of this is the DataReader and DataRow class of ADO .Net. It uses indexes to access values in your fields in a row in the database.

SqlDataReader reader = command.ExecuteReader();
int empId = (int) reader[“Id”];

Happy Indexing!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:  
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Add comment


(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading