Introduction to Indexed Properties |
|
A Property Can Be Indexed |
Introduction |
We learned 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 namespace System; int main() { array<double> ^ Numbers = gcnew array<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++) Console::WriteLine(L"Number {0}: {1}", i+1, Numbers[i]); Console::WriteLine(); return 0; } |
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.
An indexer, also called an indexed property, 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 member variable that is an array and initialize it appropriately. Here is an example: public ref class CNumber { array<double> ^ Numbers; public: CNumber() { Numbers = gcnew array<double>(5); } }; Then, in the body of the class, create a property named default with its accessor(s). The default property must be the same type as the member variable it will refer to. The property must take a parameter as an array. This means that it must have square brackets. Inside of the brackets, include the parameter type 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 but the index' parameter must not have a name. This would be done as follows: public ref class CNumber { array<double> ^ Numbers = gcnew double[5]; public: property double default[int] { } } If you want the property to be read-only, include only a get accessor. When creating the get accessor, you must pass a parameter to it. The parameter must be the same type you passed to the default property, except that, as mentioned above, the parameter passed to default must not have a name but the parameter passed to get must have a name. When implementing the get accessor, it must return an element of the array member variable the property refers to, using the parameter passed to get. This would be done as follows: public ref class CNumber { array<double> ^ Numbers; public: CNumber() { Numbers = gcnew array<double>(5); } property double default[int] { double get(int i) { return Numbers[i]; } } }; Once you have created the indexed property, the class can be used. To start, you can declare a handle of the class. To access its arrayed member variable, you can apply the square brackets directly to it. Here is an example: using namespace System; public ref class CNumber { array<double> ^ Numbers; public: CNumber() { Numbers = gcnew array<double>(5); Numbers[0] = 927.93; Numbers[1] = 45.155; Numbers[2] = 2.37094; Numbers[3] = 73475.25; Numbers[4] = 186.72; } property double default[int] { double get(int i) { return Numbers[i]; } } }; int main() { CNumber ^ nbr = gcnew CNumber; for(int i = 0; i < 5; i++) Console::WriteLine(L"Number {0}: {1}", i + 1, nbr[i]); Console::WriteLine(); return 0; }
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. Here is an example of a 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]; } } CPhilosopher() { phil = gcnew array<String ^>(8); phil[0] = L"Aristotle"; phil[1] = L"Emmanuel Kant"; phil[2] = L"Tom Huffman"; phil[3] = L"Judith Jarvis Thompson"; phil[4] = L"Thomas Hobbes"; phil[5] = L"Cornell West"; phil[6] = L"Jane English"; phil[7] = L"James Rachels"; } }; int main() { CPhilosopher ^ thinker = gcnew CPhilosopher; for(int i = 0; i < 8; i++) Console::WriteLine(L"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 . . . In the same way, you can created a Boolean-based indexed property by simply making it return a bool type. Here is an example: using namespace System; public ref class CDrivingWhileIntoxicated { array<bool>^ dwi; public: property bool default[int] { bool get(int i) { return dwi[i]; } } CDrivingWhileIntoxicated() { dwi = gcnew array<bool>(7); dwi[0] = false; dwi[1] = true; dwi[2] = true; dwi[3] = false; dwi[5] = false; dwi[6] = false; } }; int main() { CDrivingWhileIntoxicated ^ driving = gcnew CDrivingWhileIntoxicated; Console::WriteLine(L"Police Report"); Console::WriteLine(L"-------------------------------"); for(int i = 0; i < 7; i++) Console::WriteLine(L"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 . . .
In Lesson 25, we saw how to create different arrays that are numeric or string based. Here is an example of a float array: using namespace System; int main() { array<float>^ ages = gcnew array<float>(5); ages[0] = 14.50f; ages[1] = 12.00f; ages[2] = 16.50f; ages[3] = 14.00f; ages[4] = 15.50f; Console::WriteLine(L"Student Age: {0}", ages[2]); Console::WriteLine(); 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: array<float>^ ages = gcnew array<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 default property, pass the desired type as the index. Here is an example: public ref class CStudentAge { property float default[string] { } }; 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 in the get accessor and make sure you pass the appropriate index to the return value of the default property. Here is an example: public ref class CStudentAge { public: property float default[String ^] { float get(String ^ name) { if( name == L"Ernestine Jonas" ) return 14.50f; else if( name == L"Paul Bertrand Yamaguchi" ) return 12.50f; else if( name == L"Helene Jonas" ) return 16.00f; else if( name == L"Chrissie Hanson" ) return 14.00f; else if( name == L"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 namespace System; public ref class CStudentAge { public: property float default[String ^] { float get(String ^ name) { if( name == L"Ernestine Jonas" ) return 14.50f; else if( name == L"Paul Bertrand Yamaguchi" ) return 12.50f; else if( name == L"Helene Jonas" ) return 16.00f; else if( name == L"Chrissie Hanson" ) return 14.00f; else if( name == L"Bernard Hallo" ) return 15.50f; else return 12.00f; } } }; int main() { CStudentAge ^ sa = gcnew CStudentAge; float age = sa[L"Paul Bertrand Yamaguchi"]; Console::WriteLine(L"Student Age: {0}", age); Console::WriteLine(); return 0; } This would produce: Student Age: 12.5 Press any key to continue . . . You can also pass an enumeration as an index. To do this, after defining the enumerator, type its name in the square brackets of the default 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 namespace System; public enum CategoryFee { Children, Adult, Senior, Unknown }; public ref class CGolfClubMembership { array<double> ^ fee; public: CGolfClubMembership() { fee = gcnew array<double>(4); fee[0] = 150.95; fee[1] = 250.75; fee[2] = 85.65; fee[3] = 350.00; } property double default[CategoryFee] { double get(CategoryFee cat) { 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]; } } }; int main() { CGolfClubMembership ^ mbr = gcnew CGolfClubMembership; Console::WriteLine(L"Membership Fee: {0}", mbr[CategoryFee::Senior]); Console::WriteLine(); return 0; } This would produce: Membership Fee: 85.65 Press any key to continue . . .
|
|
||
Home | Copyright © 2007-2013, FunctionX | Next |
|