I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.
We all know Array is nothing but sequential memory locations in which it stores data. Let us say the size of continues memory location is 80 KB and size of one unit of data is 2 KB. The statement implies that we have an array of 40 data in a sequential memory locations. The below picture explains this:
For Example, Consider the below Array:
If we assume the size required to store each department is 2 KB, we have 40 blocks of size 2 KB is allocated to accommodate 40 department objects. Also, note that 40 objects are allocated in sequential order. So, how do we get the object at the third memory block? We use the below statement:
What is  represents here? It says to take the object from the third memory block. So here, each memory blocks are referred by the Indexed location. So the notation  is what called Indexer.
In this article, we will create a collection class and then we will see how we can implement a simple Position Based Indexer and Value Based Indexer.
2. The Product Class
We consider the below specified simple class which represents the product for a retail shop. It has two private data members, a constructor and a public methods to set or retrieve the data members.
3. The SuperMarket Class
As Every Super market have a collection of products, this class will have a collection of a product object. The members of this class is shown below:
The variable “Pos” is to iterate through the Products collection. OK, you may get the idea now. The class SuperMarket is a user defined (defined by us now) collection of Products.
The constructor of this class will take an array of products as a parameter and assigns it to the private member of the Products instance. Note, for this article, we are allocating fixed space of 1000 slots and each space has null reference initially. We will replace the null reference with the passed in the array of objects. Below is the code for the Constructor:
We override the ToString() method to get the entire product in a comma-separated format. The method implementation is shown below:
4. Position based Indexer
The will implement the indexer just like the operator overloading functions. To implement the '’ notation follow the below Syntax:
The Implementation Skeleton on the Simple Indexer is shown below:
In the above picture, we can see that get portion of the indexer is called whenever we want to read from the collection using “Index Of” operator. The same way, set portion gets called when we want to write to the collection.
In our case, we will implement the Index for the Supermarket. So, using the Positional Index, we will retrieve a product. The way the index implemented will give a NULL reference to the caller when the index is out of Range Say below 0 or above 1000. Note, the Maximum product supported by the supermarket is 1000. Below is the function implementation:
The client code which uses the indexer is given below.
Client 001: Creates the Array of 6 Products.
Client 002: Populates the product array. In real world Array will be populated from Database.
Client 003: Supermarket is created with 6 New Products. Note, in our example, the supermarket capacity is 1000.
Client 004: Uses the Indexer to add a new product to the Products collection. market = new Product(1015, "Orange"); Will call the indexer with index = 15. new Product(1015, "Orange"); will be referred in the set portion of our Indexer using the value keyword.
Client 005: Product prod = market; Supermarket Object accessed with Indexer . We will move to get a portion of the Indexer and indexer returns Product at the position offset 5. The returned object reference is assigned to prod.
5. Value Based Indexer
The previous indexer locates the memory block based on the Index by calculating the offset as it knows the size of the memory block. Now, we will implement value-based index which will get the product based on the ProductId value. We will walk through the changes done on the Classes.
1) The product class changed to have a method which sets the ProductName, and a get method for ProductId. We also have an overridden method for ToString just to print Product Name. Below are the Changes:
2) In the SuperMarket class, we declare a variable called numeric_index_mode. We use this variable to decide whether the Indexer is referred as Positional-based or in Value-based .
Inside the constructor, We initialize indexer mode to 0. It means, the SuperMarket class by default treats the Indexer as Positional indexer and retrieves the product based on the calculated positional offset.
3) We implement a public function to retrieve the Positional index for the passed-in Product Id. Note, the product id is unique for this Value based Index. The function will iterate through the Products in the Supermarket and returns when a match for Product ID is found. It will return –1 when match not occurred. Below is the new function implemented to support the value-based index:
4) First, in the get portion of the Indexer, wrap the existing code with an if construct. That is; when the Mode = 0, go with positional Index. It holds true for Set portion of the Indexer as well. Below is the Change:
5) If we are in Value mode, In the Get part of the indexer first get the positional index for a product id. Once we have the positional index, we are ready to make a recursive call to same indexer routine. Make sure to set the indexer mode to 0 as we need to access the indexer to get the product based on the indexed position. Once we have the Product, reset index mode back to 1; that reset indexer mode to value based on the client code would expect that. Below is the Code for “Get” portion:
Note, we can change the GetProduct function to return a product and make this implementation simple.
6) Set portion of the Indexer also changed in the same way. I hope further explanation not required:
Using Value based Indexer
Code below explains how we switch from Position based indexer to Value based indexer, use value based indexer and go back to default indexer mode. Read the inline comments and it is easy to follow.
6. Closing Notes
1) You can implement string value based indexer also. The skeleton is:
Complete Source Code
The Code output
The output of executing the above example is given below: