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

The items in a control such as ListBox or ComboBox need not be only text. They can actually be just about any object. (When the items are objects, note that the controls' SelectedItem and SelectedItems properties return the objects, not just their text.)

When the items are not text, these controls use the objects' ToString methods to determine what to display. By default, ToString returns the name of the class including its namespace, a value that's not very useful to the user.

You can make these controls display something more useful by overriding the class's ToString method so it displays something more meaningful.

This example defines the following simple Person class with FirstName and LastName properties.

public class Person
{
// 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; }
}
}

The following code derives the Student subclass from the Person class. It overrides the ToString method to return the Student's first and last names.

public class Student : Person
{
// Override ToString.
public override string ToString()
{
return FirstName + " " + LastName;
}
}

The ": 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.

private void Form1_Load(object sender, EventArgs e)
{
// 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);
}

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."

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.