Home

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 must include a set accessor for the property. When creating the set accessor, pass two parameters. The first should be an integer type that would represent the index of the item. The second parameter is used to represent the value that will be assigned the the property. Here is an example of how this can be done:

public ref class CNumbers
{
    array<double> ^ Nbrs;

public:
    property double default[int]
    {
        double get(int i) { return Nbrs[i]; }
        void set(int i, double value) { Nbrs[i] = value; }
    }

    CNumbers()
    {
	Nbrs = gcnew array<double>(5);
    }
};

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 member variable 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 namespace System;

public ref class CNumbers
{
    array<double> ^ Nbrs;

public:
    property double default[int]
    {
        double get(int i) { return Nbrs[i]; }
        void set(int i, double value) { Nbrs[i] = value; }
    }

    CNumbers()
    {
	Nbrs = gcnew array<double>(5);
    }
};

int main()
{
    CNumbers ^ values = gcnew CNumbers;
        
    values[2] = 249.37094;

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

    Console::WriteLine();
    return 0;
}

This would produce:

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

Press any key to continue . . .

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 namespace System;

public ref class CPhilosopher 
{
    array<String ^> ^ phil;

public:
    property String ^ default[int]
    {
        String ^ get(int i) { return phil[i]; }
        void set(int i, String ^ value) { phil[i] = value; }
    }

    CPhilosopher()
    {
	phil = gcnew array<String ^>(8);
    }
};

int main()
{
    CPhilosopher ^ thinker = gcnew CPhilosopher;

    thinker[5] = L"Stuart Rachels";

    for (int i = 0; i < 8; i++)
            Console::WriteLine(L"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 double values.

 

Previous Copyright © 2007-2012 FunctionX Home