What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleOverride a class's ToString method to allow controls such as ListBox to display objects in Visual Basic .NET
DescriptionThis example shows how to override a class's ToString method to allow controls such as ListBox to display objects in Visual Basic .NET.
KeywordsToString, override, ListBox, ComboBox, array, initialize array, initialize object, Visual Basic .NET, Visual Basic, VB.NET
CategoriesControls, Software Engineering
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 m_FirstName As String
    Public Property FirstName() As String
            Return m_FirstName
        End Get
        Set(ByVal value As String)
            m_FirstName = value
        End Set
    End Property

    Private m_LastName As String
    Public Property LastName() As String
            Return m_LastName
        End Get
        Set(ByVal value As String)
            m_LastName = value
        End Set
    End Property
End Class
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
    Inherits Person

    ' Override ToString.
    Public Overrides Function ToString() As String
        Return FirstName & " " & LastName
    End Function
End 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 Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make an array of Persons.
    Dim people() As Person = { _
        New Person() With {.FirstName = "Alice", .LastName _
            = "Archer"}, _
        New Person() With {.FirstName = "Ben", .LastName = _
            "Balk"}, _
        New Person() With {.FirstName = "Cindy", .LastName _
            = "Carter"}, _
        New Person() With {.FirstName = "Dean", .LastName = _
            "Dent"}, _
        New Person() With {.FirstName = "Ermintrude", _
            .LastName = "Eag"} _

    ' Display the People in the ListBox.
    lstPeople.DataSource = people

    ' Make an array of Students.
    Dim students() As Student = { _
        New Student() With {.FirstName = "Alice", .LastName _
            = "Archer"}, _
        New Student() With {.FirstName = "Ben", .LastName = _
            "Balk"}, _
        New Student() With {.FirstName = "Cindy", .LastName _
            = "Carter"}, _
        New Student() With {.FirstName = "Dean", .LastName _
            = "Dent"}, _
        New Student() With {.FirstName = "Ermintrude", _
            .LastName = "Eag"} _

    ' Display the Students in the ListBox.
    lstStudents.DataSource = students

    ' Rearrange the ListBoxes.
    Form1_Resize(Nothing, Nothing)
End Sub
The text "Dim people() As Person" means the program is declaring a variable that is an array of Person objects. The declaration it followed by a list of Person objects surrounded by curly braces.

Each object in the array is created by using the new keyword. The With statement followed by curly braces and values assigns values to the new object's properties. (This was introduced in Visual Basic 2008 so if you're using Visual Basic 2005 or earlier, then you need to initialize the objects in some other way.)

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

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.