|
If you want to associate a procedure that takes arguments to a delegate, when declaring the delegate, provide the necessary argument(s) in its parentheses. Here is an example of a delegate that takes two arguments (and returns a value): Module Exercise
Delegate Function Multiplication(ByVal value As Double) As Double
End Module
When defining the associated procedure, besides returning the same type of value, make sure that the procedure takes the same number of arguments. Here is an example: Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Delegate Function Multiplication(ByVal value As Double) As Double
Public Function Area(ByVal Side As Double) As Double
Return 6 * Side * Side
End Function
End Module
To associate the procedure, declare a variable of the type of delegate and access the name of the procedure using the AddressOf operator. Here is an example: Module Exercise
Delegate Function Multiplication(ByVal value As Double) As Double
Public Function Area(ByVal Side As Double) As Double
Return 6 * Side * Side
End Function
Public Function Main() As Integer
Dim AreaDefinition As Multiplication = New Multiplication(AddressOf Area)
Return 0
End Function
End Module
To assist you with accessing a delegate, the Delegate class is equipped with a method named Invoke. Therefore, to associate a method to a delegate, call this method on the variable of the delegate. The syntax of the Invoke() method is: Public Function Invoke(method As Delegate) As Object Here is an example: Module Exercise
Delegate Function Multiplication(ByVal value As Double) As Double
Public Function Area(ByVal Side As Double) As Double
Return 6 * Side * Side
End Function
Public Function Volume(ByVal Side As Double) As Double
Return Side * Side * Side
End Function
Public Function Main() As Integer
Dim Side As Double = 46.95
Dim AreaDefinition As Multiplication
Dim VolDefinition As Multiplication
AreaDefinition = New Multiplication(AddressOf Area)
VolDefinition = New Multiplication(AddressOf Volume)
MsgBox("Cube Calculation" & vbCrLf & _
"Side: " & vbTab & CStr(Side) & vbCrLf & _
"Area: " & vbTab & CStr(AreaDefinition.Invoke(Side)) & vbCrLf & _
"Volume: " & vbTab & CStr(VolDefinition.Invoke(Side)))
Return 0
End Function
End Module
Using delegates, a procedure can be indirectly passed as argument to another procedure. To proceed, first declare the necessary delegate. Here is a example of such a delegate: Module Exercise
Delegate Function Squared(ByVal value As Double) As Double
End Module
A delegate can be passed as argument to a procedure. Such an argument would be used as if it were a procedure itself. This means that, when accessed in the body of the procedure, the name of the delegate must be accompanied by parentheses and if the delegate takes an argument or arguments, the argument(s) must be provided in the parentheses of the called delegate. Here is an example: Module Exercise
Private Radius As Double
Delegate Function Squared(ByVal value As Double) As Double
Public Function Area(ByVal sqd As Squared) As Double
Return sqd(Radius) * Math.PI
End Function
End Module
After declaring a delegate, remember to define a procedure that implements the needed behavior of that delegate. Once the procedure is ready, to associate it to a delegate, declare a variable of the type of the delegate using the New operator. In the parentheses of the constructor, access the name of the associated procedure using the AddressOf operator. To access the delegate, call the Invoke() method. Here is an example: Module Exercise
Private Radius As Double
Delegate Function Squared(ByVal value As Double) As Double
Public Function Area(ByVal sqd As Squared) As Double
Return sqd(Radius) * Math.PI
End Function
Public Function ValueTimesValue(ByVal value As Double) As Double
Return value * value
End Function
Public Function Main() As Integer
Dim Side As Double = 46.95
Dim AreaDefinition As Squared
AreaDefinition = New Squared(AddressOf ValueTimesValue)
MsgBox("Circle Calculation" & vbCrLf & _
"Side: " & vbTab & CStr(Side) & vbCrLf & _
"Area: " & vbTab & CStr(AreaDefinition.Invoke(Side)))
Return 0
End Function
End Module
|
|
|||||||
|
|