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
ASP.NET/MSSQL/ MVC completing backend solution + integrating with frontend. - repost Project Description: System is used for tracking company's activities through several pro

Define an event. Whenever an action takes place in a class, that class gives a notification to other classes or objects that are assigned to perform certain tasks. These notifi

What is a Link? At the lowest level, a network can have of two or more computers directly linked by some physical medium such as coaxial cable or optical fiber. Such a physical

Develop a Windows Backup Application Application Goal The goal is to prepare an easy to use application that provides the user peace of mind that all of their firewalls are b

1. Prepare  a grocery list   having four columns (Serial number, The name of the product, quantity and price)  for the month of April, 06. Font specifications for Title(Groce

What precautions we need to take while deploying satellite assemblies? When we distribute the assembly, the folder structure has to be very organized. The table below shows how

What is Reference counting in COM? The Reference counting is a memory management method used to count the number of times an object has a pointer referring to it. The very firs

Develop Skype Recorder Application + Setup + Sourcecode + NICE UI Project Description: i need an application built which will allow user to record skype audio or video calls

Assignment for inheritance based on c#

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