Delegate, DOT NET Programming

What is a delegate?

The Delegate is the class that can hold a reference to a method or a function. The Delegate class has a signature and it can only reference to those methods whose signature is compliant with the class. The Delegates are type-safe functions pointers or callbacks.

The sample code is shown below which shows an example of how to implement delegates.

Public Class FrmDelegates

Inherits System.Windows.Forms.Form

Public Delegate Sub DelegateAddString()

Private Sub FrmDelegates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub AddString()

lstDelegates.Items.Add("Running AddString() method") End Sub

Private Sub cmdDelegates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelegates. Click

Dim objDelegateAddString As DelegateAddString objDelegateAddString = AddressOf AddString objDelegateAddString.Invoke()

End Sub

End Class

In the above code there is a method called "AddString()" that adds a string to a listbox. You can also view a delegate declared as:-

Public Delegate Sub DelegateAddString()

This delegate signature is compatible with all the "AddString" method. When I say compatibility that means that there return types and passing parameter types are similar. After in command click of the button object of the Delegate is created and the method pointer is received from "AddressOf" keyword. Then by using "Invoke" method the method is invoked.

 

668_delegate.png

Figure:-Delegate in Action

Posted Date: 9/22/2012 2:59:25 AM | Location : United States







Related Discussions:- Delegate, Assignment Help, Ask Question on Delegate, Get Answer, Expert's Help, Delegate Discussions

Write discussion on Delegate
Your posts are moderated
Related Questions
What is the relationship between a class and an object? A class acts as a blue-print that explains the properties, states, and behaviors that are common to a number of objects.

Project Description: The software is developed to help individuals train both their subconscious and conscious mind so they can improve their life and get more success. - How

Develop a iPhone and Android mobile game Project Description: I am seeking a developer to create a mobile game for me. I need an endless runner game with simple controls. The

What is UDDI? The Full form of the UDDI is Universal Description, Discovery and Integration. The directory that can be used to publish and discover public Web Services. If you

Very, Very Boards does a big business in shirts, especially for groups and teams. They need a project that will calculate the price for individual orders, as well as a summary for

Develop a Rummikub Game Project Description: I require a game(Rummikub) multiplayer with source codes. I have done to much but i need to help me in Algorithm who can check

Mention the name of the directory where it is essential to locate the proxy file to use a Web service. The proxy file must be kept in the /bin directory. This directory is situ

Project Description: Program or Fan Page Poster for facebook fan pages. Major task of this program would be posting pictures on your facebook fan page from other users fan pa

What is "AutoPostBack" feature in ASP.NET? If we want to control the automatically postback in case of any event, we will need to check the attribute as true. For example on a

Investment Equation - C# Program I didn't know the exact code for computing Investment Equation. Can anybody suggest me the correct way for my problem?