Fundamentals of Classes and Indexed Properties |
|
We learned to create and use indexed properties that were taking parameters of primitive types. Just as we did with primitive types, you can create an indexed property that is of a class type. For example, you can create a class so that one of the its member variables declared as an array can be accessed with an index directly applied to an instance of the class. |
Before designing an indexed property that is class-based, first create the class that will be used as the data type. The class can be simple or complex as you judge it necessary. Here is an example of a simple class: public ref class CStudent { public: String ^ FirstName; String ^ LastName; int Gender; }; When creating the class that will host the indexed property, declare an array member variable for the class. Then, create the default property with the desired accessor(s). Here is an example: public ref class CStudent { public: String ^ FirstName; String ^ LastName; int Gender; }; public ref class CSchoolRegistration { array<CStudent ^> ^ std; public: property CStudent ^ default[int] { CStudent ^ get(int i) { return std[i]; } } CSchoolRegistration() { std = gcnew array<CStudent ^>(5); } }; After creating the indexing class, you can use it and access the indexed property; for example, you can retrieve its value(s). Here is an example: using namespace System; public ref class CStudent { public: String ^ FirstName; String ^ LastName; int Gender; }; public ref class CSchoolRegistration { array<CStudent ^> ^ std; public: property CStudent ^ default[int] { CStudent ^ get(int i) { return std[i]; } } CSchoolRegistration() { std = gcnew array<CStudent ^>(5); std[0] = gcnew CStudent; std[0]->FirstName = L"Alfredo"; std[0]->LastName = L"Olmos"; std[0]->Gender = 2; std[1] = gcnew CStudent; std[1]->FirstName = L"Patricia"; std[1]->LastName = L"Katts"; std[1]->Gender = 1; std[2] = gcnew CStudent; std[2]->FirstName = L"Josiane"; std[2]->LastName = L"Euler"; std[2]->Gender = 1; std[3] = gcnew CStudent; std[3]->FirstName = L"Joan"; std[3]->LastName = L"Jones"; std[3]->Gender = 3; std[4] = gcnew CStudent; std[4]->FirstName = L"George"; std[4]->LastName = L"Paulson"; std[4]->Gender = 2; } }; int main() { CSchoolRegistration ^ pupils = gcnew CSchoolRegistration; for (int i = 0; i < 5; i++) { CStudent ^ pupil = pupils[i]; Console::WriteLine(L"Student Information"); Console::WriteLine(L"---------------------"); Console::WriteLine(L"First Name: {0}", pupil->FirstName); Console::WriteLine(L"Last Name: {0}", pupil->LastName); Console::WriteLine(L"Gender: {0}\n", (pupil->Gender == 1 ? "Female" : (pupil->Gender == 2 ? "Male" : "Unknown"))); } Console::WriteLine(); return 0; } This would produce: Student Information --------------------- First Name: Alfredo Last Name: Olmos Gender: Male Student Information --------------------- First Name: Patricia Last Name: Katts Gender: Female Student Information --------------------- First Name: Josiane Last Name: Euler Gender: Female Student Information --------------------- First Name: Joan Last Name: Jones Gender: Unknown Student Information --------------------- First Name: George Last Name: Paulson Gender: Male Press any key to continue . . .
The above implementation of the CSchoolRegistration class easily allowed us to locate an element of the array by specifying its integer-based index. As done for primitive types, an indexed property can take a parameter other than an integer. In some cases, you may use your class or a class created by someone else and need to access an element of the array without information other than its index. Consider the following program: public ref class CStudent { }; public ref class CSchoolRegistration { array<CStudent ^> ^ students; public: property CStudent ^ default[...] { } CSchoolRegistration() { students = gcnew array<CStudent ^>(50); } }; Previously, we saw that you could create an indexed property that took a type other than an integer. For example, we saw that a string could be used as an index. By now, we know that a basic indexed property produces (or all the indexed properties we have studied so far produce) only one value. If you have a class that has only one member variable, this would be enough. In reality, most of the time, a class has many member variables. In such a case, when you create an indexed property , you need to be able to refer to one exact element of the array. To make this possible, you must define a way to point to the particular element you want. One way you can do this is to use one member variable of the class as a reference. This is better if that member variable holds unique values among the other elements of the array. For our CStudent class, we could use the StudentID member variable (because we will make sure that each student has a unique ID). You can start the property as follows: public ref class CSchoolRegistration { public: property CStudent ^ default[long] { } }; When a user uses this property, he or she must provide a value that uniquely identifies an element of the array. You in turn, when you get this value, you can search for it in the array. If you find it and the array has a get accessor, you can then return the desired but appropriate value. Here is how this can be done: public ref class CSchoolRegistration { array<CStudent ^> ^ students; public: property CStudent ^ default[long] { CStudent ^ get(long id) { for (int i = 0; i < students->Length; i++) { if (students[i]->StudentID == id) return students[i]; } // Unknown student or the number was not found return nullptr; } } CSchoolRegistration() { students = gcnew array<CStudent ^>(50); } }; After creating the indexed property, you can use it. Once again, you must follow the rules of a method that takes an argument and returns a value other than void. In this case, the indexed property must take a string and it must return a CStudent object. Here is an example: using namespace System; public enum Classification { Female, Male, Unknown }; public ref class CStudent { public: long StudentID; String ^ FirstName; String ^ LastName; Classification Gender; virtual String ^ ToString() override { String ^ str = String::Concat(L"Student ID: ", StudentID.ToString(), L"\nFirst Name: ", FirstName, L"\nLast Name: ", LastName, L"\nGender: ", Gender); return str; } }; public ref class CSchoolRegistration { array<CStudent ^> ^ students; public: property CStudent ^ default[long] { CStudent ^ get(long id) { for (int i = 0; i < students->Length; i++) { if (students[i]->StudentID == id) return students[i]; } // Unknown student or the number was not found return nullptr; } } CSchoolRegistration() { students = gcnew array<CStudent ^>(50); students[0] = gcnew CStudent; students[0]->StudentID = 917294; students[0]->FirstName = L"Helene"; students[0]->LastName = L"Mukoko"; students[0]->Gender = Female; students[1] = gcnew CStudent; students[1]->StudentID = 283764; students[1]->FirstName = L"Patrice"; students[1]->LastName = L"Katts"; students[1]->Gender = Unknown; students[2] = gcnew CStudent; students[2]->StudentID = 192046; students[2]->FirstName = L"Armand"; students[2]->LastName = L"Essono"; students[2]->Gender = Male; students[3] = gcnew CStudent; students[3]->StudentID = 618268; students[3]->FirstName = L"Bertrand"; students[3]->LastName = L"Yamaguchi"; students[3]->Gender = Male; students[4] = gcnew CStudent; students[4]->StudentID = 820648; students[4]->FirstName = L"Hortense"; students[4]->LastName = L"McNeal"; students[4]->Gender = Female; } }; int main() { CSchoolRegistration ^ pupils = gcnew CSchoolRegistration; CStudent ^ pupil = pupils[820648]; Console::WriteLine(L"Student Information"); Console::WriteLine(L"---------------------"); Console::WriteLine(L"First Name: {0}", pupil->FirstName); Console::WriteLine(L"Last Name: {0}", pupil->LastName); Console::Write(L"Gender: "); Console::WriteLine(pupil->Gender); pupil = pupils[192046]; Console::WriteLine(L"Student Information"); Console::WriteLine(L"---------------------"); Console::WriteLine(L"First Name: {0}", pupil->FirstName); Console::WriteLine(L"Last Name: {0}", pupil->LastName); Console::Write(L"Gender: "); Console::WriteLine(pupil->Gender); return 0; } This would produce: Student Information --------------------- First Name: Hortense Last Name: McNeal Gender: Female Student Information --------------------- First Name: Armand Last Name: Essono Gender: Male Press any key to continue . . .
|
|
||
Home | Copyright © 2007-2013, FunctionX | Next |
|