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

Experienced Outlook addin (Microsoft Office) developer Project Description: About us in a nutshell: We are a young start-up that buys a refreshing product in the field of ent

I have to develop a Google Map Fix .NET Site on Android(maybe Cross Browser issue) Project Description: A developer has implemented a characteristic which involves data from

What is marshalling and types of marshalling? The Marshaling is used when an object is converted so that it can be sent across the network or across the application domains. Th

Which are the namespaces in localization and globalization? There are 2 most important namespaces:- 1)System.Globalization - It contains classes that define culture-relate

Auto stock market robot Project Description: A total solution for the Robot trading on Nse , currency, Mcx, ncdex,using formulas or using buy sell signals . The solution s

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

What are the differences between AJAX and JavaScript? The differences among AJAX and JavaScript are given as follows: AJAX sends request to the server and does not wait for

Need a K2 Blackpearl workflows help Need a K2 Blackpearl workflows experienced developer to integrate in SharePoint 2010 and 2013. The workflow integrates with Navision 5 provid

What is a candidate key? A table may have more than one combination of columns that could uniquely identify the rows in a table; every combination is a candidate key. During th