Chapter 25
Events
Events alter associate degree object to apprise different objects once one thing of interest happens. The item that raises the event is referred the publisher and therefore the objects that handle the event are called subscribers.
Publisher
To demonstrate the utilization of events the publisher are created initial. This may be a category that inherits from Array List, however this version can raise an occurrence whenever associate degree item is intercalary to the list. Before the event are often created a delegate is required that may hold the subscribers. This might be any reasonably delegate, however the quality style pattern is to use a void delegate that accepts 2 parameters. The primary parameter specifies the supply object of the event, and therefore the second parameter could be a kind that either is or inherits from the System.EventArgs category. This parameter sometimes contains the main points of the event, however during this example there ought not to pass any event knowledge then the bottom Eventers category are used because the parameter’s kind.
Public delegate void EventHandlerDelegate(object sender,
System.EventArgs e);
class Publisher : System.Collections.ArrayList
Event keyword
With the delegate outlined, the event are often created within the Publisher category victimization the event keyword followed by the delegate and therefore the name of the event. The event keyword creates a special reasonably delegate that may solely be invoked from at intervals the category wherever it's declared. Its access level is public so different categories are allowed to take this event. The delegate that follows the event keyword is termed the event delegate. The name of the event is usually a verb. During this case the event are raised when the item has been intercalary therefore the past-tense of the verb “Add” is employed, that is “Added”. If a pre-event was created instead, that is raised before the particular event, then the –ing style of the verb would be used, during this case “Adding”.
public event EventHandlerDelegate Added;
Alternatively, in situ of this practice event delegate the predefined System.EventHandler delegate might are used. This delegate is a dead ringer for the one outlined antecedently, and is employed within the .NET category libraries for making events that don't have any event knowledge.
Event caller
To invoke the event an occurrence caller are often created. The naming convention for this technique is to precede the event’s name with the word “On”, that during this case becomes “On Added”. The strategy has the protected access level to forestall it from being referred to as from associate degree unrelated category, and it's marked as virtual to permit etymologizing categories to override it. It takes the event arguments as its one parameter that during this case is of the EventArgs kind. The strategy can solely raise the event if it's not null, which means only if the event has any registered subscribers. To boost the event the instance reference is passed because the sender, and therefore the EventArgs object is that the object that was passed to the strategy.
Protected virtual void OnAdded(System.EventArgs e)
Raising events
Now that the category has an occurrence and a way for business it, the ultimate step is to override the ArrayList’s Add technique to create it raise the event. During this overridden version of {the technique|the tactic|the strategy} the bottom class’s Add method is initial referred to as, and therefore the result's keep. The event is then raised with the OnAdded technique, by passing to that the Empty field within the System.EventArgs category that represents an occurrence with no knowledge. Finally, the results came back to the caller.
public override int Add(object value)
{
int i = base.Add(value); OnAdded(System.EventArgs.Empty); come back i;
}
The complete publisher category currently has the subsequent look.
class Publisher : System.Collections.ArrayList
{
public delegate void EventHandlerDelegate(object sender,
System.EventArgs e); public event EventHandlerDelegate Added;
protected virtual void OnAdded(System.EventArgs e)
public override int Add(object value)
{
int i = base.Add(value); OnAdded(System.EventArgs.Empty); come back i;
}
}
Subscriber
To make use of the publisher category another category are created that may take the event.
class Subscriber
Event handler
This category contains an occurrence handler that could be a technique that has an equivalent signature because the event delegate and is employed to handle an occurrence. The name of the handler is usually the same because the name of the event followed by the “EventHandler” suffix.
class Subscriber
">
}
Subscribing to events
The publisher and subscriber categories ar currently complete. To demonstrate their use, a Main technique is intercalary wherever objects of the Publisher and Subscriber categories are created.
In order to register the handler within the Subscriber object to the event within the Publisher object, the event handler is intercalary to the event as if it absolutely was a delegate. Not like a delegate, however, the event might not be referred to as directly from outside its containing category. Instead, the event will solely be raised by the Publisher category, that during this case happens once associate degree item is intercalary thereto object.