Use Delegate to implement custome sort : Delegate « Language Basics « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Language Basics » DelegateScreenshots 
Use Delegate to implement custome sort
Use Delegate to implement custome sort
 
Imports System

Public Class MainClass
    Shared Sub Main(  )
             Dim Student1 As New Student("Student1")
             Dim Student2 As New Student("Student2")
             Dim Employee1 As New Employee(65)
             Dim Employee2 As New Employee(12)

             Dim studentPair As New Pair(Student1, Student2)
             Dim employeePair As New Pair(Employee1, Employee2)
             Console.WriteLine("studentPair: {0}", _
                 studentPair.ToString(  ))
             Console.WriteLine("employeePair: {0}", _
                 employeePair.ToString(  ))

             Dim theStudentDelegate As New _
               Pair.WhichIsSmaller(AddressOf Student.WhichStudentIsSmaller)
             Dim theEmployeeDelegate As New _
               Pair.WhichIsSmaller(AddressOf Employee.WhichEmployeeIsSmaller)

             studentPair.Sort(theStudentDelegate)
             Console.WriteLine("After Sort studentPair: {0}", _
                 studentPair.ToString(  ))
             studentPair.ReverseSort(theStudentDelegate)
             Console.WriteLine("After ReverseSort studentPair: {0}", _
                 studentPair.ToString(  ))

             employeePair.Sort(theEmployeeDelegate)
             Console.WriteLine("After Sort employeePair: {0}", _
                 employeePair.ToString(  ))
             employeePair.ReverseSort(theEmployeeDelegate)
             Console.WriteLine("After ReverseSort employeePair: {0}", _
                 employeePair.ToString(  ))
    
    End Sub 'Main

   
End Class

     Public Enum Comparison
         theFirst = 1
         theSecond = 2
     End Enum

     Public Class Pair
         Private thePair(2As Object

         Public Delegate Function WhichIsSmaller(ByVal obj1 As Object, ByVal obj2 As ObjectAs Comparison

         Public Sub New(ByVal firstObject As Object,ByVal secondObject As Object)
             thePair(0= firstObject
             thePair(1= secondObject
         End Sub

         Public Sub Sort(ByVal theDelegatedFunc As WhichIsSmaller)
             If theDelegatedFunc(thePair(0), thePair(1)) = _
                Comparison.theSecond Then
                 Dim temp As Object = thePair(0)
                 thePair(0= thePair(1)
                 thePair(1= temp
             End If
         End Sub

         Public Sub ReverseSort(ByVal theDelegatedFunc As WhichIsSmaller)
             If theDelegatedFunc(thePair(0), thePair(1)) = _
                    Comparison.theFirst Then
                 Dim temp As Object = thePair(0)
                 thePair(0= thePair(1)
                 thePair(1= temp
             End If
         End Sub

         Public Overrides Function ToString(  ) As String
             Return thePair(0).ToString(  ) ", " & thePair(1).ToString(  )
         End Function
     End Class

     Public Class Employee
         Private weight As Integer

         Public Sub New(ByVal weight As Integer)
             Me.weight = weight
         End Sub

         Public Shared Function WhichEmployeeIsSmaller(ByVal o1 As Object, ByVal o2 As ObjectAs Comparison
             Dim d1 As Employee = DirectCast(o1, Employee)
             Dim d2 As Employee = DirectCast(o2, Employee)
             If d1.weight > d2.weight Then
                 Return Comparison.theSecond
             Else
                 Return Comparison.theFirst
             End If
         End Function

         Public Overrides Function ToString(  ) As String
             Return weight.ToString(  )
         End Function
     End Class

     Public Class Student

         Private name As String

         Public Sub New(ByVal name As String)
             Me.name = name
         End Sub

         Public Shared Function WhichStudentIsSmaller_
           ByVal o1 As Object, ByVal o2 As ObjectAs Comparison
             Dim s1 As Student = DirectCast(o1, Student)
             Dim s2 As Student = DirectCast(o2, Student)
             If String.Compare(s1.name, s2.nameThen
                 Return Comparison.theFirst
             Else
                 Return Comparison.theSecond
             End If
         End Function

         Public Overrides Function ToString(  ) As String
             Return name
         End Function
     End Class


           
         
  
Related examples in the same category
1.Delegate with and without parametersDelegate with and without parameters
2.Two ways to init a DelegateTwo ways to init a Delegate
3.Multicast DelegateMulticast Delegate
4.Register Delegates and call themRegister Delegates and call them
5.Simple Delegate DemoSimple Delegate Demo
6.Sort DelegateSort Delegate
7.Function Delegation DemoFunction Delegation Demo
8.Function Delegate: output stringFunction Delegate: output string
9.Delegate Demo for a Simple ClassDelegate Demo for a Simple Class
10.Delegate Syntax
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.