I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I
would like some clarify the difference between events and delegates. On page
156 I see a WinForms example of timer that uses the "WithEvents" and events.
There is another example on page 124 that shows how to use delegates to sort
an array.
I don't understand the difference between events and delegates. Are they
redundant features? How do I decide which to use?
Thanks,
Siegfried
For me, delegates basically serve two functions. This first is to sort-
of act like an Interface for a single method. When you create a
delegate, you specify the necessary method signature (return value and
parameters) that are needed to map a method to a delegate. This
relationship becomes more obvious when you look at an example of
creating a custom event.
Suppose we want to create a custom event that will expose a special
property to the event handler. One way to do this is to create a new
class that inherits from EventArgs and contains this additional
property. Once we've done that, we must define a delegate to force any
handler to have an instance of the class we just created. Finally, we
can then add the new event to our class:
////////////////////
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'// Raise our event
OnMyCustomEvent(New MyCustomEventArgs("hello, world"))
End Sub
'// Your custom event
Public Event MyCustomEvent As MyCustomEventHandler
'// Internal method to call our event
Protected Overridable Sub OnMyCustomEvent(ByVal e As
MyCustomEventArgs)
RaiseEvent MyCustomEvent(Me, e)
End Sub
End Class
'// A delegate that defines the required
'// method signature
Public Delegate Sub MyCustomEventHandler(ByVal sender As Object, ByVal
e As MyCustomEventArgs)
'// The custom EventArgs you want added
'// to your custom event
Public Class MyCustomEventArgs
Inherits EventArgs
Public Sub New(ByVal myCustomProperty As String)
Me.MyCustomProperty = myCustomProperty
End Sub
Public Property MyCustomProperty() As String
Get
Return _MyCustomProperty
End Get
Set(ByVal value As String)
_MyCustomProperty = value
End Set
End Property
Private _MyCustomProperty As String = String.Empty
End Class
////////////////////
The second (and I think coolest) feature of delegates is the super
easy way to use them for multithreaded solutions. I won't go into
using them with IAsyncCallbacks and the like, so if you'll have to hit
google or msdn for some more in depth samples.
Basically, a delegate exposes a BeginInvoke and EndInvoke method that
can be use to start and end an asynchronous method call. The following
sample will show you how to start a method processing, then perform
some other chores, and finally block until the worker method
completes. This is a very simple example, but can be expanded and
applied to many situations:
////////////////////
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim worker As New DoTheWorkAsync(AddressOf DoTheWork)
Dim result As IAsyncResult = worker.BeginInvoke("hello,
world", Nothing, Nothing)
'// Do something else while we wait
'// for DoTheWork to finish
Debug.WriteLine("Working...")
System.Threading.Thread.Sleep(1000)
Dim message As String = "the answer to life, the universe, and
everything = {0}"
MsgBox(String.Format(message, worker.EndInvoke(result)))
Me.Text = "Finished!"
End Sub
'// Delegate to use to run our method
'// asynchronously
Private Delegate Function DoTheWorkAsync(ByVal text As String) As
Integer
'// Method to call Asynchronously
Private Function DoTheWork(ByVal text As String) As Integer
System.Threading.Thread.Sleep(2500)
Return 42
End Function
End Class
////////////////////
I hope that helps to explain some of the uses of Delegates a bit
better. Let me know if you need me to clarify anything I said.
Thanks,
Seth Rowe