With the event keyword a subscription mechanism can be done that is based on delegates. Again, all languages define an event keyword for offering events from a class. The class EventDemo fires events with name DemoEvent of type DemoDelegate.
In C# the syntax for firing the event looks like a method call of the event. The event variable is null as long as nobody registered to the event, so a check for not null must be done before firing the event. The handler method is registered by using the += operator and passing the address of the handler method with the help of delegate inference.
// C# public class EventDemo { public event DemoDelegate DemoEvent; public void FireEvent() { if (DemoEvent != null) DemoEvent(44); } } public class Subscriber { public void Handler(int x) { // handler implementation } } //... EventDemo evd = new EventDemo(); Subscriber subscr = new Subscriber(); evd.DemoEvent += subscr.Handler; evd.FireEvent();
C++/CLI is very similar to C# with the exception that firing the event does not require first checking to see that the event variable is not null. This is automatically done by the IL code created from the compiler.
Tip | Both C# and C++/CLI use the -= operator to unregister from an event. |
// C++/CLI public ref class EventDemo { public: event DemoDelegate^ DemoEvent; public void FireEvent() { DemoEvent(44); } } public class Subscriber { public: void Handler(int x) { // handler implementation } } //... EventDemo^ evd = gcnew EventDemo(); Subscriber^ subscr = gcnew Subscriber(); evd->DemoEvent += gcnew DemoDelegate(subscr, &Subscriber::Handler); evd->FireEvent();
Visual Basic has a different syntax. The event is declared with the Event keyword, which is the same as with C# and C++/CLI. However, the event is raised with the RaiseEvent statement. The RaiseEvent statement checks if the event variable is initialized by a subscriber. To register a handler, the AddHandler statement has the same functionality as the += operator with C#. AddHandler requires two parameters: the first defines the event, the second the address of the handler. The RemoveHandler statement is used to unregister a handler from the event.
' Visual Basic Public Class EventDemo Public Event DemoEvent As DemoDelegate public Sub FireEvent() RaiseEvent DemoEvent(44); End Sub End Class Public Class Subscriber Public Sub Handler(ByVal x As Integer) ' handler implementation End Sub End Class '... Dim evd As New EventDemo() Dim subscr As New Subscriber() AddHandler evd.DemoEvent, AddressOf subscr.Handler evd.FireEvent()
Visual Basic offers another syntax that is not available with the other languages: you can also use the Handles keyword with the method that subscribes to the event. The requirement for this is to define a variable with the WithEvents keyword:
Public Class Subscriber Public WithEvents evd As EventDemo Public Sub Handler(ByVal x As Integer) Handles evd.DemoEvent ' Handler implementation End Sub Public Sub Action() evd = New EventDemo() evd.FireEvent() End Sub End Class