Override a class's ToString method to allow controls such as ListBox to display objects in C#
This example actually demonstrates several useful techniques:
- Subclassing
- Overriding the ToString method
- Declaring and initializing arrays
- Initializing new objects
- Displaying an array of objects in a ListBox
public class PersonThe following code derives the Student subclass from the Person class. It overrides the ToString method to return the Student's first and last names.
{
// Properties.
private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
}
public class Student : PersonThe ": Person" part of the class declaration means the class should inherit from the Person class. Note when you type "public override" and a space in the code editor, IntelliSense lists the public methods that you can override. In this case, these include Equals, GetHashCode, and ToString. If you select one, the editor fills in a default version of the overridden method. You can simply replace the default implementation with whatever code you want. When the program loads, it uses the following code to declare and initialize arrays of objects. If you haven't seen this before, take a minute to study it.
{
// Override ToString.
public override string ToString()
{
return FirstName + " " + LastName;
}
}
private void Form1_Load(object sender, EventArgs e)The text "Person[]" means the program is declaring a variable that is an array of Person objects. The declaration then has the variable's name, followed by an equal sign, followed by a list of values surrounded by curly brackets. Each object in the array is created by using the new keyword. The class's name is followed by curly brackets enclosing statements that initialize the object's properties. This lets you easily declare, create, and initialize an object all in one statement. Finally, after it creates an array of objects, the program assigns the array to a ListBox's DataSource property. (The same works for ComboBoxes.) The ListBox uses the objects' ToString methods to decide what to display. Because the Person class uses its default ToString method that it inherits from the ultimate base class Object, its ListBox displays its objects' class names: "howto_override_tostring.Person." The Student class overrides its ToString method so its ListBox displays the Students' names as in "Alice Archer."
{
// Make an array of Persons.
Person[] people = {
new Person() { FirstName="Alice", LastName="Archer"},
new Person() { FirstName="Ben", LastName="Balk"},
new Person() { FirstName="Cindy", LastName="Carter"},
new Person() { FirstName="Dean", LastName="Dent"},
new Person() { FirstName="Ermintrude", LastName="Eag"},
};
// Display the People in the ListBox.
lstPeople.DataSource = people;
// Make an array of Students.
Student[] students = {
new Student() { FirstName="Alice", LastName="Archer"},
new Student() { FirstName="Ben", LastName="Balk"},
new Student() { FirstName="Cindy", LastName="Carter"},
new Student() { FirstName="Dean", LastName="Dent"},
new Student() { FirstName="Ermintrude", LastName="Eag"},
};
// Display the Students in the ListBox.
lstStudents.DataSource = students;
// Rearrange the ListBoxes.
Form1_Resize(null, null);
}


Comments