// NewEditionPublished - Handler method for the NewEdition event. // Note that the method matches the event's signature. void NewEditionPublished(object sender, EventArgs e) { // Must cast e to get at its contents. NewEditionEventArgs ne = (NewEditionEventArgs)e; Console.WriteLine("({0}) {1}: {2} - {3}", ne.PubDate, ne.Head, Name, Gripe); }
// Publish - Put out a new edition of the DailyNoise. // This causes the NewEdition event to be raised. public void Publish(DateTime date, string majorHeadline) { Console.WriteLine("Publication: {0}, {1}", date, majorHeadline); // Set up the event arguments. NewEditionEventArgs e = new NewEditionEventArgs(date, majorHeadline); // Call our "event raiser" method. OnNewEdition(e); }
// Main()'s handler method for the NewEdition event. static void DailyNoise_NewEdition(object sender, EventArgs e) { // Cast sender if you need information from the sending object. Publisher pub = (Publisher)sender; // Cast e if you need additional arguments with the event. NewEditionEventArgs ne = (NewEditionEventArgs)e; Console.WriteLine("({0}) {1}: {2} - {3}", ne.PubDate, ne.Head, pub.Name, "Ah, the paper's on my roof again!"); }
// OnNewEdition - This method takes the correct steps to // safely raise the NewEdition event. // Note: Always provide an OnXXX event raiser method. protected virtual void OnNewEdition(NewEditionEventArgs e) { // Save a copy (for safety in multithreaded programs). EventHandler<NewEditionEventArgs> temp = NewEdition; // Make sure there are subscribers to the event. if (temp != null) { // Actually raise the event. // 'this' is the Publisher object. temp(this, e); } }
// OnNewEdition - This method takes the correct steps to // safely raise the NewEdition event. // Note: Always provide an OnXXX event raiser method. protected virtual void OnNewEdition(NewEditionEventArgs e) { // Save a copy (for safety in multithreaded programs). EventHandler <NewEditionEventArgs> temp = NewEdition; // Make sure there are subscribers to the event. if (temp != null) { // Actually raise the event. // 'this' is the Publisher object. temp(this, e); } }