In this
article I will to share with you, what is Delegates with a small example?
Well we
can define Delegates as a type safe “Function Pointer” which holds references
to static or instance methods. Delegates are used to call methods dynamically
at runtime.
Example:-
Public Class
MyAccount
' Delegates Declaration
Public
Delegate Function
CalVariables(ByVal
x As Double, ByVal y As Double) As Double
' Shared Function in class which can be call
without creating object of this class.
' This Class use Delegates as Parameter.
Public
Shared Function
Calculate(ByVal c As
CalVariables, ByVal
x As Double, ByVal y As Double, ByVal z As Double)
Return c(x, y) + z
End
Function
End Class
Public Class
Form1
'''
'''
On Button click, selected value of combo box is taken in Switch case.
'''
If the user had selected Add then the delegates function will add x and y.
'''
If the user had selected subtract then the delegate function will subtract x
and y.
'''
And so on...
'''
'''
'''
'''
Private
Sub BtnShowDetails_Click(ByVal sender As
System.Object, ByVal
e As System.EventArgs)
Handles BtnShowDetails.Click
Select Case Convert.ToString(cmbCar.SelectedItem.ToString())
Case "Add"
Dim AddValue As New MyAccount.CalVariables(AddressOf
AddValues)
'
MyAccount.Calculate will call Calculate
in MyAccount class first.
' Then c(x, y) will
call the AddValues function.
MessageBox.Show(Convert.ToString(MyAccount.Calculate(AddValue, 5, 5, 5)))
Case "Subtract"
Dim subtractValue As New MyAccount.CalVariables(AddressOf
subtractValues)
'
MyAccount.Calculate will call
Calculate in MyAccount class first.
' Then c(x, y) will
call the subtractValues function.
MessageBox.Show(Convert.ToString(MyAccount.Calculate(subtractValue, 5, 5, 5)))
Case "Multiply"
Dim MultiplyValue As New MyAccount.CalVariables(AddressOf
MultiplyValues)
'
MyAccount.Calculate will call
Calculate in MyAccount class first.
' Then c(x, y) will
call the MultiplyValues function.
MessageBox.Show(Convert.ToString(MyAccount.Calculate(MultiplyValue, 5, 5, 5)))
Case "Divide"
Dim DivideValue As New MyAccount.CalVariables(AddressOf
DivideValues)
'
MyAccount.Calculate will call
Calculate in MyAccount class first.
' Then c(x, y) will
call the DivideValues function.
MessageBox.Show(Convert.ToString(MyAccount.Calculate(DivideValue, 5, 5, 5)))
End Select
End
Sub
'
Function To Add x and y
Private
Function AddValues(ByVal
x As Double, ByVal y As Double) As Double
Return x + y
End
Function
'
Function To Subtract x and y
Private
Function subtractValues(ByVal x As Double, ByVal y As Double) As Double
Return x - y
End
Function
'
Function To Multiply x and y
Private
Function MultiplyValues(ByVal x As Double, ByVal y As Double) As Double
Return x * y
End
Function
'
Function To Divide x and y
Private
Function DivideValues(ByVal
x As Double, ByVal y As Double) As Double
Return x / y
End
Function
End Class
In
MyAccount Class I have declared Delegate.
Public Delegate
Function CalVariables(ByVal x As Double, ByVal y As Double) As Double
CalVariables
is a shared function where CalVariables Delegates is used as Parameter.
In Form1 on Button click, selected value of combo box is taken in
Switch case.
If the user had selected Add then,
Our delegates is addressed to AddValues Function.
Dim AddValue As
New MyAccount.CalVariables(AddressOf
AddValues)
MessageBox.Show(Convert.ToString(MyAccount.Calculate(AddValue,
5, 5, 5)))
Will be executed.
On calling MyAccount.Calculate(AddValue, 5, 5, 5)), Calculate
method is called and
From there on c(x, y) + z; it calls to AddValues function.
This is a small basic example of .NET delegates as a powerful
mechanism to perform dynamic method invocation.
No comments:
Post a Comment