Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?
*/

Other Views

corner
*/

VB.NET School - Delegates and Events - Lesson #10 - Page 1

Next Page



Delegates and Events

If you are new to VB.Net School
This is the 10th in the series of lessons of our VB.Net School. The VB.Net School is a kind of interactive learning platform where those who want to learn .NET with VB.Net can find help and support. With one issue a week, describing some areas of the VB.Net Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author ( farazrasheed@acm.org) or the WEBMASTER ( info@programmersheaven.com).

For previous lessons click here

Lesson Plan
In this lesson we will explore the concept of delegates and events. We will start out by looking at the idea behind delegates and will see how delegates are used in VB.Net. Later we will explore multicast delegates and their significance. Finally we will learn about events and the event handling mechanism through delegates.

Delegates Basics
Delegates are referenced to methods. So far we have used references for objects such as

        Dim st As New Stack()


In this senario st is a reference to an object of the Stack class type. Each reference has two properties 1. The type of object (class), the reference can point to
2. The actual object referenced (or pointed) by the reference.


Delegates are similar to object references but are used to reference methods instead of objects. The type of a delegates is the type or signature of a method rather than the class. Hence a delegate has three properties 1. The type or signature of the method the delegate can point to
2. A delegate reference which can be used to reference a method
3. The actual method referenced by the delegate


Author's Note: The concept of delegates is similar to the function pointers used in C++.

http://www.programmersheaven.com/articles/faraz/lesson10_img1.gif


1. The type or signature of the method the delegate can point to
Before using a delegate, we need to specify the type or signature of method the delegate can reference to. The signature of a method includes its return type and the type of parameter which it requires to be passed. For example,

Sub someMethod(ByVal args() As Integer)


is one of the common signatures of Main() method of VB.Net programs defined as

Sub Main(ByVal args() As Integer)
...
End Sub


and for the following Add() method

    Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
        Return a + b
    End Function


The signature will be

    Function aMethod(ByVal p As Integer, ByVal q As Integer) As Integer


which is also the signature of the following Subtract() method

    Function Subtract(ByVal a As Integer, ByVal b As Integer) As Integer
        Return a - b
    End Function


It should be noticed from the above examples that the name of a method is not part of its signature but only its return type and parameters.

In case of delegates, we define the type of a delegate using the Delegate keyword as coded below

    Delegate Function MyDelegate(ByVal p As Integer, ByVal q As Integer) As Integer


Here we have defined a delegate type with the name 'MyDelegate'. The reference of this delegate type can be used to point any method which takes two integers as parameters and returns an integer value.

2. The delegate reference which can be used to reference a method
Once we have defined a delegate type, we can set it to references actual methods with the matching signature. A delegate reference can be declared just like an object reference. For example, a reference of type MyDelegate (defined above) can be declared as

        Dim arithMethod As MyDelegate


The delegate reference arithMethod can now reference any method whose signature is similar to the signature of MyDelegate

3. The actual method referenced by the delegate - the AddressOf keyword
The delegate reference can be made to reference any method with a matching signature by passing its name in the parameter of delegate type with the AddressOf keyword like below

        arithMethod = New MyDelegate(AddressOf Add)


We can see the arithMethod delegate reference has been made to point out the Add() method by passing its name as a parameter to the delegate type (MyDelegate). The AddressOf keyword here justifies that we are actually passing the address of the method 'Add' to the delegate. Internally the delegate will use this address to call the 'Add' method.

The last two steps can be merged together in a single statement as

        Dim arithMethod As New MyDelegate(AddressOf Add)



School Home
Next Page





corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings