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
Explain the architecture of ADO.NET in brief. AD0.NET having of two fundamental components: The DataSet, which is disconnected from the data source and does not require to k

want to generate random numbers between 1-15 and no one should repeat until all numbers have come once....

How many types of diagrams are there in UML? There are nine types of diagrams in the UML as shown below:- 1)Use case diagram: The use case diagram describe "WHAT" of a

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

ASP Programmer I need a fixing all W3c validation errors and it is a ASP website. Have any queries please send a message and I will provide more information on project. De

Name the classes that are introduced in the System.Numerics namespace. The following two new classes are introduced in the System.Numerics namespace:  BigInteger - Refers

Briefly explain what user controls are and what server controls are and the differences among the two.   An ASP.NET control (sometimes known as a server control) is a server-si

Counting Number of Words in a String - C# Program Anyone can suggest me the output of the following program. using System;   class CountWords { public static void

What is Delegation? A delegate seems like a strongly type function pointer. Delegates can request the methods that they reference without making explicit calls to those methods

What is DISCO? The Web Service Description Language (WSDL) is a W3C specification which defines the XML grammar for describing the Web Services.XML grammar describes details li