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

A guide to work with Delegates in .NET

This article describes on the StaticCallbacks method to exhibit the different ways of using callback delegates. Delegates’ holds up a dynamic event programming sculpt and transient procedural types as arguments.

When you are programming .NET, the delegate class is considered one of the vital amongst all the classes. It is regarded as the “spine” of the VB .NET event’s model. Let us get to know about the delegates i.e. System.Delegates class and which are also strong to handle the event procedures.

How it is implemented?


  • Delegates are put into practice making use of the delegate class establish in the System namespace.
  • It is a class that is obtained from System.MulticastDelegate. A delegate is considered as a data structure, it points to a static routine or we can say to a class instance and an instance method of that class.
  • It is used like a type safe pointer as well. Like a callback operator, one can use a delegate to refer to a method as well. Other example other than call back can be of “AddressOf” operator.
  • They are most of the time utilized for asynchronous programming and are the perfect technique for commonly defining events.

Prior to entering into the real delegate class, it is important to have a glimpse to some code which replicates the delegate technique or the functionality. This is made possible via easy class code for the understanding purposes. The below code shows a simple class namely “VBDelegate” and two static methods “Call Delegate” and “Display Message”. CallDelegate method is called to exhibit the message.

Listing 1: VBDelegate 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)
}
}

There doesn’t seem to be anything wrong following this procedure. This is in fact more often used than the delegates. But it carries along with it a drawback that it is unable to offer anything in flexibility terms or you can say it is not capable of providing a dynamic event model.

However, if we talk of the delegates in .NET, one can easily pass a method to the other you can pass a let it carry out the method as an alternative. Till the time you run your code, you can never know about which method to call for thereby making the delegates an important part during the same. Let us try to put in the same functionality as we did above making use of the real delegate class.

How are delegates declared in .NET?

Delegates are declared as Delegate [Function/Sub] methodname(arg1,arg2..argN)

Similar method signature is made us 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 shown below.

Listing 2: Declaring Delegates

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);
}
}

One message window displays the output as “Former Delegation” and the other displays the message as “Later Delegation”.

Let us have a brief in about the above written code. Initially, we defined a delegate which is on identical lines to defining the method’s signature. The code defines clearly that a delegate is required that is capable of accepting a string in the form of argument. In other words, the delegate has the ability to work with any sort of method that has the capacity to take in identical arguments.

Two instances are formed of ‘MyDelegate’ for the CallDelagates method. Post doing the same, it is made to run through the MyDelegate's constructor. This is nothing but the address of the 'DisplayMessage' method. In simple words, the method that we made to ran through the constructor is mandatory to possess the signature method.

Wondering, "Why are we entering in the address of a method at the time of defining the delegate to allow a string entity as its key argument?" We are actually informing the delegate as to which method it should call and not the string we're passing in.

Finally we have 'DisplayMessage' method- It takes the input as the string passed in by the delegate and informs as to which string to be displayed

The CompareFirstNames scheme requests String.Compare to evaluate the FirstName properties of the two Name instances. The String class has numerous convenience schemas. Example includes Compare that is used for working with strings. One should never permit to make use of this method to come n between the Delegates learning process. You should just understand that this is a handler process that is used by delegates just for the reference irrespective of what code lies inside that method.

Create an instance of the same in order to make use of a delegate. This is created much on the similar lines of a class instance making use of a single alone parameter. This parameter recognizes the proper delegate method required for handling.



I am a software developer from India with hands on experience on java, html for over 5 years.

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