Understanding Delegates using Visual Basic.NET 2005
(Page 1 of 6 )
This article gives you a solid understanding of delegates using Visual Basic.NET 2005. You will need a good foundation in object-oriented programming and Visual Basic.NET to understand this article.
If you are new to OOP in Visual Basic.NET, I strongly suggest that you read the articles you will find at the following links:
http://www.aspfree.com/c/a/VB.NET/Using-Constructors-with-Object-Oriented-Database-Development-with-VB-NET-2005/
http://www.aspfree.com/c/a/VB.NET/Properties-and-Object-Oriented-Database-Development-with-VBNET-2005/
http://www.aspfree.com/c/a/VB.NET/Using-Methods-with-Object-Oriented-Database-Development-with-VB-NET-2005/
http://www.aspfree.com/c/a/VB.NET/Inheritance-with-VB-NET-2005/
http://www.aspfree.com/c/a/VB.NET/Understanding-Custom-Events-using-Visual-Basic-NET-2005/
The entire source code for this article is available in the form of a downloadable zip file. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition. I didn't really test it in any other environment. I request that you post in the discussion area if you have any problems in execution.
A simple introduction to Delegates using Visual Basic 2005
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."
Next: A simple introduction Continued >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee