Home

Read/Write Indexers

 

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 of the class. In other words, clients of the class can change the values to 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()
    {
        Number 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 ClassName
{
    DataType[] ArrayName = new DataType[Index];

    public DataType this[int i]
    {
        get { return ArrayName[i]; }
        set { ArrayName[i] = value; }
    }
}

class Program
{
    static int Main()
    {
        ClassName Variable = new ClassName();

        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-based. 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 of 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()
    {
        Philosopher 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 © 2007-2012 FunctionX Home