IComparable : IComparable « Collections « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Collections » IComparable 
8.20.1.IComparable
Imports System.Collections

Module Test
    Public Sub Main()

        Dim objEmployee1 As New Employee()
        Dim objEmployee2 As New Employee()
        Dim objEmployee3 As New Employee()

        objEmployee1.FirstName = "A"
        objEmployee1.LastName = "B"
        objEmployee2.FirstName = "C"
        objEmployee2.LastName = "D"
        objEmployee3.FirstName = "E"
        objEmployee3.LastName = "F"

        Dim slPeople As New SortedList()

        slPeople.Add(objEmployee1, 1)
        slPeople.Add(objEmployee2, 2)
        slPeople.Add(objEmployee3, 3)

        Dim objDE As DictionaryEntry

        For Each objDE In slPeople
            Console.WriteLine("{0} {1}",CType(objDE.Key, Employee).DisplayName, CType(objDE.Value,Integer))
        Next
    End Sub
End Module

Public Class Employee
    Implements IComparable
    Public FirstName As String
    Public LastName As String

    Public ReadOnly Property DisplayName() As String
        Get
            Return String.Format("{0} {1}", FirstName, LastName)
        End Get
    End Property

    Public Function CompareTo(ByVal obj As ObjectAs Integer _
        Implements System.IComparable.CompareTo

        Dim objOtherEmployee As Employee
        objOtherEmployee = CType(obj, Employee)

        If objOtherEmployee.LastName > Me.LastName Then
            Return -1
        ElseIf objOtherEmployee.LastName < Me.LastName Then
            Return 1
        Else
            If objOtherEmployee.FirstName > Me.FirstName Then
                Return -1
            ElseIf objOtherEmployee.FirstName < Me.FirstName Then
                Return 1
            Else
                Return 0
            End If
        End If
    End Function
End Class
A B 1
C D 2
E F 3
8.20.IComparable
8.20.1.IComparable
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.