I have this program:
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() {
There are 2 classes - one and two, two - a descendant of one. An event is declared in the class One (SomethingChanged), and it is fired by the class One and class Two. But take a look at Two.ChangeSomething - it raises an event by calling the base class method. However, if I try to trigger an event using raw code, for example
if (SomethingChanged! = null)
SomethingChanged (this);
I get a compiler error
The event 'One.SomethingChanged' can only appear on the left hand side of + = or - = (except when used from within the type 'eventstest.Program.One')
So I'm just curious why I can't use the raw code in class 2 to raise an event, but when I call the corresponding function event, the question arises?
[EDIT] Here were some clarifications: C #: creating an inherited event
source share