16.8 Implement a Custom Event Argument


Problem

When you raise an event, you need to pass an event-specific state to the event handlers.

Solution

Create a custom event argument class derived from the System.EventArg class. When you raise the event, create an instance of your event argument class and pass it to the event handlers.

Discussion

When you declare your own event types, you will often want to pass event-specific state to any listening event handlers. To create a custom event argument class that complies with the Event pattern defined by the .NET Framework, you should

  • Derive your custom event argument class from the EventArgs class. The EventArgs class contains no data and is used with events that don't need to pass event state.

  • Give your event argument class a meaningful name ending in the word EventArgs , for example, DiskFullEventArgs or MailReceivedEventArgs .

  • Mark your argument class as sealed if you do not intend other event argument classes to extend it.

  • Implement additional data members and properties to support event state that you need to pass to event handlers. It's best to make event state immutable, so you should use private readonly data members and use public properties to provide read-only access to the data members.

  • Implement a public constructor that supports the initial configuration of the event state.

  • Make your event argument class serializable so that the runtime can marshal instances of it across application domain and machine boundaries. Applying the attribute System.SerializableAttribute is usually sufficient for event argument classes. However, if your class has special serialization requirements, you must also implement the interface System.Runtime.Serialization.ISerializable . (See recipe 16.1 for details on making classes serializable.)

The following listing shows the implementation of an event argument class named MailReceivedEventArgs . Theoretically, an e-mail server passes instances of the MailReceivedEventArgs class to event handlers in response to the receipt of an e-mail message. The MailReceivedEventArgs class contains information about the sender and subject of the received e-mail message.

 using System; [Serializable] public sealed class MailReceivedEventArgs : EventArgs {     // Private read-only members that hold the event state that is to be     // distributed to all event handlers. The MailReceivedEventArgs class     // will specify who sent the received mail and what the subject is.     private readonly string from;     private readonly string subject;     // Constructor, initializes event state     public MailReceivedEventArgs(string from, string subject) {         this.from = from;         this.subject = subject;     }     // Read-only properties to provide access to event state     public string From { get { return from; } }     public string Subject { get { return subject; } } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net