Comparison method based on delegate : Delegate « Class Module « 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 » Class Module » Delegate 
6.28.7.Comparison method based on delegate
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.FirstNme = FirstNme
            Me.LastName = LastName
            Me.Title = Title
            Me.Salary = Salary
        End Sub

    End Class

    Delegate Function Compare(ByVal A As Employee, ByVal B As EmployeeAs Boolean

    Dim CompareMethod As New Compare(AddressOf CompareTitle)

    Function CompareTitle(ByVal A As Employee, ByVal B As EmployeeAs Boolean
        If (A.Title < B.TitleThen
            CompareTitle = False
        Else
            CompareTitle = True
        End If
    End Function

    Function CompareLastName(ByVal A As Employee, ByVal B As EmployeeAs Boolean
        If (A.LastName < B.LastNameThen
            CompareLastName = False
        Else
            CompareLastName = True
        End If
    End Function

    Function CompareSalary(ByVal A As Employee, ByVal B As EmployeeAs Boolean
        If (A.Salary < B.SalaryThen
            CompareSalary = False
        Else
            CompareSalary = True
        End If
    End Function

    Public Sub SortList(ByVal Employeelist() As Employee)
        Dim I, J As Integer
        Dim Temp As Employee

        For I = To Employeelist.Length - 2
            For J = I + To Employeelist.Length - 1
                If (CompareMethod(Employeelist(I), Employeelist(J))) Then
                    Temp = Employeelist(I)
                    Employeelist(I= Employeelist(J)
                    Employeelist(J= Temp
                End If
            Next
        Next
    End Sub

    Sub Main()
        Dim EmployeeList(3As 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)
        Next
    End Sub
End Module
By Title
P A 39.99
Z J 49.99
B K 49.99
A W 49.99
By LastName
A W 49.99
B K 49.99
P A 39.99
Z J 49.99
By Salary
P A 39.99
Z J 49.99
B K 49.99
A W 49.99
6.28.Delegate
6.28.1.Define delegate
6.28.2.Define a delegate to be a pointer to a subroutine that has a string parameter.
6.28.3.Use Delegate Sub
6.28.4.Delegate Function
6.28.5.Uses delegates to sort random numbers (ascending or descending)
6.28.6.Use AddressOf to assign function to a delegate
6.28.7.Comparison method based on delegate
6.28.8.Multiple Delegates
6.28.9.Math delegation
6.28.10.Use one Delegate to reference two different functions
6.28.11.Reference a method that has one parameter and returns a value: Func<(Of <(T, TResult>)>)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.