Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to implement delegates in .NET

We will learn today how to work with delegates in .NET and we will take a look on the delegate methods that are required for handling the class instances.

We will see today the process to implement delegates in .NET that takes up a dynamic event programming and arguments.

We cannot ignore the delegate classes when the application is being developed in .NET since this is considered to be one of the most vital of all the classes. The delegate classes that we are going to talk about today in this article hold up a good portion to manage the event procedures.

Ways to implement Delegates

The delegate class that has been established in the system namespace is made use of to put Delegates into practice. The delegate class is retrieved from System.MulticastDelegate and is regarded as a data structure that pin points to a static routine or to a class instance and an instance method of that class. This is used on much similar lines of a type safe pointer as well. Also, like a call operator does, delegates can also be made use of to point to a method or procedure. “AddressOf” can be considered as one of the other examples apart from the call back.

Uses of Delegate Class:

Delegates are mostly made use of while doing asynchronous programming. It is also considered as one of the finest processes for most commonly used events. Before one enters the actual delegate class, the point to note would be to take a look at the code that is exactly similar to the delegate process or functionality. How this is made possible? This can be achieved with the help of class that is known as “VBDelegate”. Also we have two static methods “Call Delegate” and “Display Message” to use the same. We have a CallDelegate method that is used to display the same.

Delegate Class:

VB.NET

Public Class VBDelegate
Static Method that calls display message function
Public Shared Sub CallDelegate()
DisplayMessage("Any text message")
End Sub
Function that displays the message
Private Shared Function DisplayMessage(ByVal strTextOutput As String)
MsgBox(strTextOutput)
End Function
End Class

C#

Public Class CSDelegate{
'Static method that calls the display message function
Public static void CallDelegate()
{
DisplayMessage("Any text message")
}
'Static Function that displays the message
Private static void DisplayMessage(String strTextOutput)
{
MessageBox.Show(strTextOutput)
}
}

We don’t see anything wrong over here if the developer follows this process and is most often used as compared to delegates.

Drawback: The drawback over here using this method is that developer will be unable to provide anything as far as flexibility is concerned in terms of a dynamic event model.

Being a developer, one can with no issues pass a method to the other and then allow the same to carry out the method as a substitute. Until and unless the code is executed or implemented, there is now way to figure out the method that should be called for. And this makes the delegates as one of the important part of the process. We will make use of the real delegate class to implement the same functionality.

Declaring Delegates

The method to declare the delegate is as Delegate [Function/Sub] methodname(arg1,arg2..argN)

The same method one can make use of by the declared delegate method name as the ones they desire to be a delegate for and is known by the name of shared method as can be seen below.

VB.NET

Class VBDelegate
Delegate Function MyDelegate(ByVal strOutput As String)
Public Shared Sub CallDelegates()
Dim d1 As New MyDelegate(AddressOf MesssageDisplay)
Dim d2 As New MyDelegate(AddressOf MesssageDisplay)
d1("Former Delegation ")
d2("Later Delegation ")
End Sub
Private Shared Function MessageDisplay(ByVal strTextOutput As String)
MsgBox(strTextOutput)
End Function
End Class

C#

Class CSDelegate
{
Stating delegate variable
Delegate void MyDelegate(String strOutput);
'Function calling the delegates
Public static void CallDelegates()
{
MyDelegate d1 = New MyDelegate(MesssageDisplay);
MyDelegate d2 = New MyDelegate(MesssageDisplay);
d1("Former Delegation ");
d2("Later Delegation ");
}
Private static void MessageDisplay(String strTextOutput)
{
MessageBox.Show(strTextOutput);

The output is displayed as “Former Delegation” on one of the message window whereas “Later Delegation is being displayed on the other one. Explanation of the code:

In the beginning, we defined a delegate that was much on the similar lines of the defining the method’s signature. It can be seen or notice clearly that there is a requirement of a delegate that should have the potential to accept string in the argument form or we can say that the delegate has the ability to work with any sort of method that has the capacity to take in identical arguments.

We have two instances of ‘MyDelegate’ for the CallDelagates method and after creating these, they are forced to pass through the MyDelegate's constructor which is nothing but the address of the 'DisplayMessage' method.

Let us see the reason to enter the address of a method when the delegates are defined to permit a string entity as one of the key arguments. This is being done so as to inform the delegate that which method should be called and not the string that is being passed in.

In the end, there is a 'DisplayMessage' method that takes in the input as the string. This string is passed in by the delegate and notifies as to which string should get displayed.



Software developer with more than 5 years of development on Java, HTML, CSS.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login