VB 2005 and sorting list of objects...

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi there everybody.
Simple question.
Ive got a Class, lets say "Person" :Public Class Person

Private mName As String
Private mLastName As String
Private mBirthDate As DateTime

Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property

Public Property LastName() As String
Get
Return mLastName
End Get
Set(ByVal value As String)
mLastName = value
End Set
End Property

Public Property BirthDate() As DateTime
Get
Return mBirthDate
End Get
Set(ByVal value As DateTime)
mBirthDate = value
End Set
End Property

Public Overrides Function ToString() As String
Return mName & " " & mLastName
End Function

End Class
Now, I want to have a List Of Persons, lets say "Persons", and simply show the list in a ListBox.
This is what I do : Test Persons
Dim Persons As New List(Of Person)

Dim P As Person

P = New Person
With P
.Name = "Mike"
.LastName = "Johnson"
.BirthDate = New DateTime(1960, 12, 22)
End With
Persons.Add(P)

P = New Person
With P
.Name = "Billy"
.LastName = "Wilder"
.BirthDate = New DateTime(1968, 11, 26)
End With
Persons.Add(P)

P = New Person
With P
.Name = "Mark"
.LastName = "Wilson"
.BirthDate = New DateTime(1968, 11, 26)
End With
Persons.Add(P)

ListBox1.DataSource = Persons
This works fine. Now Id like to have items ordered in a simple alphabetic ASC order.
Sorted property on ListBox doesnt work, because of it is a listbox bounded with a list of objects in the DataSource...

Please note that Im strictly on VB 2005 and have no Linq support.
What should be a possible, quick, solution ?
Thanks.

View the full article
 
Back
Top