Previous | Home | Next |
A delegate in VB language allows us to reference a method. If we are using a C or C++ then we would sound familiar because a delegate because it is basically a function pointer. Delegates have other uses in addition to event handling. Delegate maintains three important pieces of information
- The name of the method on which it make calls.
- Any argument (if any) of this method.
- The return value (if any) of this method.
What is a Function Pointer
Function Pointers are pointers, i.e. variables, which point to the address of a function.
Types of Delegate
- Single Cast delegate
public delegate <return type> <delegate name>(parameters)
- Multi Cast delegate
public delegate void <delegate name>(parameters)
Advantage of using Delegate
Dynamic binding can be done by using delegate because we can call more than one methods at a time dynamically by calling the delegate in which the methods is defined.
Example
Namespace delgnew Public Delegate Function Delg(x As Integer, y As Integer) As Integer Public Class Math Public Shared Function Add(x As Integer, y As Integer) As Integer Return x + y End Function Public Shared Function Multiply(x As Integer, y As Integer) As Integer Return x * y End Function End Class Class Fun Private Shared Sub Main(args As String()) Dim del1 As New Delg(AddressOf Math.Add) Dim add1 As Integer = del1(7, 7) Console.WriteLine("7 + 7 = {0}" & vbLf, add1) Dim del2 As New Delg(AddressOf Math.Multiply) Dim multiply1 As Integer = del2(7, 7) Console.WriteLine("7 * 7 = {0}", multiply1) Console.ReadLine() End Sub End Class End Namespace
Static Delegates
Denoting static field is meaning that it will not be modified. You can invoke delegates without declaring a local delegate instance. Just pass in the static delegate of the class.
Delegates as Properties
The problem with static delegates is that they must be instantiated whether or not they are ever used.
Event
An event might be a button push, a menu selection in short we can cay that something happens and you must respond to it. You cannot predict the order in which events will arise.
Example
when you click a button, it might raise the Click event. When you add to a drop-down list, it might raise a List Changed event.
Defining Event
Class Eventtest Public Custom Event myfun As EventHandler AddHandler(ByVal value As EventHandler) Console.WriteLine("Event Fired") End AddHandler RemoveHandler(ByVal value As EventHandler) Console.WriteLine("Controlled") End RemoveHandler End Event Private Shared Sub Main() Dim et As Eventest = New Eventtest() et.myfun += New EventHandler(et.DoNothing) et.myfun -= Nothing End Sub Private Sub DoNothing(sender As Object, e As EventArgs) End Sub End Class
An event allows a class (or other object) to send notifications to other classes (or objects) that something has occurred. In simple terms an event is the outcome of a specific action. If you have developed programmers in graphical user interfaces (GUI) then you are very familiar with Event handling.
When a user interacts with a GUI control (e.g., clicking a button on a form), one or more methods are executed in response to the above event. Events can also be generated without user interactions. Event handlers are methods in an object that are executed in response to some events occurring in the application.
Previous | Home | Next |