Using Compare methods based on Delegates in VB.NET

In this article you will learn how to use Compare methods based on Delegates in VB.NET.
  • 1069

This code helps you to understand how to use Compare methods based on Delegates in VB.NET.

Module Module1
    Class Employee
        Public FirstNme As String
        Public LastName As String
        Public Title As String
        Public Salary As Double
        Public Sub New(ByVal LastName As String, ByVal FirstNme As String, _
          ByVal Title As String, ByVal Salary As Double)
            Me.Salary = Salary
        End Sub
    End Class
    Delegate Function Compare(ByVal A As Employee, ByVal B As Employee) As Boolean
    Dim CompareMethod As New Compare(AddressOf CompareTitle)
    Function CompareTitle(ByVal A As Employee, ByVal B As Employee) As Boolean
        If (A.Title < B.Title) Then
            CompareTitle = False
        Else
            CompareTitle = True
        End If
    End Function
    Function CompareLastName(ByVal A As Employee, ByVal B As Employee) As Boolean
        If (A.LastName < B.LastName) Then
            CompareLastName = False
        Else
            CompareLastName = True
        End If
    End Function
    Function CompareSalary(ByVal A As Employee, ByVal B As Employee) As Boolean
        If (A.Salary < B.Salary) Then
            CompareSalary = False
     
      CompareSalary = True
     
  End If
    End Function
    Public Sub SortList(ByVal Employeelist() As Employee)
        Dim I, J As Integer
        Dim Temp As Employee
        For I = 0 To Employeelist.Length - 2
            For J = I + 1 To Employeelist.Length - 1
                If (CompareMethod(Employeelist(I), Employeelist(J))) Then
                    Employeelist(I) = Employeelist(J)
                    Employeelist(J) = Temp
                End If
 
      Next
 
   End Sub
    Sub Main()
        Dim EmployeeList(3) As Employee
        EmployeeList(0) = New Employee("A", "Z", "W", 49.99)
        EmployeeList(1) = New Employee("Z", "A", "J", 49.99)
        EmployeeList(2) = New Employee("B", "X", "K", 49.99)
        EmployeeList(3) = New Employee("P", "O", "A", 39.99)
        SortList(EmployeeList)
        Console.WriteLine("By Title")
        Dim EmployeeEntry As Employee
        For Each EmployeeEntry In EmployeeList
            Console.WriteLine(EmployeeEntry.LastName & " " & EmployeeEntry.Title & " " & EmployeeEntry.Salary)
        Next
        Console.WriteLine("By LastName")
        CompareMethod = AddressOf CompareLastName
        SortList(EmployeeList)
        For Each EmployeeEntry In EmployeeList

            Console.WriteLine(EmployeeEntry.LastName & " " & EmployeeEntry.Title & " " & EmployeeEntry.Salary)
        Next
 
       Console.WriteLine("By Salary")
        CompareMethod = AddressOf CompareSalary
        SortList(EmployeeList)
        For Each EmployeeEntry In EmployeeList
            Console.WriteLine(EmployeeEntry.LastName & " " & EmployeeEntry.Title & " " & EmployeeEntry.Salary)
            Console.ReadLine()
        Next
    End Sub

OUTPUT:

comparedelegate.gif

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.