Math delegation : Delegate « Class Module « VB.Net Tutorial






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
Enter Operation to Perform
Result = 0.2079
Result = 0.9781
Result = 0.2126








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>)>)