Still confused RRS feed

  • Question

  • User1715503494 posted

    I'm still confused about delegates in vb.net. I read a great deal of articles but there is still one thing that i can't comprehend. Maybe someone can explain to me why and what the difference is between using a delegate or directly use the procedures in the class. For example why must/should one use delegates to point at functions when u also can directly use the methods from a class. Is there a bigger advantage with delegates or can you write less code or....?????

    For example

    Public ClassInLibrary
      Public Delegate Function MyDelegate() As String
      Public Function Call1() As String
            Return "Delegate 1"
       End Function 
       Public Function Call2() As String
            Return "Delegate 2"
       End Function
    End Class
    Partial Class CodeBehindPage
    'Now i can do this
     Dim cls1 As ClassInLibrary
     Dim myDel as MyDelegate
     myDel = New MyDelegate(AddressOf cls1.Call1)
     label1.Text = myDel.Invoke
    'But I can also do this
     Dim cls1 As ClassInLibrary
     Label1.Text = cls1.Call1()
    End Class 
     So what is the advantage of using a delegate??

    Saturday, June 14, 2008 6:44 PM

All replies

  • User-125547262 posted

    In the example you have given... I don't see any benefit in using a delegate over a regular method call. If your class  ClassInLibrary had some events then it would make sense to use  delegates and handlers

    Saturday, June 14, 2008 7:20 PM
  • User1715503494 posted

    Hi Jeev,

    Thanks for your reply, but thats just the point WHEN to use delegates or WHEN can you benefit of delegates. Maybe you can provide me with a real life example that shows the real power of delegates. As said before I read a great deal about delegates and looked into many examples, so far none of them could convince me of the use of delegates.


    Sunday, June 15, 2008 4:43 AM