events and delegates ??

C

Chris

Hi,

what is the difference between using events and delegates (apart from the
syntax) ?

have a look at following (working) programs please
(you can just copy/paste and build it) :

First program uses delegates, the second events but both do inherently the
same :

Using delegates :
Imports System
Public Delegate Sub WakeUpDelegate()

class AClass
Public Shared Sub WakeUpA()
Console.WriteLine("WakeUpA()")
End Sub
End Class

Class Timer
Public deleg As WakeUpDelegate
Public Sub Alarm()
If Not deleg Is Nothing Then
deleg()
End If
End Sub
End Class

Class Program
Public Shared Sub Main()
Console.WriteLine("Using Delegates (VB)")
Dim objTimer As Timer = New Timer
Dim callback As WakeUpDelegate = New WakeUpDelegate(AddressOf
AClass.WakeUpA)
objTimer.deleg = CType([Delegate].Combine(objTimer.deleg, callback),
WakeUpDelegate)
objTimer.Alarm()
End Sub
End Class


Using events :
Imports System
Public Delegate Sub WakeUpDelegate()

class AClass
Public Shared Sub WakeUpA()
Console.WriteLine("WakeUpA()")
End Sub
End Class

Class Timer
Public Event deleg As WakeUpDelegate
Public Sub Alarm()
RaiseEvent deleg()
End Sub
End Class

Class Program
Public Shared Sub Main()
Console.WriteLine("Using Events (VB)")
Dim objTimer As Timer = New Timer
AddHandler objTimer.deleg, AddressOf AClass.WakeUpA
objTimer.Alarm()
End Sub
End Class



Thanks for any help
Chris
 
J

Jay B. Harlow [MVP - Outlook]

Chris,
The way I view it is:

Events are implemented in terms of a Delegate, similar to how a Property is
implemented in terms of a Field.

The Event protects the underlying Delegate, similar to how a Property
protects the underlying Field.

In other words you use Events when you want to encapsulate the Delegate,
similar to how you use a Property when you want to encapsulate a field.

See:
http://msdn.microsoft.com/library/d...bcn7/html/vaconEventsDelegatesInheritance.asp


Hope this helps
Jay
 
A

Armin Zingler

Chris said:
what is the difference between using events and delegates (apart from
the syntax) ?

have a look at following (working) programs please
(you can just copy/paste and build it) :

First program uses delegates, the second events but both do
inherently the same :
[sample code]


The difference is that an event is a MulticastDelegate, i.e. raising the
event can notify an unlimited number of objects about the event. Simplified,
an event is an array of delegates (AFAIR, in fact it is a linked list).
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
The difference is that an event is a MulticastDelegate,
A delegate can be multicast also, remember MulticastDelegate inherits from
Delegate. Although I agree with your statement in that most of the time a
Delegate variable are single callbacks.

Try the following to see what I mean.

Private Delegate Sub Stuff()

Public Sub Main
Dim a, b, c As Stuff
a = AddressOf Method1
b = AddressOf Method2
c = DirectCast([Delegate].Combine(a, b), Stuff)
c.Invoke()
End Sub

Public Sub Method1()
Debug.WriteLine("Hello", "Method1")
End Sub

Public Sub Method2()
Debug.WriteLine("Goodbye", "Method2")
End Sub

Hope this helps
Jay



Armin Zingler said:
Chris said:
what is the difference between using events and delegates (apart from
the syntax) ?

have a look at following (working) programs please
(you can just copy/paste and build it) :

First program uses delegates, the second events but both do
inherently the same :
[sample code]


The difference is that an event is a MulticastDelegate, i.e. raising the
event can notify an unlimited number of objects about the event. Simplified,
an event is an array of delegates (AFAIR, in fact it is a linked list).


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top