Home

Topics on Indexers

 

Multi-Parameterized Indexed Properties

The indexed properties we have used so far were taking only one parameter. You can create an indexed property whose array uses more than one dimension. To start an indexed property that would use various parameters, first declare the array as we saw in Lesson 24. After declaring the array, create a this property that takes the parameters. Here is an example for an indexed property that relates to a two-dimensional array:

public class Numbers
{
    double[,] nbr;

    public double this[int x, int y]
    {
    }
}

 

In the body of an accessor (get or set), use the parameter as appropriately as you see fit. At a minimum, for a get accessor, you can return the value of the array using the parameters based on the rules of a two-dimensional array. This can be done as follows:

public class Numbers
{
    double[,] nbr;

    public double this[int x, int y]
    {
        get { return nbr[x, y]; }
    }
}

After creating the property, you can access each element of the array by applying the square brackets to an instance of the class. Here is an example:

using System;

public class Numbers
{
    double[,] nbr;

    public double this[int x, int y]
    {
        get { return nbr[x, y]; }
    }

    public Numbers()
    {
        nbr = new double[2,4];
        nbr[0, 0] = 927.93;
        nbr[0, 1] = 45.155;
        nbr[0, 2] = 2.37094;
        nbr[0, 3] = 73475.25;
        nbr[1, 0] = 186.72;
        nbr[1, 1] = 82.350;
        nbr[1, 2] = 712734.95;
        nbr[1, 3] = 3249.0057;
    }
}

public class Program
{
    static int Main()
    {
        Numbers nbr = new Numbers();

        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                double value = nbr[i, j];
                Console.WriteLine("Number [{0}][{1}]: {2}", i, j, value);
            }
        }

        Console.WriteLine();
        return 0;
    }
}

Remember that one of the most valuable features of an indexed property is that, when creating it, you can make it return any primitive type and you can make it take any parameter of your choice. Also, the parameters of a multi-parameter indexed property don't have to be the same type. One can be a character while the other is a bool type; one can be a double while the other is a short, one can be an integer while the other is a string. When defining the property, you must apply the rules of both the methods and the arrays. Here is an example of a property that takes an integer and a string:

using System;

public class Catalog
{
    long[] nbrs;
    string[] names;

    public double this[long nbr, string name]
    {
        get
        {
            if ((nbr == nbrs[0]) && (name == names[0]))
                return 275.25;
            else if ((nbr == nbrs[1]) && (name == names[1]))
                return 18.75;
            else if ((nbr == nbrs[2]) && (name == names[2]))
                return 50.00;
            else if ((nbr == nbrs[3]) && (name == names[3]))
                return 65.35;
            else if ((nbr == nbrs[4]) && (name == names[4]))
                return 25.55;
            else
                return 0.00;
        }
    }

    public Catalog()
    {
        nbrs = new long[5];
        nbrs[0] = 273974;
        nbrs[1] = 539759;
        nbrs[2] = 710234;
        nbrs[3] = 220685;
        nbrs[4] = 192837;
        names = new string[5];
        names[0] = "Women Double-faced wool coat";
        names[1] = "Men Cotton Polo Shirt";
	names[2] = "Children Cable-knit Sweater";
	names[3] = "Women Floral Silk Tank Blouse";
        names[4] = "Girls Jeans with Heart Belt";
    }
}

public class Program
{
    static int Main()
    {
        Catalog cat = new Catalog();

        long itemNumber = 539759;
        string itemDescription = "Men Cotton Polo Shirt";
        double price = cat[itemNumber, itemDescription];

        Console.WriteLine("Item #:      {0}", itemNumber);
        Console.WriteLine("Description: {0}", itemDescription);
        Console.WriteLine("Unit Price:  {0}", price);

        Console.WriteLine();
        return 0;
    }
}

This would produce:

Item #:      539759
Description: Men Cotton Polo Shirt
Unit Price:  18.75

Press any key to continue . . .

In the above example, we first declared the variables to be passed as parameters to the indexed property. You can also pass the parameter(s) directly on the instance of the class. Here is an example:

public class Program
{
    static int Main()
    {
        Catalog cat = new Catalog();

        double price = cat[220685, "Women Floral Silk Tank Blouse"];

        Console.WriteLine("Item #:      220685");
        Console.WriteLine("Description: Women Floral Silk Tank Blouse");
        Console.WriteLine("Unit Price:  {0}", price);

        Console.WriteLine();
        return 0;
    }
}

Just as you can create a two-dimensional indexed property, you can also create a property that takes more than two parameters. Once again, it is up to you to decide what type of parameter would be positioned where in the square brackets. Here is an example of an indexed property that takes three parameters:

using System;

public class Catalog
{
    public string this[long nbr, string name, double price]
    {
        get
        {
            return "Item #:      " + nbr.ToString() + "\n" +
                   "Description: " + name + "\n" +
                   "Unit Price:  " + price.ToString("C");
        }
    }
}

public class Program
{
    static int Main()
    {
        Catalog cat = new Catalog();

        Console.WriteLine("Item Description");
        Console.WriteLine(cat[220685, "Women Floral Silk Tank Blouse", 50.00]);

        Console.WriteLine();
        return 0;
    }
}

Overloading an Indexed Property

An indexer borrows various characteristics of a method. One of them is the ability to create various indexers in the same class but all of them must have the same name: this. Still, the various indexers of a class can return the same type of value. Because of this, when creating the indexers, you must find a way to distinguish them. One way you can do this, as seen with method overloading, consists of passing a different type of parameter to each indexer. This is referred to as overloading.

To overload the this property, if two indexed properties take only one parameter, each must take a different (data) type of parameter than the other. Here is an example:

using System;

public class StudentIdentifications
{
    int[] studentIDs;
    string[] fullnames;

    // This property takes a student ID, as an integer,
    // and it produces his/her name
    public string this[int id]
    {
        get
        {
            for (int i = 0; i < studentIDs.Length; i++)
                if (id == studentIDs[i])
                    return fullnames[i];

            return "Unknown Student";
        }
    }

    // This property takes a student name, as a string,
    // and it produces his/her student ID
    public int this[string name]
    {
        get
        {
            for (int i = 0; i < fullnames.Length; i++)
                if (name == fullnames[i])
                    return studentIDs[i];

            return 0;
        }
    }

    public StudentIdentifications()
    {
        studentIDs = new int[6];
        studentIDs[0] = 39472;
        studentIDs[1] = 13957;
        studentIDs[2] = 73957;
        studentIDs[3] = 97003;
        studentIDs[4] = 28947;
        studentIDs[5] = 97395;

        fullnames = new string[6];
        fullnames[0] = "Paul Bertrand Yamaguchi";
        fullnames[1] = "Ernestine Ngovayang";
        fullnames[2] = "Patricia L Katts";
        fullnames[3] = "Helene Mukoko";
        fullnames[4] = "Joan Ursula Hancock";
        fullnames[5] = "Arlette Mimosa";
    }
}

public class Program
{
    static int Main()
    {
        StudentIdentifications std = new StudentIdentifications();

        Console.WriteLine("Student Identification");
        Console.WriteLine("Student ID: 39472");
        Console.WriteLine("Full Name:  {0}\n", std[39472]);

        Console.WriteLine("Student Identification");
        Console.WriteLine("Full Name:  Joan Ursula Hancock");
        Console.WriteLine("Student ID: {0}\n", std["Joan Ursula Hancock"]);

        return 0;
    }
}

This would produce:

Student Identification
Student ID: 39472
Full Name:  Paul Bertrand Yamaguchi

Student Identification
Full Name:  Joan Ursula Hancock
Student ID: 28947

Press any key to continue . . .

An indexer combines the features of an array and those of a method that takes one or more parameters. As an array, an indexer can use one or more dimensions as we have seen so far. Borrowing the features of a method, an indexer can take one or more parameters and it can return a value. Besides passing different types of parameters to various indexers, you can create some of them that take more than one parameter. Here is an example:

using System;

public class Catalog
{
    //double[] item;
    long[] nbrs;
    string[] names;
    double[] prices;

    // This property produces the name of an item, as a string,
    // if it is given the item #, as a number
    public string this[long nbr]
    {
        get
        {
            for (int i = 0; i < nbrs.Length; i++)
                if (nbr == nbrs[i])
                    return names[i];

            return "Unknown Item";
        }
    }

    // This property produces the price of the item, as a number,
    // if it is given the item name and its number
    public double this[string name, long nbr]
    {
        get
        {
            for (int i = 0; i < 5; i++)
                if ((nbr == nbrs[i]) && (name == names[i]))
                    return prices[i];

            return 0.00;
        }
    }

    public Catalog()
    {
        nbrs = new long[5];
        nbrs[0] = 273974;
        nbrs[1] = 539759;
        nbrs[2] = 710234;
        nbrs[3] = 220685;
        nbrs[4] = 192837;

        names = new string[5];
        names[0] = "Women Double-faced wool coat";
        names[1] = "Men Cotton Polo Shirt";
        names[2] = "Children Cable-knit Sweater";
        names[3] = "Women Floral Silk Tank Blouse";
        names[4] = "Girls Jeans with Heart Belt";

        prices = new double[5];
        prices[0] = 275.25;
        prices[1] = 18.75;
        prices[2] = 50.00;
        prices[3] = 65.35;
        prices[4] = 25.55;
    }
}

public class Program
{
    static int Main()
    {
        Catalog cat = new Catalog();

        Console.WriteLine("Item Identification");
        Console.WriteLine("Item #:      539759");
        Console.WriteLine("Unit Price:  {0}\n", cat[539759]);

        Console.WriteLine("Item Identification");
        Console.WriteLine("Item #:      192837");
        Console.WriteLine("Description: Girls Jeans with Heart Belt");
        Console.WriteLine("Unit Price:  {0}\n",
            cat["Girls Jeans with Heart Belt", 192837]);

        return 0;
    }
}	
This would produce:
Item Identification
Item #:      539759
Unit Price:  Men Cotton Polo Shirt

Item Identification
Item #:      192837
Description: Girls Jeans with Heart Belt
Unit Price:  25.55

Press any key to continue . . .

 

 

Previous Copyright © 2007-2012 FunctionX Next