Inherited event firing - compiler error

S

Sharon

Hello gurus,

I have an abstract class:
public abstract class MyBase
{
public delegate void EventAdded(Int32 EventID);
public event EventAdded OnEventAdded;
}

And a derived class:
public class MyDerived : MyBase
{
Public void Func()
{
int n = 0;
if( OnEventAdded != null )
{
OnEventAdded(n);
}
}
}

And the compiler throw the error:
Error 1 The event MyDerived.OnEventAdded' can only appear on the left hand
side of += or -=

It seems the derived class can use the base class event only as += or -= and
ca not fire it.

Am I correct?
Why is that?

How can use the event at the derived class to fire it?
 
J

Jeroen Mostert

Sharon said:
I have an abstract class:
public abstract class MyBase
{
public delegate void EventAdded(Int32 EventID);
public event EventAdded OnEventAdded;
}

And a derived class:
public class MyDerived : MyBase
{
Public void Func()
{
int n = 0;
if( OnEventAdded != null )
{
OnEventAdded(n);
}
}
}

And the compiler throw the error:
Error 1 The event MyDerived.OnEventAdded' can only appear on the left hand
side of += or -=

It seems the derived class can use the base class event only as += or -= and
ca not fire it.

Am I correct?
Yes.

Why is that?
I'm too lazy to look this up, but basically, an event is actually quite a
bit more underwater, and for purposes of doing anything with it other than
attaching or removing a handler it should be considered private (even if
declared as public).
How can use the event at the derived class to fire it?
The proper pattern is this (mixing in the recommended use of EventArgs):

class EventAddedEventArgs : EventArgs {
private int eventID;
public int EventID { get { return eventID; } }

public EventAddedEventArgs(int eventID) {
this.eventID = eventID;
}
}

abstract class MyBase {
protected virtual void OnEventAdded(int eventID) {
EventAdded eventAdded = EventAdded;
if (eventAdded != null) EventAdded(this, new
EventAddedEventArgs(eventID));
}

public event EventHandler<EventAddedEventArgs> EventAdded;
}

public class MyDerived : MyBase {
public void Func() {
OnEventAdded(0);
}
}

If the derived class needs to customize event handling, or respond to the
event itself, it should override OnEventAdded. See also
http://msdn.microsoft.com/library/hy3sefw3
 
B

Ben Voigt [C++ MVP]

The proper pattern is this (mixing in the recommended use of EventArgs):
class EventAddedEventArgs : EventArgs {
private int eventID;
public int EventID { get { return eventID; } }

public EventAddedEventArgs(int eventID) {
this.eventID = eventID;
}
}

abstract class MyBase {
protected virtual void OnEventAdded(int eventID) {
EventAdded eventAdded = EventAdded;

The type needs to be EventHandler said:
if (eventAdded != null) EventAdded(this, new
EventAddedEventArgs(eventID));

A little case typo there, you went to all the trouble of making a local copy
for the test to handle the case where another thread unsubscribes the last
handler... and then made the call using the public field anyway.

For that matter, you could use the same exact name and use "this." to grab
the instance field, thus preventing any accidental reference to the field.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top