Delegates Basics

A delegate allows us to encapsulate a reference to a method inside an object -- a delegate object, to be precise. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Before trying to understand the above, let us work with a simple example. The following is a sample class:


Public Class Sample01

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 Sub Add()

MessageBox.Show("Sum = " & (Me.X + Me.Y))

End Sub


Public Sub Multiply()

MessageBox.Show("Product = " & (Me.X * Me.Y))

End Sub

End Class



The above class has two private fields ("_x" and "_y") which are only accessible within the class, and not outside the class. Further, it has two public properties and two public methods, "Add" and "Multiply." Note that public members are accessible even outside the class.

To test the above class, add a new form with two buttons and a label. Modify your code to match the following:


'without using delegates

Public Class Form1


Dim obj As New Sample01(10, 20)


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

obj.Add()

End Sub


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

obj.Multiply()

End Sub


End Class


There is nothing new in the above form. We are simply instantiating an object based on the "Sample01" class and calling its methods. We will do the same in the next section, but with "Delegates."

In the previous section, a class named "Sample01" was introduced. The following is a simple "delegate" way of coding:


'sample of using delegate


Public Class Form2




Delegate Sub Calculate()


Dim obj As New Sample01(10, 20)




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


Dim delg As New Calculate(AddressOf obj.Add)


delg.Invoke()


End Sub




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


Dim delg As New Calculate(AddressOf obj.Multiply)


delg.Invoke()


End Sub


End Class



Let us try to understand it step by step. First of all we have the following:


Delegate Sub Calculate()


The line says that "Calculate" is a delegate. Even though its signature looks like a method, it is actually a class. Consider "Calculate" to be a named class of yours having its own functionality to invoke "methods of other objects dynamically."

Further proceeding we have the following:


Dim obj As New Sample01(10, 20)


It is simply an instantiation. Further on we have the following:


Dim delg As New Calculate(AddressOf obj.Add)


As previously described, "Calculate" is a class. And now, "delg" is an instance of the class "Calculate," which is "authorized" to access and execute the method named "obj.Add()".

The "obj.Add()" method gets executed when the following statement is invoked:


delg.Invoke()


Finally, a delegate is simply an object which can execute methods of other objects dynamically at run time.

To make all of this simpler, the above can also be written as follows:



'Without creating instances of a delegate and without using invoke


Public Class Form3




Delegate Sub Calculate()


Dim obj As New Sample01(10, 20)


Dim deleg As Calculate




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


deleg = AddressOf obj.Add


deleg()


End Sub




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


deleg = AddressOf obj.Multiply


deleg()


End Sub


End Class

0 comments:

Post a Comment

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