Note that there are some explanatory texts on larger screens.

plurals
  1. POC# event inheritance
    primarykey
    data
    text
    <p>I have this program:</p> <pre><code> class One { public delegate void del(object o); public event del SomethingChanged; int x; public int X { get { return x; } set { x = value; OnSomethingChanged(this); } } protected void OnSomethingChanged(object o) { if (SomethingChanged != null) SomethingChanged(o); } } class Two: One { public void ChangeSomething() { //if (SomethingChanged != null) // SomethingChanged(this); OnSomethingChanged(this); } } static void Main(string[] args) { One one = new One(); Console.WriteLine(one.X); one.SomethingChanged += new One.del(one_SomethingChanged); one.X = 5; } static void one_SomethingChanged(object o) { Console.WriteLine(((One)o).X); } </code></pre> <p>There are 2 classes - One and Two, Two is descendant of One. There is event declared in class One(SomethingChanged) and it is triggered by class One and class Two. But take a look at Two.ChangeSomething - it raises event by invoking base class's method. However if I try to invoke event using raw code like</p> <pre> if (SomethingChanged != null) SomethingChanged(this); </pre> <p>I'm getting compiler error saying</p> <pre> The event 'One.SomethingChanged' can only appear on the left hand side of += or -= (except when used from within the type 'eventstest.Program.One') </pre> <p>So I'm just curious why I can't use 'raw' code in class Two to raise event but when I invoke a corresponding function event gets raised?</p> <p>[EDIT] Found some clarification here: <a href="https://stackoverflow.com/questions/756237/c-raising-an-inherited-event">C#: Raising an inherited event</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload