How to: implement interface events (C # programming guide)

Source: Internet
Author: User

C # programming guide

How to: implement interface events (C # programming guide)

The interface can declare events. The following example shows how to implement interface events in a class. The implementation rules of interface events are basically the same as those of any interface methods or attributes. Implement an interface event in the class to declare the event in the class, and then call the event at the appropriate location. Copy the public interface idrawingobject {event eventhandler shapechanged;} public class myeventargs: eventargs {...} Public class shape: idrawingobject {event eventhandler shapechanged; void changeshape () {// do something before the event... Onshapechanged (New myeventsargs (...)); // Or do something after the event.} protected virtual void onshapechanged (myeventargs e) {If (shapechanged! = NULL) {shapechanged (this, e) ;}} the example below demonstrates how to deal with the following uncommon situations: Your class is inherited from more than two interfaces, each interface contains an event with the same name ). In this case, you must provide at least an explicit interface implementation for one of the events. When you compile an explicit interface implementation for an event, you must write AddAnd RemoveEvent accessors. These two event accessors are generally provided by the compiler, but in this case the compiler cannot. You can provide your own accessors to specify whether these two events are represented by the same event in your class or by different events. For example, according to the interface specification, if an event should be triggered at different times, you can associate each event with a separate implementation in the class. In the following example, the user forcibly converts a shape reference IshapeOr IdrawingobjectTo determine which one you will receive OndrawEvent. C # copy the code namespace wraptwointerfaceevents {using system; public interface idrawingobject {// raise this event before drawing // the object. event eventhandler ondraw;} public interface ishape {// raise this event after drawing // the shape. event eventhandler ondraw;} // base class event publisher inherits two // interfaces, each with an ondraw event publicclass shape: idrawingobject, ishape {/ /Create an event for each interface event eventhandler predrawevent; event eventhandler postdrawevent; // explicit interface implementation required. // associate idrawingobject's event with // predrawevent event eventhandler idrawingobject. ondraw {Add {predrawevent + = value;} remove {predrawevent-= value ;}// explicit interface implementation required. // associate ishape's event W Ith // postdrawevent event eventhandler ishape. ondraw {Add {postdrawevent + = value;} remove {postdrawevent-= value ;}// for the sake of simplicity this one method // implements both interfaces. publicvoid draw () {// raise idrawingobject's event before the object is drawn. eventhandler handler = predrawevent; If (handler! = NULL) {handler (this, new eventargs ();} console. writeline ("drawing a shape. "); // raiseishape's event after the object is drawn. handler = postdrawevent; If (handler! = NULL) {handler (this, new eventargs () ;}} publicclass subscriber1 {// references the shape object as an idrawingobject public subscriber1 (shape) {idrawingobject d = (idrawingobject) shape; D. ondraw + = new eventhandler (d_ondraw);} void d_ondraw (Object sender, eventargs e) {console. writeline ("sub1 es the idrawingobject event. ") ;}// references the shape object as an ishape publicclass subscriber2 {public subscriber2 (shape) {ishape d = (ishape) shape; D. ondraw + = new eventhandler (d_ondraw);} void d_ondraw (Object sender, eventargs e) {console. writeline ("sub2 has es the ishape event. ") ;}} publicclass program {staticvoid main (string [] ARGs) {shape = new shape (); subscriber1 sub = new subscriber1 (SHAPE ); subscriber2 sub2 = new subscriber2 (SHAPE); shape. draw (); console. writeline ("press enter to close this window. "); console. readline () ;}} output sub1 events es the idrawingobject event. drawing a shape. sub2 has es the ishape event. (Source: msdn)
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.