Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Delegates in VB.Net

Author: DevASP
Download Source Code : 711_Delegates.zip

In this article I will try to explain you the basics of Delegates in VB.Net. The source code is created using .Net Framework 2.0

Delegates in simple words, is a data structure that refers to a static method or to an object instance, and an instance method of that object. Delegates add safety and as compared to the traditional pointers. In C\C++ we declare pointer in two ways that is by reference or by value. In Vb.Net delegate stores the reference of function using “addressof” reserved word. It not only stores the reference of instance method, but it also contains the reference of object instance for which it is being invoked.

 

To begin with delegates we define it by “Delegate” reserved word:

 

 Public Delegate Function BeginingDelegate() As String

  

 

This delegate returns the string when it calls the function. Note we need to declare the function same as delegate signature. Like:

 

   Public Function SingleCall1() As String

        Return "Delegate 1"

   End Function

 

   Public Function SingleCall2() As String

        Return "Delegate 2"

   End Function

 

We drop three buttons on windows form, on all three buttons OnClick event add the following code:

 

This method will call the single delegate: 

 

Private Sub btnSingleDelegate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSingleDelegate.Click

 

        Dim showMsg As BeginingDelegate

        showMsg = AddressOf SingleCall1

        lblMainMessage.Visible = True

        lblMainMessage.Text = showMsg.Invoke().ToString()

 

End Sub

 

//Second single call to delegate

 

Private Sub btnSecondDelegate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSecondDelegate.Click

 

        Dim showMsg As BeginingDelegate

        showMsg = AddressOf SingleCall2

        lblMainMessage.Visible = True

        lblMainMessage.Text = showMsg.Invoke().ToString()

 

End Sub

 

 

This method will Multi cast delegates:

 

Private Sub btnMutiDelegates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMutiDelegates.Click

 

        Dim Call1 As BeginingDelegate

        Dim Call2 As BeginingDelegate

 

        Call1 = AddressOf SingleCall1

        Call2 = New BeginingDelegate(AddressOf SingleCall2)

 

        Dim multiExecution As BeginingDelegate = [Delegate].Combine(Call1, Call2)

 

        lblMainMessage.Text = "Multi Cast Effect... " & multiExecution.Invoke().ToString()

 

End Sub

 

Here I have used the two methods first one is Invoke() and second is Combine(). Invoke() methods invoke the delegate and Combine() multi cast the delegate. In multi cast delegate call to delegates is synchronously. If exception rises by called method, then the delegate ceases, and the exception is forward to the delegate caller. So the best thing you can do is try to use “Try…Catch” block which makes your code more reliable for execution.

Article Comments
thank you

for giving me a clear example
all my doubt's were solved

iff, have more content kindly give me the url

Posted on 3/18/2008 11:43:07 AM by ankhenatraj

Im not getting the desired results for combine method.
its showing only second argument output only.
i want result as "Delegate 1 Delegate 2 "

Posted on 5/10/2008 2:50:52 AM by ramu

thanks
your example give every understandable details about delegates

Posted on 5/20/2008 3:59:07 AM by jaypal rajput

hi this is very good example for the delegate events

Posted on 11/17/2008 11:48:51 PM by madhavareddy

very nice! easy to understand. keep it up:)

Posted on 3/9/2010 2:38:56 AM by Joe

thank you

Posted on 3/10/2010 7:00:10 AM by m

It's very useful for me i understand very in basic level i need to know more please help me

Posted on 3/31/2010 11:16:59 AM by Lessly

Send me an email about this article when other users post a CommentSend me an email about this article when other users post a Comment

Posted on 7/15/2010 7:14:24 AM by gjgh

ha is very intresting easy to learn

Posted on 11/15/2010 2:22:38 PM by madhavareddy

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get IP Address Machines on the Local Area network from Machine Name using System.DirectoryServices NameSpace .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net