VB.Net Tutorial/Class Module/Delegate

Материал из VB Эксперт
Перейти к: навигация, поиск

Comparison method based on delegate

<source lang="vbnet">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 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
       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 = 0 To Employeelist.Length - 2
           For J = I + 1 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(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)
       Next
   End Sub

End Module</source>

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

Define a delegate to be a pointer to a subroutine that has a string parameter.

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim m_DisplayStringRoutine As StringDisplayerType
       m_DisplayStringRoutine = AddressOf ShowStringInMessageBox " ShowStringInOutputWindow
       m_DisplayStringRoutine("Hello world")
  End Sub
   Private Delegate Sub StringDisplayerType(ByVal str As String)
   Private Shared Sub ShowStringInOutputWindow(ByVal str As String)
       Console.WriteLine(str)
   End Sub
   Private Shared Sub ShowStringInMessageBox(ByVal str As String)
       Console.WriteLine("+++"+str)
       
   End Sub

End class</source>

+++Hello world

Define delegate

<source lang="vbnet">public class Test

   Private Delegate Function NumEmployeesDelegate() As Integer
   Private Delegate Function GetNameDelegate() As String
  public Shared Sub Main
     Dim emp As New Employee()    
       Dim show_name As GetNameDelegate
       show_name = AddressOf emp.ToString
       Console.WriteLine(show_name())
     
  End Sub

End class

Public Class Employee

   Public Shared Function GetNumEmployees() As Integer
       Return 13
   End Function
   Public Overrides Function ToString() As String
       Return "Employee"
   End Function

End Class</source>

Employee

Delegate Function

<source lang="vbnet">Module Module1

   Delegate Function Numbers(ByVal A As Double, ByVal B As Double) As Double
   Function SubtractNumbers(ByVal A As Double, ByVal B As Double) As Double
       SubtractNumbers = A - B
   End Function
   Function AddNumbers(ByVal A As Double, ByVal B As Double) As Double
       AddNumbers = A + B
   End Function
   Sub Main()
       Dim DoNumbers As New Numbers(AddressOf SubtractNumbers)
       Console.WriteLine(DoNumbers.Invoke(100, 50))
       DoNumbers = AddressOf AddNumbers
       Console.WriteLine(DoNumbers.Invoke(100, 50))
   End Sub

End Module</source>

50
150

Math delegation

<source lang="vbnet">Imports System Module Test

 Sub Main()
   Console.WriteLine("Enter Operation to Perform")
   PerformOp(New MathOperation(AddressOf Math.Sin))
   PerformOp(New MathOperation(AddressOf Math.Cos))
   PerformOp(New MathOperation(AddressOf Math.Tan))
 End Sub
  Delegate Function MathOperation(ByVal radians As Double) As Double
  Private Sub PerformOp(ByVal op As MathOperation)
    Dim angle As Double = 12
    Dim radians As Double
    radians = (angle / 360) * 2 * Math.PI
    Dim result As Double
    result = op.Invoke(radians)
    result = Math.Round(result, 4)
    Console.Writeline("Result = " & result.ToString())
 End Sub

End Module</source>

Enter Operation to Perform
Result = 0.2079
Result = 0.9781
Result = 0.2126

Multiple Delegates

<source lang="vbnet">Module Module1

   Delegate Sub NotifyInfo()
   Dim ListOfDelegates(3) As NotifyInfo
   Sub Testing()
       Console.WriteLine("AAA")
   End Sub
   Sub Coding()
       Console.WriteLine("BBB")
   End Sub
   Sub Meeting()
       Console.WriteLine("CCC")
   End Sub
   Sub Main()
       ListOfDelegates(0) = New NotifyInfo(AddressOf Coding)
       ListOfDelegates(1) = New NotifyInfo(AddressOf Testing)
       ListOfDelegates(2) = New NotifyInfo(AddressOf Meeting)
       Dim NotifyAll As New NotifyInfo(AddressOf Coding)
       NotifyAll = NotifyInfo.rubine(ListOfDelegates)
       NotifyAll.Invoke()
   End Sub

End Module</source>

BBB
AAA
CCC

Use AddressOf to assign function to a delegate

<source lang="vbnet">public class Test

   Private Delegate Function NumEmployeesDelegate() As Integer
   Private Delegate Function GetNameDelegate() As String
  public Shared Sub Main
     Dim emp As New Employee()    
       Dim show_num As NumEmployeesDelegate
       show_num = AddressOf Employee.GetNumEmployees
       Console.WriteLine(show_num().ToString)
     
  End Sub

End class

Public Class Employee

   Public Shared Function GetNumEmployees() As Integer
       Return 13
   End Function
   Public Overrides Function ToString() As String
       Return "Employee"
   End Function

End Class</source>

13

Use Delegate Sub

<source lang="vbnet">Module Module1

   Delegate Sub StringMethod(ByVal S As String)
   Sub DisplayString(ByVal S As String)
       Console.WriteLine(S)
   End Sub
   Sub UpperString(ByVal S As String)
       Console.WriteLine(UCase(S))
   End Sub
   Sub Main()
       Dim MyMethod As New StringMethod(AddressOf DisplayString)
       MyMethod.Invoke("Visual Basic .Net Programming")
       MyMethod = AddressOf UpperString
       MyMethod.Invoke("Visual Basic .Net Programming")
   End Sub

End Module</source>

Visual Basic .Net Programming
VISUAL BASIC .NET PROGRAMMING

Uses delegates to sort random numbers (ascending or descending)

<source lang="vbnet">Public Class Tester

  Private Shared Function SortAscending(ByVal element1 As Integer, _
     ByVal element2 As Integer) As Boolean
     Return element1 > element2
  End Function " SortAscending
  Private Shared Function SortDescending(ByVal element1 As Integer, _
     ByVal element2 As Integer) As Boolean
     Return element1 < element2
  End Function " SortDescending
  Public Shared Sub Main
     Dim mBubbleSort As New BubbleSort()
     Dim mElementArray As Integer() = New Integer(9) {}
     Dim randomNumber As Random = New Random()
     Dim i As Integer
     " create String with 10 random numbers
     For i = 0 To mElementArray.GetUpperBound(0)
        mElementArray(i) = randomNumber.Next(100)
        Console.WriteLine(mElementArray(i))
     Next
   
     mBubbleSort.SortArray(mElementArray, AddressOf SortAscending)
     For i = 0 To mElementArray.GetUpperBound(0)
        Console.Write(mElementArray(i) & " " )
     Next
     Console.WriteLine()
     mBubbleSort.SortArray(mElementArray, AddressOf SortDescending)
     For i = 0 To mElementArray.GetUpperBound(0)
        Console.Write(mElementArray(i) & " " )
     Next
  End Sub

End Class Public Class BubbleSort

  Public Delegate Function Comparator( _
     ByVal element1 As Integer, _
     ByVal element2 As Integer) As Boolean
  Public Sub SortArray(ByVal array As Integer(), _
     ByVal Compare As Comparator)
     Dim i, pass As Integer
     For pass = 0 To array.GetUpperBound(0)
        For i = 0 To array.GetUpperBound(0) - 1
           If Compare(array(i), array(i + 1)) Then
              Swap(array(i), array(i + 1))
           End If
        Next " inner loop
     Next " outer loop
  End Sub " SortArray
  Private Sub Swap(ByRef firstElement As Integer, _
     ByRef secondElement As Integer)
     Dim hold As Integer
     hold = firstElement
     firstElement = secondElement
     secondElement = hold
  End Sub " Swap

End Class</source>

77
24
72
44
64
75
23
44
93
73
23 24 44 44 64 72 73 75 77 93
93 77 75 73 72 64 44 44 24 23 "