
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Set Up Event Handlers Without the Mess
|
541
9.13 Set Up Event Handlers Without the Mess
Problem
In versions of the .NET Framework previous to 2.0, the System.EventHandler dele-
gate could be used on events in which the arguments were always of type
System.
EventArgs
. This was great if you really didn’t care about any data that went along
with an event. But as you are all fine programmers and can see the possibilities of
passing data along with the event, you had to set up a delegate and an event for every
event you wanted. Example 9-14 demonstrates an old newspaper class that sends
news to subscribers using the pre-.NET 2.0 event and event-handling methodology.
Example 9-14. Using pre-.NET 2.0 event and event-handling methods
class IWannaKnowThen
{
// Show the client talking to the newspaper class.
public static void TryMe( )
{
// Make a newspaper class.
OldNewsPaper DailyPaperFlash = new OldNewsPaper( );
// Hook up the news event to our handler (StaleNews).
DailyPaperFlash.NewsEvent +=
new OldNewsPaper.NewsEventHandler(StaleNews);
// Send news.
DailyPaperFlash.TransmitNews("Patriots win first super bowl!");
DailyPaperFlash.TransmitNews("W takes office amongst recount.");
DailyPaperFlash.TransmitNews("VS2003 is sooo passe");
}
// Write out news to debug stream
private static void StaleNews(object ...