A Property Can Be Indexed

Introduction

We already know how to create an array, how to assign values to its elements, and how to get the value of each element. Here is an example:

using static System.Console;

public class Exercise
{
    public static void Main()
    {
        double[] numbers = new double[5];

        numbers[0] = 927.93;
        numbers[1] = 45.155;
        numbers[2] = 2.37094;
        numbers[3] = 73475.25;
        numbers[4] = 186.72;

        for (int i = 0; i < numbers.Length; i++)
            WriteLine("Number {0}: {1}", i + 1, numbers[i]);

        WriteLine("============================");
    }
}

This would produce:

Number 1: 927,93
Number 2: 45,155
Number 3: 2,37094
Number 4: 73475,25
Number 5: 186,72

Press any key to continue . . .

In the same way, if we declared an array as a member variable of a class, to access the elements of that member, we had to use an instance of the class, followed by the period operator, followed by the member variable applied with the square brackets. Instead of accessing each element through its member variable, you can create a type of property referred to as an indexer.

Practical LearningPractical Learning: Introducing Indexed Properties

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New -> Project...
  3. In the middle list of the dialog box, click Console App (.NET Framework).
    Change the Name to PayrollPreparation6
  4. Click OK
  5. On the main menu, right-click PayrollPreparation6 -> Add -> Class...
  6. Type Employee as the name of the class
  7. Press Enter
  8. Change the file as follows:
    namespace PayrollPreparation6
    {
        public class Employee
        {
            public long   EmployeeNumber { get; set; }
            public string FirstName      { get; set; }
            public string LastName       { get; set; }
            public double HourlySalary   { get; set; }
        }
    }
  9. To create a new class, on the main menu, click Project -> Add Class...
  10. Type StaffManagement
  11. Press Enter
  12. Change the file as follows:
    namespace PayrollPreparation6
    {
        public class StaffManagement
        {
            public Employee[] pers;
    
            public StaffManagement()
            {
                pers = new Employee[5];
    
                pers[0] = new Employee();
                pers[0].EmployeeNumber = 994_759;
                pers[0].FirstName = "Michael    ";
                pers[0].LastName  = "Grant      ";
                pers[0].HourlySalary = 22.58;
    
                pers[1] = new Employee();
                pers[1].EmployeeNumber = 394_225;
                pers[1].FirstName = "Veronique  ";
                pers[1].LastName  = "Beatten    ";
                pers[1].HourlySalary = 28.07;
    
                pers[2] = new Employee();
                pers[2].EmployeeNumber = 208_249;
                pers[2].FirstName = "George     ";
                pers[2].LastName  = "Kroll      ";
                pers[2].HourlySalary = 17.96;
    
                pers[3] = new Employee();
                pers[3].EmployeeNumber = 592_740;
                pers[3].FirstName = "Maurissette";
                pers[3].LastName  = "Phukan     ";
                pers[3].HourlySalary = 30.05;
    
                pers[4] = new Employee();
                pers[4].EmployeeNumber = 379_725;
                pers[4].FirstName = "Douglas    ";
                pers[4].LastName  = "Sorrel     ";
                pers[4].HourlySalary = 26.37;
            }
        }
    }
  13. In the Solution Explorer, right-click Program.cs and click Rename
  14. Type PayrollPreparation to get PayrollPreparation.cs, and press Enter
  15. In the Solution Explorer, double-click PayrollPreparation.cs to access it and change it as follows:
    using static System.Console;
    
    namespace PayrollPreparation5
    {
        public class PayrollManagement
        {
            public static int Main(string[] args)
            {
                StaffManagement staff = new StaffManagement();
    
                WriteLine("     Human Resources - Employees");
                WriteLine("===========================================");
                WriteLine("Empl #     First Name    Last Name  Salary");
                WriteLine("-------------------------------------------");
                for (int i = 0; i < 5; i++)
                    WriteLine(" {0}    {1}   {2}{3}",
                        staff.pers[i].EmployeeNumber, staff.pers[i].FirstName,
                        staff.pers[i].LastName, staff.pers[i].HourlySalary);
                WriteLine("===========================================");
    
                return 0;
            }
        }
    }
  16. Press Ctrl + F5 to execute the application. This would produce:
         Human Resources - Employees
    ===========================================
    Empl #     First Name    Last Name  Salary
    -------------------------------------------
     994759    Michael       Grant      22.58
     394225    Veronique     Beatten    28.07
     208249    George        Kroll      17.96
     592740    Maurissette   Phukan     30.05
     379725    Douglas       Sorrel     26.37
    ===========================================
    Press any key to continue . . .
  17. Press Enter to close the DOS window and return to your programming environment

An Indexed Property

An indexed property, also called an indexer, is a class's property that allows you to access a member variable of a class using the features of an array. To create an indexed property, start the class like any other. In the body of the class, create a field that is an array. Here is an example:

public class Number
{
    double[] numbers = new double[5];
}

Then, in the body of the class, create a property named this with its accessor(s). The this property must be the same type as the field to which it will refer. The property must take a parameter as an array. This means that it must have square brackets. Inside the brackets, include the parameter you will use as index to access the members of the array.

Traditionally, and as we have seen so far, you usually access the members of an array using an integer-based index. Therefore, you can use an int type as the index of the array. Of course, the index's parameter must have a name, such as i. This can be done as follows:

public class Number
{
    double[] numbers = new double[5];

    public double this[int i]
    {
    }
}

Primary Characteristics of Indexed Properties

A Read-Only Indexed Property

If you want the property to be read-only, include only a get accessor. In the get accessor, you should return an element of the array field to which the property refers, using the parameter of the property. This would be done as follows:

public class Number
{
    double[] numbers = new double[5];

    public double this[int i]
    {
        get { return numbers[i]; }
    }
}

Once you have created the indexed property, the class can be used. To start, you can declare a variable of the class. To access its arrayed field, you can apply the square brackets directly to it. Here is an example:

using static System.Console;

public class Number
{
    double[] numbers;

    public double this[int i]
    {
        get { return numbers[i]; }
    }

    public Number()
    {
        numbers = new double[5];
        numbers[0] = 927.93;
        numbers[1] = 45.155;
        numbers[2] = 2.37094;
        numbers[3] = 73475.25;
        numbers[4] = 186.72;
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var nbr = new Number();

        for (int i = 0; i < 5; i++)
            WriteLine("Number {0}: {1}", i + 1, nbr[i]);

        return 0;
    }
}

Based on this, a type of formula to create and use a basic indexed property is:

class class-name
{
    data-type[] array-name = new data-type[length];

    public data-type this[int i]
    {
        get { return array-name[i]; }
    }
}

A String-Based Indexed Property

In the above example, we created a property that produced double-precision values. When creating an indexed property, you will decide what type of value the property must produce or the type it can have. As opposed to an int or a double, you can also create a property that takes or produces a string. To do this, you can use the above class template with the desired data type, such as string. Here is an example of a string-based indexed property:

using System;

public class Philosopher
{
    string[] phil = new string[8];

    public string this[int i]
    {
        get { return phil[i]; }
    }

    public Philosopher()
    {
        phil[0] = "Aristotle";
        phil[1] = "Emmanuel Kant";
        phil[2] = "Tom Huffman";
        phil[3] = "Judith Jarvis Thompson";
        phil[4] = "Thomas Hobbes";
        phil[5] = "Cornell West";
        phil[6] = "Jane English";
        phil[7] = "James Rachels";
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var thinker = new Philosopher();

        for (int i = 0; i < 8; i++)
            Console.WriteLine("Philosopher: {0}", thinker[i]);

        Console.WriteLine("====================================");
        return 0;
    }
}

This would produce:

Philosopher: Aristotle
Philosopher: Emmanuel Kant
Philosopher: Tom Huffman
Philosopher: Judith Jarvis Thompson
Philosopher: Thomas Hobbes
Philosopher: Cornell West
Philosopher: Jane English
Philosopher: James Rachels
====================================
Press any key to continue . . .

A Boolean Indexed Property

You can create a Boolean-based indexed property by simply making it return a bool type. Here is an example:

using System;

public class DrivingWhileIntoxicated
{
    bool[] dwi = new bool[7];

    public bool this[int i]
    {
        get { return dwi[i]; }
    }

    public DrivingWhileIntoxicated()
    {
        dwi[0] = false;
        dwi[1] = true;
        dwi[2] = true;
        dwi[3] = false;
        dwi[5] = false;
        dwi[6] = false;
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var driving = new DrivingWhileIntoxicated();

        Console.WriteLine("Police Report");
        Console.WriteLine("-------------------------------");
        for(int i = 0; i < 7; i++)
            Console.WriteLine("Driver Was Intoxicated: {0}", driving[i]);

        Console.WriteLine("==================================");
        return 0;
    }
}

This would produce:

Police Report
-------------------------------
Driver Was Intoxicated: False
Driver Was Intoxicated: True
Driver Was Intoxicated: True
Driver Was Intoxicated: False
Driver Was Intoxicated: False
Driver Was Intoxicated: False
Driver Was Intoxicated: False
==================================
Press any key to continue . . .

A Floating-Point Number Indexed Property

We know how to create different arrays that are numeric or string based. Here is an example of a float array:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        float[] ages = new float[5];

        ages[0] = 14.50f;
        ages[1] = 12.00f;
        ages[2] = 16.50f;
        ages[3] = 14.00f;
        ages[4] = 15.50f;

        Console.WriteLine("Student Age: {0}", ages[2]);
        
        return 0;
    }
}

When we think of arrays, we usually consider passing an integer-based parameter to the square brackets of the variable, as done for the above ages array:

float[] ages = new float[5];

When using an indexed property, you can use almost any type of index, such as a real value or a string. To do this, in the square brackets of the this property, pass the desired type as the index. Here is an example:

public class StudentAge
{
    public float this[string name]
    {
    }
}

When defining the indexed property, there are two rules you must follow and you are aware of them already because an indexed property is like a method that takes a parameter and doesn't return void. Therefore, when implementing an indexed property, make sure you return the right type of value and make sure you pass the appropriate index to the return value of the this property. Here is an example:

public class StudentAge
{
    public float this[string name]
    {
        get
        {
            if(  name == "Ernestine Jonas" )
                return 14.50f;
            else if( name == "Paul Bertrand Yamaguchi" )
                return 12.50f;
            else if( name == "Helene Jonas" )
                return 16.00f;
            else if( name == "Chrissie Hanson" )
                return 14.00f;
            else if( name == "Bernard Hallo" )
                return 15.50f;
            else
                return 12.00f;
        }
    }
}

Once you have defined the property, you can use it. To access any of its elements, you must pass the appropriate type of index. In this case, the index must be passed as a string and not an integer. You can then do whatever you want with the value produced by the property. For example, you can display it to the user. Here is an example:

using System;

public class StudentAge
{
    public float this[string name]
    {
        get
        {
            if(  name == "Ernestine Jonas" )
                return 14.50f;
            else if( name == "Paul Bertrand Yamaguchi" )
                return 12.50f;
            else if( name == "Helene Jonas" )
                return 16.00f;
            else if( name == "Chrissie Hanson" )
                return 14.00f;
            else if( name == "Bernard Hallo" )
                return 15.50f;
            else
                return 12.00f;
        }
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var sa = new StudentAge();
        var age = sa["Paul Bertrand Yamaguchi"];

        Console.WriteLine("Student Age: {0}", age);

        Console.WriteLine("==================================");
        return 0;
    }
}

This would produce:

Student Age: 12.5
==================================
Press any key to continue . . .

An Enumeration-Based Indexed Property

You can create an indexed Property that is based on an enumeration. Such a property uses an enumeration as its index. To do this, after defining the enumeration, type its name and a parameter name in the square brackets of the this member, then define the property as you see fit. To access the property outside, apply an enumeration member to the square brackets on an instance of the class. Here is an example:

using System;

public enum CategoryFee
{
    Children,
    Adult,
    Senior,
    Unknown
}

public class GolfClubMembership
{
    double[] fee = new double[4];

    public GolfClubMembership()
    {
        fee[0] = 150.95d;
        fee[1] = 250.75d;
        fee[2] = 85.65d;
        fee[3] = 350.00d;
    }

    public double this[CategoryFee cat]
    {
        get
        {
            if (cat == CategoryFee.Children)
                return fee[0];
            else if (cat == CategoryFee.Adult)
                return fee[1];
            else if (cat == CategoryFee.Senior)
                return fee[2];
            else
                return fee[3];
        }
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var mbr = new GolfClubMembership();

        Console.WriteLine("Membership Fee: {0}", mbr[CategoryFee.Senior]);

        Console.WriteLine("===============================");
        return 0;
    }
}

This would produce:

Membership Fee: 85.65
===============================
Press any key to continue . . .

Practical LearningPractical Learning: Creating an Indexer

  1. To create an indexer, access the StaffManagement.cs file and change it as follows:
    namespace PayrollPreparation5
    {
        public class StaffManagement
        {
            public Employee[] pers;
    
            public StaffManagement()
            {
                pers = new Employee[5];
    
                pers[0] = new Employee();
                pers[0].EmployeeNumber = 994_759;
                pers[0].FirstName = "Michael    ";
                pers[0].LastName  = "Grant      ";
                pers[0].HourlySalary = 22.58;
    
                pers[1] = new Employee();
                pers[1].EmployeeNumber = 394_225;
                pers[1].FirstName = "Veronique  ";
                pers[1].LastName  = "Beatten    ";
                pers[1].HourlySalary = 28.07;
    
                pers[2] = new Employee();
                pers[2].EmployeeNumber = 208_249;
                pers[2].FirstName = "George     ";
                pers[2].LastName  = "Kroll      ";
                pers[2].HourlySalary = 17.96;
    
                pers[3] = new Employee();
                pers[3].EmployeeNumber = 592_740;
                pers[3].FirstName = "Maurissette";
                pers[3].LastName  = "Phukan     ";
                pers[3].HourlySalary = 30.05;
    
                pers[4] = new Employee();
                pers[4].EmployeeNumber = 379_725;
                pers[4].FirstName = "Douglas    ";
                pers[4].LastName  = "Sorrel     ";
                pers[4].HourlySalary = 26.37;
            }
    
            public string this[long code]
            {
                get
                {
                    for (int i = 0; i < pers.Length; i++)
                        if (code == pers[i].EmployeeNumber)
                            return "Employee #:    " + pers[i].EmployeeNumber +
                                   "\nFirst Name:    " + pers[i].FirstName +
                                   "\nLast Name:     " + pers[i].LastName +
                                   "\nHourly Salary: " + pers[i].HourlySalary.ToString("F");
                    return "Unidentifiable Employee";
                }
            }
        }
    }
  2. Access the PayrollPreparation.cs document and change it as follows:
    using static System.Console;
    
    namespace PayrollPreparation5
    {
        public class PayrollManagement
        {
            public static int Main(string[] args)
            {
                long emplNbr = 0L;
                StaffManagement staff = new StaffManagement();
    
                WriteLine("     Human Resources - Employees");
                WriteLine("===========================================");
                WriteLine("Empl #     First Name    Last Name  Salary");
                WriteLine("-------------------------------------------");
                for (int i = 0; i < 5; i++)
                    WriteLine(" {0}    {1}   {2}{3}",
                        staff.pers[i].EmployeeNumber, staff.pers[i].FirstName,
                        staff.pers[i].LastName, staff.pers[i].HourlySalary);
                WriteLine("===========================================");
    
                try
                {
                    Write("Enter an employee #: ");
                    emplNbr = long.Parse(ReadLine());
    
                    WriteLine("======================================");
                    WriteLine("Employee Details");
                    WriteLine("--------------------------------------");
                    WriteLine(staff[emplNbr]);
                    WriteLine("======================================");
    
                }
                catch (System.FormatException)
                {
                    WriteLine("=- Invalid Employee Number -=");
                }
    
                return 0;
            }
        }
    }
  3. Press Ctrl + F5 to execute the application
  4. At the prompt, type 592740
         Human Resources - Employees
    ===========================================
    Empl #     First Name    Last Name  Salary
    -------------------------------------------
     994759    Michael       Grant      22.58
     394225    Veronique     Beatten    28.07
     208249    George        Kroll      17.96
     592740    Maurissette   Phukan     30.05
     379725    Douglas       Sorrel     26.37
    ===========================================
    Enter an employee #: 592740
  5. Press Enter
  6.      Human Resources - Employees
    ===========================================
    Empl #     First Name    Last Name  Salary
    -------------------------------------------
     994759    Michael       Grant      22.58
     394225    Veronique     Beatten    28.07
     208249    George        Kroll      17.96
     592740    Maurissette   Phukan     30.05
     379725    Douglas       Sorrel     26.37
    ===========================================
    Enter an employee #: 592740
    ======================================
    Employee Details
    --------------------------------------
    Employee #:    592740
    First Name:    Maurissette
    Last Name:     Phukan
    Hourly Salary: 30.05
    ======================================
    Press any key to continue . . .
  7. Press Enter to close the DOS window and return to your programming environment

Topics on Indexed Properties

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. 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 Exercise
{
    static int Main(string[] args)
    {
        var 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 Exercise
{
    static int Main(string[] args)
    {
        var cat = new Catalog();

        var itemNumber = 539759;
        var itemDescription = "Men Cotton Polo Shirt";
        var 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 Exercise
{
    static int Main(string[] args)
    {
        var cat = new Catalog();

        var 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 Exercise
{
    static int Main(string[] args)
    {
        var 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.

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 Exercise
{
    static int Main(string[] args)
    {
        var 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. 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 Exercise
{
    static int Main(string[] args)
    {
        var 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 . . .

Read/Write Indexed Properties

Introduction

So far, we have purposely used indexed properties that only produced a value. This type is used when you are (always) in charge of specifying the values of the elements of the array: the only action you would expect from the user is to retrieve the value of the property. This is referred to as a read-only property. In some cases, you will use a property created by someone else. You may want to specify the value of an element of the array. In the same way, you may create an indexed property and you want the users of that property to be able to specify the value of the array.

If you want an indexed property to be read/write, besides the get accessor as we have been using it so far, you should also include a set accessor.

A Read/Write Property of a Primitive Type

To create a read/write indexed property, you should include a set accessor for the property. In the set accessor, assign the value contextual keyword to the field indexed with the this parameter. Here is an example of a read/write indexed property that includes a set accessor:

public class Number
{
    double[] numbers;

    public double this[int i]
    {
        get { return numbers[i]; }
        set { numbers[i] = value; }
    }
}

After creating the read/write property, you can assign its values outside the class. In other words, clients of the class can change the values of its elements. Remember that the advantage of an indexed property is that each element of the arrayed field can be accessed from the instance of the class by directly applying the square brackets and the (appropriate) index to it. Here is an example:

using System;

public class Number
{
    double[] numbers = new double[5];

    public double this[int i]
    {
        get { return numbers[i]; }
        set { numbers[i] = value; }
    }
}

public class Program
{
    static int Main(string[] args)
    {
        var nbr = new Number();
        
        nbr[2] = 2.37094;

        for (int i = 0; i < 5; i++)
            Console.WriteLine("Number {0}: {1}", i + 1, nbr[i]);

        Console.WriteLine();
        return 0;
    }
}

This would produce:

Number 1: 0
Number 2: 0
Number 3: 2.37094
Number 4: 0
Number 5: 0

Press any key to continue . . .

Based on this, a type of formula to create and use a basic indexed property is:

class class-name
{
    data-type[] array-name = new data-type[Index];

    public data-type this[int i]
    {
        get { return array-name[i]; }
        set { array-name[i] = value; }
    }
}

class Program
{
    static int Main(string[] args)
    {
        class-name Variable = new class-name();

        Variable[Low-Index] = Value1;
        . . .
        Variable[High-Index] = Value_n;

        return 0;
    }
}

We saw that the index of a property could be a value other than an integer. For example, we created an index that was a string type. For such a property, if you make it read/write, you can assign its values outside the class. Here is an example of a read/write string-based indexed property:

using System;

public class Philosopher
{
    string[] phil = new string[8];

    public string this[int i]
    {
        get { return phil[i]; }
        set { phil[i] = value; }
    }
}

public class Program
{
    static int Main(string[] args)
    {
        var thinker = new Philosopher();

        thinker[5] = "Stuart Rachels";

        for (int i = 0; i < 8; i++)
            Console.WriteLine("Philosopher: {0}", thinker[i]);

        Console.WriteLine();
        return 0;
    }
}

This would produce:

Philosopher:
Philosopher:
Philosopher:
Philosopher:
Philosopher:
Philosopher: Stuart Rachels
Philosopher:
Philosopher:

Press any key to continue . . .

The same rules would apply to a read/write indexed property that can receive Boolean or decimal values.


Previous Copyright © 2008-2019, FunctionX Next