|
Topics on Indexers and Classes |
|
|
A Class as Index |
|
As opposed to returning a class, an indexer can use a class as its index. When creating such a property, the primary action you must take is to include a class and its name as a parameter to the this property. You can start such a class as follows: using System;
public enum Classification
{
Female,
Male,
Unknown
}
public class Student
{
public long StudentID;
public string FirstName;
public string LastName;
public Classification Gender;
}
public class SchoolRegistration
{
public string this[Student std]
{
}
}
|
|
When implementing the class, you should proceed the same way we have done so far following the rules of a method that takes an argument and returns a value other than void. Here is an example: public class SchoolRegistration
{
Student[] students = new Student[50];
public string this[Student std]
{
get
{
for (int i = 0; i < students.Length; i++)
{
if (std.StudentID == students[i].StudentID)
return "Student ID: " + students[i].StudentID +
"\nFirst Name: " + students[i].FirstName +
"\nLast Name: " + students[i].LastName +
"\nGender: " + students[i].Gender;
}
// Unknown student or the number was not found
return "";
}
}
}
After creating the property, you can use it. To do this, you must pass an object that is the type of the index. You can then use the returned value as you see fit. Here is an example: using System;
public enum Classification
{
Female,
Male,
Unknown
}
public class Student
{
public long StudentID;
public string FirstName;
public string LastName;
public Classification Gender;
}
public class SchoolRegistration
{
Student[] students = new Student[50];
public string this[Student std]
{
get
{
for (int i = 0; i < students.Length; i++)
{
if (std.StudentID == students[i].StudentID)
return "Student ID: " + students[i].StudentID +
"\nFirst Name: " + students[i].FirstName +
"\nLast Name: " + students[i].LastName +
"\nGender: " + students[i].Gender;
}
// Unknown student or the number was not found
return "";
}
}
public SchoolRegistration()
{
students[0] = new Student();
students[0].StudentID = 917294;
students[0].FirstName = "Helene";
students[0].LastName = "Mukoko";
students[0].Gender = Classification.Female;
students[1] = new Student();
students[1].StudentID = 283764;
students[1].FirstName = "Patrice";
students[1].LastName = "Katts";
students[1].Gender = Classification.Unknown;
students[2] = new Student();
students[2].StudentID = 192046;
students[2].FirstName = "Armand";
students[2].LastName = "Essono";
students[2].Gender = Classification.Male;
students[3] = new Student();
students[3].StudentID = 618268;
students[3].FirstName = "Bertrand";
students[3].LastName = "Yamaguchi";
students[3].Gender = Classification.Male;
students[4] = new Student();
students[4].StudentID = 820648;
students[4].FirstName = "Hortense";
students[4].LastName = "McNeal";
students[4].Gender = Classification.Female;
students[5] = new Student();
students[5].StudentID = 917394;
students[5].FirstName = "Alfredo";
students[5].LastName = "Olmos";
students[5].Gender = Classification.Unknown;
students[6] = new Student();
students[6].StudentID = 163864;
students[6].FirstName = "Josiane";
students[6].LastName = "Euler";
students[6].Gender = Classification.Female;
students[7] = new Student();
students[7].StudentID = 826384;
students[7].FirstName = "Joan";
students[7].LastName = "Jones";
students[7].Gender = Classification.Female ;
}
}
public class Program
{
static int Main()
{
SchoolRegistration pupils = new SchoolRegistration();
Student pupil = new Student();
pupil.StudentID = 820648;
string strStudent = pupils[pupil];
Console.WriteLine("=====================");
Console.WriteLine("Student Information");
Console.WriteLine("---------------------");
Console.WriteLine(strStudent);
//pupil = new Student();
pupil.StudentID = 192046;
strStudent = pupils[pupil];
Console.WriteLine("=====================");
Console.WriteLine("Student Information");
Console.WriteLine("---------------------");
Console.WriteLine(strStudent);
Console.WriteLine("=====================\n");
return 0;
}
}
This would produce: ===================== Student Information --------------------- Student ID: 820648 First Name: Hortense Last Name: McNeal Gender: Female ===================== Student Information --------------------- Student ID: 192046 First Name: Armand Last Name: Essono Gender: Male ===================== Press any key to continue . . . You can also directly pass an instance of the class in the square brackets of the object that holds the indexed property, as long as you specify the object. Here is an example: using System;
public enum Classification
{
Female,
Male,
Unknown
}
public class Student
{
public long StudentID;
public string FirstName;
public string LastName;
public Classification Gender;
public Student()
{
}
public Student(long id)
{
this.StudentID = id;
}
}
public class SchoolRegistration
{
Student[] students = new Student[50];
public string this[Student std]
{
. . . No Change
}
public SchoolRegistration()
{
. . . No Change
}
}
public class Program
{
static int Main()
{
SchoolRegistration pupils = new SchoolRegistration();
string strStudent = pupils[new Student(618268)];
Console.WriteLine("=====================");
Console.WriteLine("Student Information");
Console.WriteLine("---------------------");
Console.WriteLine(strStudent);
Console.WriteLine("=====================\n");
return 0;
}
}
This would produce: ===================== Student Information --------------------- Student ID: 618268 First Name: Bertrand Last Name: Yamaguchi Gender: Male ===================== Press any key to continue . . .
As mentioned for indexers that return primitive types, you can overload an indexed property that produces a class. You do this following the same rules applied to method overloading and arrays:
As done for a primitive type, you can allow the clients of your indexer to assign values to the array's elements. Once again, when defining the property, you should include a set accessor to it. In the set accessor, you should assign the value keyword to an element of the array. Here is an example: public class SchoolRegistration
{
Student[] std = new Student[5];
public Student this[int i]
{
get { return std[i]; }
set { std[i] = value; }
}
}
After doing this, you can create an element of the array by applying the square brackets to the instance of the class and assigning the desired value to it. The problem with the class is that, since it may have many fields (or properties), to completely define each element, you must provide a value to the member variables of the class itself. Here is an example: using System;
public enum Classification
{
Female,
Male,
Unknown
}
public class Student
{
public long StudentID;
public string FirstName;
public string LastName;
public Classification Gender;
public override string ToString()
{
string str = "Student ID: " + StudentID +
"\nFirst Name: " + FirstName +
"\nLast Name: " + LastName +
"\nGender: " + Gender;
return str;
}
}
public class SchoolRegistration
{
Student[] std = new Student[5];
public Student this[int i]
{
get { return std[i]; }
set { std[i] = value; }
}
}
public class Program
{
static int Main()
{
SchoolRegistration registration = new SchoolRegistration();
Student stud = new Student();
stud.StudentID = 604057;
stud.FirstName = "Gertrude";
stud.LastName = "Monayong";
stud.Gender = Classification.Female;
registration[2] = stud;
Console.WriteLine("Student Information");
Console.WriteLine("---------------------");
Console.WriteLine("First Name: {0}", registration[2].FirstName);
Console.WriteLine("Last Name: {0}", registration[2].LastName);
Console.WriteLine("Gender: {0}\n",registration[2].Gender);
return 0;
}
}
This would produce: Student Information --------------------- First Name: Gertrude Last Name: Monayong Gender: Female Press any key to continue . . . |
|
|
||
| Previous | Copyright © 2007 Yevol | Home |
|
|
||