Getting Ready to Wire−up: The Event Model in a Nutshell
OnAsteroidExit method (from the client class or an inner−state object) starts the "chain of events" by first
invoking the Delegate. Here is an example:
Protected Sub OnRockRedAlert()
RaiseEvent AsteroidEnter(Roc, RocLoc)
End Sub
Notice that the method raising the event is modified with the Protected keyword. Although not essential, I
recommend this to allow derived classes to override the event without attaching a Delegate to it. When
deriving from a parent class that implements the event, the derived class must always call the method in the
parent class that raises it (MyBase.OnEvent). This ensures that Delegates defined in the base−class receive
the event.
Getting Ready to Wire−up: The Event Model in a Nutshell
Let's summarize the event model or pattern preferred for .NET applications:
1. An event Delegate is constructed in your client class (or a proxy) as follows:
Delegate Function GetRocLoc(ByVal roc As Asteroids) As Coordinates
or
Public Delegate Sub EventNameEventHandler(sender As Object, _
e As EventNameEventArgs)
If you simply need to make something happen and do not need to send data about the source or the
event, you can use a simplified no−return Delegate as follows:
Public Delegate Sub EventNameEventHandler()
Remember, however, that the Delegate's method signature and the signature of the target method
must match.
2. Your client class (or a proxy) defines an event as follows:
Public Event AsteroidEnter As FireLases
or
Public Event EventName As EventNameEventHandler
which bridges the event to the Delegate.
3. Your client class (or a proxy) implements a method that is activated to raise the event. This method
signature should use the On prefix as follows:
Public Sub OnAsteroidEnter()
4. The class or object that handles the event (the target object) derives from the EventArgs base−class.
In this class, you will code methods that respond to the Delegate invocation. They will perform an
action either with or without data (source of the event or event information or both), and they may
even return data to the Delegate.
489