What is a delegate
What is a delegate ?
Delegate is a class that can hold a reference to a method or a function. Delegate class has
a signature and it can only reference those methods whose signature is compliant with the
class. Delegates are type-safe functions pointers or callbacks.
Below is a sample code which shows a 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 there is a method called “AddString()” which adds a string to a listbox.You
can also see a delegate declared as :-
Public Delegate Sub DelegateAddString()
This delegate signature is compatible with the “AddString” method. When I mean compatibility that means that there return types and passing parameter types are same. Later in command click of the button object of the Delegate is created and the method pointer is received from “AddressOf ” keyword. Then by using the “Invoke” method the method is invoked.
2007-10-23, 6287👍, 0💬
.NET INTERVIEW QUESTIONS - How can you avoid deadlock in threading? A good and careful planning can ...
.NET INTERVIEW QUESTIONS - What are Daemon threads and how can a thread be created as Daemon? Daemon...
How do I force the Dispose method to be called automatically, as clients can forget to call Dispose ...
Once I have developed the COM wrapper do I have to still register the COM in registry? Yes.
Write out a function that prints out all the permutations of a string. For example, abc would give y...