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 ExpandoObject and DynamicObject classes. The ExpandoObject class refers to a class whose members can be explicitly added and removed at runtime. In other words, the Exp

Describe the EventLog class. The EventLog class is used to access the Windows event logs from Windows services. Using EventLog, you can also customize Windows event logs that r

ASP.NET/MSSQL/ MVC completing backend solution + integrating with frontend. Project Description: System is used for tracking company's activities through several projects by

Define the ToolTip control. How can you associate it with other controls? The ToolTip control produces a small pop-up window with explanatory text for an element it is displaye

A fresh web project utilizing ASP.NET 4.5, MVC 5 with Visual Studio 2013 a) Excellent web layout DESIGN skills using CSS. b) Solid experience with ASP.NET 4.0 MVC 4. c) Cu

How do we access attributes using "XmlReader"? The below snippets represents the way to access attributes. At First in order to check whether there any attributes present in th

What are LINQ query expressions? A LINQ query, also known as a query expression, having of a combination of query clauses that identify the data sources for the query. It conta


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

Why The JavaScript Validation Not Run on the Asp.Net? The Asp.Net Button Is post backed on the server & not yet Submit & when It goes to the server its states is lost so if we