Delegates in VB.NET

Delegates in VB.NET

Delegates are similar to function pointer in C, C++. Now,  if would be wondering what is pointer, if you have not know C, C++ or Java before.

Here, below is 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 Sub

 

End Class

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!

Great, now, think of passing function as 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 parameter, and you will pass DoSomeWork as parameter to this function.

Yes, you can do this in .NET

I you didn’t know this, it would WOW for you. It happens to me itself.

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

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

Few things, you should know about the Delegate,

  1. Declaration Delegate is like normal Procedure/Method in VB.NET, which accepts parameter, but, doesn’t return anything.
  2. Delegate does not return any value. This is general statement, exceptions are there, but for now basic, keep in mind, they do not return any value.
  3.  
  4. Delegate have only declaration, they do not have any body.

  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

  1. To use Delegate, you need to attach some function to it. Delegates own it own does nothing.

Here is the example to use 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 think! Don’t sweat, will explain you below

To understand this, let go line by line

1)      First Line

    Public Delegate Sub SomeWorkDelegate(ByVal strSomeString As String)

Here, in the line, you have declared a Delegate which will accept String as parameter.

Now, it like this will in future, will point to some function which would accept just one String as Parameter.

What, I have some function which have input as Two String Parameter, sorry, folks, can’t use this delegate to point to your function, you need to declare another Delegate which has input as two string Parameter

 

2)      First Function

‘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 tell VB.NET, it expecting some function as parameter which as same signature as Delegate ‘SomeWorkDelegate’ i.e. Only One parameter as input, and that too, need to be String.

This mean, I can pass all the functions in the world to this function, which have one parameter, and that is String

This down, that the hell I am going to do with parameter which in Function by itself.

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. So,

someDelegateFunc(“Here is the work”)

This line, I am passing parameter as string.

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.

3)      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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s