Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Designing a Type That Exposes an Event

There are many steps a developer must take in order to define a type that exposes one or more event members. In this section, I’ll walk through each of the necessary steps. The MailManager sample application (which can be downloaded from the Books section in Resources at http://wintellect.com/ Books) shows all of the source code for the MailManager type, the Fax type, and the Pager type.

You’ll notice that the Pager type is practically identical to the Fax type.


Step #1: Define a type that will hold any additional information that should be sent to receivers of the event notification

When an event is raised, the object raising the event may want to pass some additional information to the objects receiving the event notification. This additional information needs to be encapsulated into its own class, which typically contains a bunch of private fields along with some read-only public properties to expose these fields. By convention, classes that hold event information to be passed to the event handler should be derived from System.EventArgs, and the name of the class should be suffixed with EventArgs. In this example, the NewMailEventArgs class has fields identifying who sent the message (m_from), who is receiving the message (m_to), and the subject of the message (m_subject).

 



// Step #1: Define a type that will hold any additional information that

// should be sent to receivers of the event notification internal class NewMailEventArgs : EventArgs {

 



private readonly String m_from, m_to, m_subject;

 



public NewMailEventArgs(String from, String to, String subject) { m_from = from; m_to = to; m_subject = subject;

}

 



public String From { get { return m_from; } } public String To { get { return m_to; } } public String Subject { get { return m_subject; } }

}

       
   
 
 


Step #2: Define the event member

An event member is defined using the C# keyword event. Each event member is given accessibility (which is almost always public so that other code can access the event member), a type of delegate indicating the prototype of the method(s) that will be called, and a name (which can be any valid identifier). Here is what the event member in our MailManager class looks like.

 



internal class MailManager {

 



// Step #2: Define the event member

public event EventHandler<NewMailEventArgs> NewMail;

...

}

 



NewMail is the name of this event. The type of the event member is EventHandler<New­ MailEventArgs>, which means that all receivers of the event notification must supply a callback method whose prototype matches that of the EventHandler<NewMailEventArgs> delegate type. Because the generic System.EventHandler delegate is defined as follows.

 



public delegate void EventHandler<TEventArgs>(Object sender, TEventArgs e);

 



the method prototypes must look like the following.

 



void MethodName(Object sender, NewMailEventArgs e);

       
   
 
 


 



Step #3: Define a method responsible for raising the event to


Date: 2016-03-03; view: 681


<== previous page | next page ==>
Nbsp;   Generic Property Accessor Methods | Notify registered objects that the event has occurred
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)