How are Delegates used?

Hi All,

What is the purpose of delegate Can't we call the methods directly instead of assigning to the AddressOf (refer my example). Please tell me the real trime implementation of delegate.

EX:


Private
Delegate Sub MyDelSub()

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim del As MyDelSub
del = New MyDelSub(AddressOf DisplayMessage)
del.Invoke()
End Sub

Private Sub DisplayMessage()
MessageBox.Show("Delegate - Demo")
End Sub

Thanks & Regards,
Sriram S




Answer this question

How are Delegates used?

  • Mihir Vaidya

    Delegates are used to guarantee that any methbod can be passed to it but that the method HAS to implement certain parameters.

    For example
    Private Delegate Sub MyDelSub(ByVal sender as Int32)
    ensures that any delegate that is passed of the type MyDelSub must accept an integer.

  • Alias2006

    hi,

    more over to what david said , the events use delegate extensively,

    who wrote the button class that you use in your forms doesn't know which method will be used to handle this click event so he used delegate, you have to register with this event through delegate

    i think of it as pattern for methods that will use this event


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler Button1.Click, AddressOf mymethod
    End Sub

    Private Sub mymethod(ByVal sender As Object, ByVal e As EventArgs)
    MessageBox.Show(
    "this button is clicked")
    End Sub


    hope this helps



  • Hong bo Dai

    Lets not forget one of the most important functions for the humble delegate is in the use of Async data callbacks... without them, realtime data aware applications would not be possible and your screen would hang most of the time...

    Cheers & Beers



  • Roland Hasenöhrl

     

     Your example is basically a function pointer

     Delegates can be described as a managed version of a function pointer.

      A class that needs to raise events must define one delegate per event.

     Types that use the class must implement one event handler method per event that must be processed. 

     The Difference between a delegate and function pointer is, delegates can reference both instance and static (also called shared) methods, while function pointers can only reference static methods.


     Purpose of delegates

     Its a mechanism used to implement event handling in .NET Framework code. Also a class that needs to raise events must define one delegate per event.



  • Mirronelli

    Delegates allows you to call methods that aren't known at compile time. A good example of this is the List(Of T) type, it makes extensive use of delegates to perform certain functions, for example sorting:



    Module Module1
     
     Sub Main()
     
      Dim list As New List(Of Integer)
     
      list.Add(2)
      list.Add(4)
      list.Add(1)
      list.Add(5)
      list.Add(3)
     
      For Each number As Integer In list
     
       Console.WriteLine(number)
     
      Next
     
      list.Sort(AddressOf SortNumbers)
     
      For Each number As Integer In list
     
       Console.WriteLine(number)
     
      Next
     
     End Sub
     
     Function SortNumbers(ByVal one As Integer, ByVal two As Integer) As Integer
     
      Return one < two
     
     End Function
     
    End Module


     

    In the above example, the developers of the List(Of T).Sort method do not know of the SortNumbers method at compile time, so instead the Sort method takes a delegate which gets called at runtime to determine if one number is greater than the other.



  • How are Delegates used?