Delegates in VB.NET

In this article we will discuss about the Delegates functionality.
  • 6558


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. 


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

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)

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)
    End Sub

 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



© 2020 DotNetHeaven. All rights reserved.