Delegates To Methods Returning Values


In
all of the previous examples, I concentrated only on methods which do not return any values. Now, let us concentrate on delegates which can be used to call methods returning values (say, functions in a class).

Let us add one more class as follows:



Public Class Sample03


Private _x As Integer


Private _y As Integer




Public Sub New()




End Sub




Public Sub New(ByVal a As Integer, ByVal b As Integer)


_x = a


_y = b


End Sub




Public Property X() As Integer


Get


Return _x


End Get


Set(ByVal value As Integer)


_x = value


End Set


End Property




Public Property Y() As Integer


Get


Return _y


End Get


Set(ByVal value As Integer)


_y = value


End Set


End Property




Public Function GetSum() As Integer


Return (Me.X + Me.Y)


End Function




Public Function GetProduct() As Integer


Return (Me.X * Me.Y)


End Function




End Class



The above class contains two methods, "GetSum()" and "GetProduct()," which return values of type integer. To access those methods using delegates, you can code as follows:


'delegates to functions


Public Class Form5


Delegate Function Calculate() As Integer




Dim delegCalc As Calculate


Dim obj As New Sample03(10, 20)




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


delegCalc = AddressOf obj.GetSum


MessageBox.Show("Sum = " & delegCalc())


'MessageBox.Show("Sum = " & delegCalc.Invoke())


End Sub




Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


delegCalc = AddressOf obj.GetProduct


MessageBox.Show("Product = " & delegCalc())


'MessageBox.Show("Product = " & delegCalc.Invoke())


End Sub




End Class


"Callback"
is one of the most important features of delegates. Let us start with an example. To make things easier to understand, I added a new class as follows:



Public Class Sample04




Delegate Sub FactorFound(ByVal FactorValue As Integer)




Private _x As Integer




Public Sub New()




End Sub




Public Sub New(ByVal a As Integer)


_x = a


End Sub




Public Property X() As Integer


Get


Return _x


End Get


Set(ByVal value As Integer)


_x = value


End Set


End Property




Public Sub FindFactors(ByVal delgFoundFactor As FactorFound)


For i As Integer = 1 To _x


If _x Mod i = 0 Then


delgFoundFactor(i)


End If


Next


End Sub




End Class



The most important method from the above class is the following:



Public Sub FindFactors(ByVal delgFoundFactor As FactorFound)


For i As Integer = 1 To _x


If _x Mod i = 0 Then


delgFoundFactor(i)


End If


Next


End Sub



The method accepts a parameter of type "delegate" which is declared at module level as follows:


Delegate Sub FactorFound(ByVal FactorValue As Integer)


That means the calling program can execute the "FindFactors" method by passing the address of another method. The address, which is passed to "FindFactors," can be invoked within the same "FindFactors" method.

In simple words, the calling program executes "FindFactors" by giving permission to the "FindFactors" method to execute (internally inside "FindFactors") another method (address) passed to it.

The following sample can be considered a caller for this demonstration:



'implementing callback methods using delegates


Public Class Form6




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Me.ListBox1.Items.Clear()


Dim obj As New Sample04(Me.TextBox1.Text)


obj.FindFactors(AddressOf FactorAvailable)


End Sub




Private Sub FactorAvailable(ByVal FactorValue As Integer)


Me.ListBox1.Items.Add(FactorValue)


End Sub


End Class



From the above, you can observe that "FactorAvailable" is passed to the "FindFactors" method. The "FactorAvailable" method gets executed with the following statement in the "FindFactors" method:



delgFoundFactor(i)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.