A typical event definition using Microsoft's recommended design pattern.

Source: Internet
Author: User

// This article is picked up from <Microsoft. NET Framework programming> written by Jeffrey Richter.

1. Scenario Description

Suppose you want to design an e-mail application. When an e-mail message arrives, the user might like the message to be forwaded to a fax machine or a pager.

 

2. Designing a type that exposes an event

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

NamespaceMailevent
{

Public   Class Mailmsgevengargs: eventargs
{
Public Mailmsgevengargs ( String From, String To, String Subject, String Body)
{
This . From = From;
This . = To;
This . Subject = Subject;
This . Body = Body;
}
Public   Readonly   String From, to, subject, body;
}

Public   Class Mailmanager
{
// 1. Delegate type defining the prototype of the callback method that receiverss must implement.
Public   Delegate   Void Mailmsgeventhandler ( Object Sender, mailmsgevengargs ARGs );

//2. The event itself
Public EventMailmsgeventhandler mailmsg;

// 3. proctected, virtual method responsible for processing Ying registered objects of the event.
Protected   Virtual   Void Onmailmsg (mailmsgevengargs E)
{
// Has any objects registered interest with the event?
If (Mailmsg ! =   Null )
{
// Yes -- nofity all the objects in teh delegate linked list
Mailmsg ( This , E );
}
}

// 4. method that translates the input into the desired event.
// This method is called when a new E-mail message arrives.
Public   Void Simulatearrivingmsg ( String From, String To, String Subject, String Body)
{
Mailmsgevengargs ARGs = New Mailmsgevengargs (from, to, subject, body );
// Call the virtual method policying the object that the event occurred. If no derived type overrides this method.
// The object will nofity all the registered listeners.
Onmailmsg (ARGs );
}

}

Public   Class Fax
{
Public Fax (mailmanager mailmgr)
{
This . Mailmgr = Mailmgr;
Mailmgr. mailmsg + =   New Mailmanager. mailmsgeventhandler (receivemail );
}

Void Receivemail ( Object Sender, mailmsgevengargs ARGs)
{
Console. writeline ( " Fax has received ed a new mail \ n --------------------------------- " );
Console. writeline ( " From: {0} \ nto: {1} \ nsubject: {2} \ nbody: {3} " , ArgS. From, argS. To, argS. Subject, argS. Body );
}

ProtectedMailmanager mailmgr;
}

Class Program
{
Static   Void Main ( String [] ARGs)
{
Mailmanager mail =   New Mailmanager ();
Fax =   New Fax (Mail );
Mail. simulatearrivingmsg ( " T-renhe@gmail.com " , " Winston.he@hotmail.com " , " Event Test " , " This is a test " );
Console. Read ();
}
}
}

Here is the screenshot When execution:

 

3. The recommended procedure to define an event in your own class.

Step 1:

Define a tupe that will hold any additional information that shold be sent to receivers of the Event Notification. By convention, types that hold event information are derived fromSystem. eventargs, AndName of the type shocould end with eventargs. As you can see, this type is nothing to write home about. it simply serves as a base type from which other types can derive. incluevents don't have any additional information to pass on. for example, when a button notifies its regisered receivers that the button has been clicked, just invoking the callback method is enough information. when you're defining an event that doesn' t have any additional data to pass on, just use eventargs. empty rather than constructing a new eventagrs object.

Step 2:

Define a delegate type specifying the prototype of the method that will be called when the event fires.By convention, the name of the delegate shold end with eventhandler. Also by convention, the prototype shocould have a void return and take two parameters.The first parameter is an object that refers to the object sending the notification, and the second parameter is an eventagrs-derived type containing any additional information that receivers of the notification require.

If you're defining an event that has no additional information that you want to pass to receivers of the event, you don't have to define a new delegate; you can use the FCL's system. eventhandler delegate and pass eventargs. empty for the second parameter. the prototype of eventhandler is as follows:

Public Delegate void eventhander (Object sender, eventargs E );

Step 3:

Define an event

In the previous example, mailmsg is the name of the event.This event is of the mailmsgeventhandler type, meaning that all receivers of the Event Notification must supply a callback method whose prototype matches that of the mailmsgeventhandler delegate.

Step 4:

DefineProtected, virtual MethodResponsible for policying registered object of the event. The onmailmsg method is called when a new E-mail message arrives. this method has es an intialized mailmsgeventargs object containing additional information about the event. this method must first check to see whether any objects have regeistered interest in the event, and if they have, fire the event.

A type that uses mailmanager as a base type is free to override the onmailmsg method. this capacity gives the derived type control over the firing of the event. the derived type can handle the new E-mail message in any way it sees fits. usually, a derived type callthe base type's onmailmsg method so that the registered object has es the notification. however, the derived type might decide not to have the event forwarded on.

Step 5:

Define a method that translates the input into the desired event. Your type must have some method that takes some input and translates it into the firing of an event. in this example, the similatearrivingmsg method is called to indicate that a new E-mail message has arrived into mailmanager. simulatearrivingmsg accetps information about the message and constructs a new mailmsgeventargs object, passing the message information to its constructor. mailmanager's own virtual onmailmsg method is then called to formally submit y the mailmanager object of the new E-mail message. normally, this causes the event to be fired, policying all the registered objects. (As mentioned before, a type using mailmanager as a base type can override this behaviour ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.