Click here to Skip to main content
Click here to Skip to main content

Delegates in VB.NET

, 28 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Explain the basic of Delegate in VB.NET

Delegates in VB.NET

Delegates are similar to function pointers in C, C++. Now, you would be wondering what a pointer is, if you do not know C, C++, or Java. Here, below is a simple example:

'This is sample Class for example
Public Class ShabsSampleClass
'This is sample method except as Parameter
Sub DoSomeWork(ByVal strWork As String)
Console.Write(strWork)
End Sub
'This is main function for this class
Sub Main()
Dim strDiggingWork As String
strDiggingWork = "Start Digging the Ground"
DoSomeWork(strDiggingWork)
End SubEnd Class

Great, now, think of passing a function as a parameter. What did I say? Some function as parameter. What does that mean? It means there would be some function like GetSomeWork which will accept function as a parameter, and you will pass DoSomeWork as a parameter to this function. Yes, you can do this in .NET. If you didn't know this, it would be a wow for you. It happens to me too.

When you are passing Function as parameter, it is known as Delegate

All the events like mouseclick, keypress, ApplicationStart (in ASP.NET), etc., handle some delegate in .NET. You won’t be knowing it, but VS IDE (Visual Studio Integrated Development Environment) does it for you.

A few things you should know about a Delegate

  1. Declaration Delegate is like a normal Procedure/Method in VB.NET which accepts a parameter but doesn’t return Sub in case of a Sub method and type in case of a Function.
  2. Delegate does not return any type value. Technically, a Sub delegate returns a Sub pointer but for now, keep in mind, they do not return any value.
  3. Delegates have only declaration, they do not have any body.
  4. Public Delegate Sub SomeWorkDelegate(ByVal strSomeString As String)
    'Here, there would be no body for delegate, like it has for Sub
    'No END SUB here
  5. To use a Delegate, you need to attach some function to it. Delegates on their own do not do anything.

Here is an example of how to use a Delegate:

'This is sample Class for example
Public Class ShabsSampleClass
	'This is sample method except as Parameter
	Sub DoSomeWork(ByVal strWork As String)
		Console.Write(strWork)
	End Sub
	'This is where, I create a Delegate with String Parameter
	Public Delegate Sub SomeWorkDelegate(ByVal strSomeString As String)
		'Here is some function, which will accept other function as parameter via some delegate
		Public Sub GetSomeWork(ByVal someDelegateFunc As SomeWorkDelegate)
		'call someDelegateFunc to so some owrk
		someDelegateFunc("Here is the work")
	End Sub
	'This is main function for this class
	Sub Main()
		'Declaring private delegate in this function with address of DoSomeWork function 
		Dim myWorkDel As SomeWorkDelegate = New SomeWorkDelegate(AddressOf DoSomeWork)
		'Using GetSomeWork function
		GetSomeWork(myWorkDel)
	End Sub
End Class

What the hell is this, I can’t understand a thing! Don’t sweat, I will explain it to you below. To understand this, let us go line by line:

  1. First line
  2. Public Delegate Sub SomeWorkDelegate(ByVal strSomeString As String)

    Here, in the line, you have declared a Delegate which will accept a String as parameter. Now it will in future point to some function which would accept just one String as parameter. What if I have some function which has as input two String parameters? Sorry, folks, can’t use this delegate to point to your function, you need to declare another Delegate which has as input two String parameters.

  3. First function
  4. 'Here is some function, which will accept other function as parameter via some delegate
    Public Sub GetSomeWork(ByVal someDelegateFunc As SomeWorkDelegate)
      'call someDelegateFunc to so some owrk
      someDelegateFunc("Here is the work")
    End Sub

    Here we have declared some function which would accept another function as parameter. It’s like I will accept some function as parameter which has only one parameter as string.

    (ByVal someDelegateFunc As SomeWorkDelegate)

    This parameter tells VB.NET that it is expecting some function as parameter which has the same signature as the Delegate SomeWorkDelegate i.e., only one parameter as input, and that too, need to be String. This means, I can pass all functions in the world to this function which have one String parameter. Well, there is some requirement, and I need to use this delegate function in my Function and hence I am using it. So now this function works only when I pass some parameter as String. E.g., someDelegateFunc("Here is the work").

    What in this world, I need to do something like this. Well, if your world is some College Project or some novice Project, you don’t need to do this. This is because; you know all the function, which are there in your Project. You would directly use that, instead of doing this circus of declaring the delegate and using it.
    But, you would some really Big project, like .NET framework itself, where there are 5000 classes and millions of function, and need to obey OOPS principle, and there are some 500 peoples working on this project, all across the Globe, i.e. there is some teams in US, India, Brazil, Canada, and so on! Here, you don’t know all the functions and need to add flexibility and scalability to you class you design.

    But, even, in college Project, it good to use it, as who know, the code which you have written would change the world, and puffs, you few lines project will grow to millions lines code, and your core class will be use by all other people.
    If you have use delegates, you don’t need to rewrite you core class again to adapt to other classes which depend on your Class.

    I know, this is getting bit stretchy, and it long, for you to read, so I need to wind up here.

  5. Main Function

    'This is main function for this class
    Sub Main()
    'Declaring private delegate in this function with address of DoSomeWork function 
    Dim myWorkDel As SomeWorkDelegate = New SomeWorkDelegate(AddressOf DoSomeWork)
    'Using GetSomeWork function
    GetSomeWork(myWorkDel)
    End Sub

    Here, in main function is where, we are going to put every thing together.

    Dim myWorkDel As SomeWorkDelegate = New SomeWorkDelegate(AddressOf DoSomeWork)


    Here, in your main function, you are declaring delegate which is private variable. And to this variable, you are attaching the ‘DoSomeWork’ function to this delegate. Why?
    Because, down the in this function, you are going to use this delegate.
    Since, I know ‘SomeWorkDelegate’ has one parameter, that too string, and there is another function ‘DoSomeWork’ which too has just one parameter, and too string. I am attaching this function to this delegate.

    'Using GetSomeWork function
    GetSomeWork(myWorkDel)


    Here, I want to use function GetSomeWork, which take Delegate ‘SomeWorkDelegate’ as parameter, hence, I would use this function with the ‘myWorkDel’ as parameter. ‘myWorkDel’ is just the private delegate you have declare in above line and attach ‘DoSomeWork’ it

    So, what would happen in GetSomeWork.
    This function will receive ‘myWorkDel’ as parameter, which will point to ‘DoSomeWork’ method. Hence, it would actual work it does is, this line below in GetSomeWork,

    someDelegateFunc("Here is the work")

    to compiler will look like

    DoSomeWork("Here is the work")

And rest you know. There is even more in to this event and delegate, I would refer in other, let say Part II post! For you.

Here, in this function DoSomeWork, we have pass the String parameter. Now, you would be telling, what new in this, I already knew this. I know, even more, like you can pass class, object, multiple parameters and even param Arrays as parameter. So, what new in that!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

sbrakl

India India
No Biography provided

Comments and Discussions

 
QuestionMy vote of five++ Pinmemberchayaphum23-Dec-13 20:20 
GeneralMy vote of 5 PinmemberKamonth17-Jan-13 17:31 
GeneralMy vote of 4 PinmemberSean Botha7-Jan-13 17:44 
GeneralMy vote of 3 PinmemberVishal I. Sahuji6-Jan-13 23:22 
good
QuestionMy vote of 3 PinmemberChris Quinn31-Dec-12 0:31 
QuestionNice Article Pinmembersushil Kumar Dhayal29-Nov-12 20:14 
GeneralMy vote of 5 Pinmembersushil Kumar Dhayal29-Nov-12 20:13 
GeneralMy vote of 5 Pinmembermanoj kumar choubey5-Mar-12 22:52 
GeneralMy vote of 5 PinmemberHeaven20203-Nov-10 8:31 
GeneralFew things, you should know about the Delegate Pinmemberluisxvarg28-Jun-09 18:03 
GeneralImportant aspect of Delegate: included object reference Pinmembersupercat927-Oct-08 7:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 28 Dec 2012
Article Copyright 2008 by sbrakl
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid