C # Foundation-Event inheritance class cannot directly raise events of base class

Source: Internet
Author: User

An event can being raised only from the declaration space in which it is declared. Therefore, a class cannot raise events from any other class, even one from which it is derived.

An event can only be used in the declaration space (class) it declares. Therefore, it cannot be thrown from any other class, even if the class is the inheriting class of the class in which the event resides.

So how can we trigger the events in the base class and give an example to everyone to see.

namespacebaseclassevents{usingSystem; usingSystem.Collections.Generic; //special EventArgs class to hold info about Shapes.     Public classShapeeventargs:eventargs {Private DoubleNewarea;  PublicShapeeventargs (Doubled) {Newarea=D; }         Public DoubleNewarea {Get{returnNewarea;} }    }    //Base class Event Publisher     Public Abstract classShape {protected DoubleArea ;  Public DoubleArea {Get{returnArea ;} Set{area =value;} }        //The event. Note that by using the generic eventhandler<t> event type//We don't need to declare a separate delegate type.         Public EventEventhandler<shapeeventargs>shapechanged;  Public Abstract voidDraw (); //The event-invoking method that derived classes can override.        protected Virtual voidonshapechanged (Shapeeventargs e) {//Make a temporary copy of the event to avoid possibility of//a race condition if the last subscriber Unsubscribes//immediately after the null check and before the event is raised.eventhandler<shapeeventargs> handler =shapechanged; if(Handler! =NULL) {Handler ( This, E); }        }    }     Public classCircle:shape {Private Doubleradius;  PublicCircle (Doubled) {radius=D; Area=3.14* RADIUS *radius; }         Public voidUpdate (Doubled) {radius=D; Area=3.14* RADIUS *radius; Onshapechanged (NewShapeeventargs (area)); }        protected Override voidonshapechanged (Shapeeventargs e) {//Do any circle-specific processing here. //Call the base class event invocation method.            Base.        Onshapechanged (e); }         Public Override voidDraw () {Console.WriteLine ("Drawing a circle"); }    }     Public classRectangle:shape {Private Doublelength; Private Doublewidth;  PublicRectangle (DoubleLengthDoublewidth) {             This. length =length;  This. width =width; Area= length *width; }         Public voidUpdate (DoubleLengthDoublewidth) {             This. length =length;  This. width =width; Area= length *width; Onshapechanged (NewShapeeventargs (area)); }        protected Override voidonshapechanged (Shapeeventargs e) {//Do any rectangle-specific processing here. //Call the base class event invocation method.            Base.        Onshapechanged (e); }         Public Override voidDraw () {Console.WriteLine ("Drawing a rectangle"); }    }    //represents the surface on which the shapes is drawn//subscribes to shape events so that it knows//When to redraw a shape.     Public classShapeContainer {List<Shape>_list;  PublicShapeContainer () {_list=NewList<shape>(); }         Public voidAddShape (Shape s) {_list.            ADD (s); //Subscribe to the base class event.S.shapechanged + =handleshapechanged; }        // ... Other methods to draw, resize, etc.        Private voidHandleshapechanged (Objectsender, Shapeeventargs e) {Shape s=(Shape) sender; //Diagnostic message for demonstration purposes.Console.WriteLine ("Received event. Shape area was now {0}", E.newarea); //Redraw the shape here.S.draw (); }    }    classTest {Static voidMain (string[] args) {            //Create The event Publishers and subscriberCircle C1 =NewCircle ( Wu); Rectangle R1=NewRectangle ( A,9); ShapeContainer SC=NewShapeContainer (); //ADD The shapes to the container.SC.            AddShape (C1); Sc.            AddShape (R1); //cause some events to be raised.C1. Update ( $); R1. Update (7,7); //Keep the console window open in debug mode.System.Console.WriteLine ("Press any key to exit.");        System.Console.ReadKey (); }    }}/*output:received event. Shape area was now 10201.86 Drawing a circle Received event. Shape area are now Drawing a rectangle*/

Reference

How To:raise Base Class Events in Derived Classes (C # Programming Guide)

C # Foundation-Event inheritance class cannot directly raise events of base class

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.