Standard event mode and standard Event Mode

Source: Internet
Author: User

Standard event mode and standard Event Mode

From http://www.cnblogs.com/willick/p/4102174.html, only for reference

The. NET Framework defines a standard mode for event programming to ensure consistency between the. NET Framework and user code. System. EventArgs is the core of the standard mode. It is a base class without any members for passing event parameters. First, define EventArgs:

1  public class PriceChangeEventArgs : System.EventArgs {2         public readonly decimal oldPrice;3         public readonly decimal newPrice;4         public PriceChangeEventArgs(decimal oldPrice, decimal newPrice) {5 6             this.oldPrice = oldPrice;7             this.newPrice = newPrice;8         }9     }

Then define the delegate for the event. The following conditions must be met:

  • It must be of the void return type;
  • There must be two parameters, the first is the object type, and the second is the EventArgs type (subclass );
  • Its name must end with EventHandler.

Considering that it is difficult to define your own delegate for each event, the. NET Framework predefines a general delegate System. EventHandler <TEventArgs> for us:

public delegate void EventHandler<TEventArgs> (object source, TEventArgs e) where TEventArgs : EventArgs;

If you do not use the EventHandler <TEventArgs> of the framework, you need to define one:

public delegate void PriceChangedEventHandler (object sender, PriceChangedEventArgs e);

If no parameter is required, you can directly use EventHandler (<TEventArgs> is not required ). With EventHandler <TEventArgs>, we can define the event in the example as follows:

public class IPhone6 {    ...    public event EventHandler<PriceChangedEventArgs> PriceChanged;    ...}

Finally, in the event standard mode, you also need to write a protected virtual method to trigger the event. This method must be prefixed with On, and the event name (PriceChanged) must be added, and an EventArgs parameter must be accepted, as follows:

1 public class IPhone6 {2     ...3     public event EventHandler<PriceChangedEventArgs> PriceChanged;4     protected virtual void OnPriceChanged(PriceChangedEventArgs e) {5         if (PriceChanged != null) PriceChanged(this, e);6     }7     ...8 }

The following is a complete example:

Public class PriceChangedEventArgs: System. eventArgs {public readonly decimal OldPrice; public readonly decimal NewPrice; public PriceChangedEventArgs (decimal oldPrice, decimal newPrice) {OldPrice = oldPrice; NewPrice = newPrice;} public class IPhone6 {decimal price; public event EventHandler <PriceChangedEventArgs> PriceChanged; protected virtual void OnPriceChanged (PriceChangedEventArgs e ){ If (PriceChanged! = Null) PriceChanged (this, e);} public decimal Price {get {return price;} set {if (price = value) return; decimal oldPrice = price; price = value; // triggered if the call list is not empty. If (PriceChanged! = Null) OnPriceChanged (new PriceChangedEventArgs (oldPrice, price) ;}} class Program {static void Main () {IPhone6 iphone6 = new IPhone6 () {Price = 5288 M }; // subscribe to the event iphone6.PriceChanged + = iphone6_PriceChanged; // adjust the price (event occurs) iphone6.Price = 3999; Console. readKey ();} static void iphone6_PriceChanged (object sender, PriceChangedEventArgs e) {Console. writeLine ("year-end promotions, iPhone 6 for sale only" + e. newPrice +" Yuan, original price "+ e. OldPrice +" Yuan, come and grab it! ");}}

Related Article

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.