Hey everyone!
I need help sorting a list, containing a custom class. I want the list to be sorted alphabetically using the .ToString() function but it crashes on runtime. (My example code is below.)
Can somebody please teach me how to use Comparers?
Thanks a lot!
~Nic
vb Code:
Public Class Form1 Dim Websites As New List(Of URL) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With Websites .Add(New URL("VB Forums", "www.vbforums.com", True)) .Add(New URL("W3 Schools", "www.w3schools.com", False)) .Add(New URL("Google", "www.google.com", True)) .Add(New URL("Amazon", "www.amazon.com", False)) End With ' I need to sort the list 'Websites' by alphabetical order. ' Using List.Sort crashes the program. Websites.Sort() For Each url In Websites ListBox1.Items.Add(url.ToString) Next End Sub End Class ' Example class with 3 properties. Public Class URL Property WebpageName As String Property WebAddress As String Property Favorite As Boolean Sub New(Name_ As String, Address_ As String, Favorite_ As Boolean) WebpageName = Name_ WebAddress = Address_ Favorite = Favorite_ End Sub Public Overrides Function ToString() As String Return WebpageName & " Url: " & WebAddress End Function End Class




Reply With Quote
