ARTICLE

Delegates in VB.NET

Posted by Munesh Sharma Articles | VB.NET Articles March 23, 2011
In this article we will discuss about the Delegates functionality.
Download Files:
 
Reader Level:

Delegate:

Delegates play an important roll in the programming model of the .NET framework. They provide valuable support for developers who need to design and write software that use the callback notification. for example Event-handling is based on Callback. 

Callback:

Application software often designed around the programming technique known as Callback. In a callback, one part of an application sends out notification to alert other parts of an application when something interesting has occurred. More specifically, a callback is call from a notification source back to method implemented by one or more handlers.

Definition of Delegates:

Delegates is nothing but, it is work as a function pointer.

Define Delegates:

In Visual Basic .NET, you define a delegate type by using the Delegate key word each delegate definition you create must include a type name and calling signature for handler method.

Public Delegate Sub delegatename1()
'Deligate without parameters

Public
Delegate Sub delegatename2(ByVal a As Integer)
'Deligate with parameters
 

Note: There are various points to remember during delegates declaration

  • Delegate can not use as a Private Delegate.
  • Out parameter is not allowed in Delegate.
  • Delegate type must be defined using either the sub keyword or the function keyword.

Creating Delegate Object:-When you want to create a delegate object, you typically use
the new operator followed by the name of the delegate type.

Dim Handler1 As delegatename1
Handler1=new delegatename1(Addressof objectname.methodname)

Note:
There are various points to remember during delegates object creation

  • Handler1 is not an object, it is a reference Id variable that can hold all reference id.
  • Before you can create delegate object, you must determine the handler method to
    which it should bind. A handler method can be either a shared or an instance method.

Coding for Delegates:

Module Module1
    Public Delegate Sub Display()
    Public Class Par
        Public Sub View1() 
            Console.WriteLine("hello world"
        End Sub
    End Class
    Class Chal : Inherits Par  
        Public Sub View2()
            Console.WriteLine("World is Beautiful")
        End Sub
    End Class
 
    Sub Main() 
        Dim obj As New Chal
        Dim obj1 As Display
        Dim obj2 As Display
        obj1 = New Display(AddressOf obj.View1)
        obj2 = New Display(AddressOf obj.View2)
        obj1.Invoke()
        obj2.Invoke()  
    End Sub
 
End
Module

 Benefits of Delegates:-There are various Advantages of delegates

  • Delegates provide one of the easiest ways to become involved in multithreading.

  • Delegates are useful to use the event handling.

  • delegates are also useful to window forms and asp.net.

Output:

Delegate-output.gif

  

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor