3
Reply

What are Delegates? In which scenarios delegates come into picture. Can anybody explain with understandable examples?

    It is a procedure pointer which stores the memory address of another procedure.

     

    Example:-

    Public Class Form1

        Inherits System.Windows.Forms.Form

     

    Delegate Sub abhisek(ByVal x As Integer, ByVal y As Integer)

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

            Dim a As abhisek

            a = AddressOf rajiv

            a.Invoke(10, 20)

     

        End Sub

     

        Public Sub rajiv(ByVal x As Integer, ByVal y As Integer)

     

            Dim z As Integer

            z = x + y

            MsgBox(z)

     

        End Sub

     

    End Class

     

    Delegates are like a  object but it contains only reference of function member, neither it contain the copy  and reference of member variable of Class.
    And Return type and signature of delegates must be same as referencing methods
    Events are build up on delegate.

    Delegates takes the responsiblity of another functrion