Fireing an inherited event 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?
 
P

Peter Duniho

[...]
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, you are correct.
Why is that?

Because the way your code is using the event, you're using the compiler's
auto-implemented event, which is hiding some of the mechanical aspects
from you.

You should the MSDN documentation on events, so that you fully understand
them. Here's a good place to start:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

But briefly:

An event is actually a pair of methods: add, and remove. When you write
"public event EventAdded OnEventAdded;", the compiler is doing a couple of
things for you:

-- Creating a hidden field for storing the delegate that represents
the event
-- Generating those two methods for allowing the field to be modified
when the methods are called

Then, the name of the event has two completely different meanings
depending on context. If you use the name of the event in the class where
the event is declared, it actually refers to that hidden field. But
_anywhere else_, it refers to the event itself (i.e. the class member that
has the add and remove methods). And since an event is only those two
methods, that's all any code outside the class can do: add or remove a
delegate instance to or from the event.
How can use the event at the derived class to fire it?

There are lots of possibilities, but IMHO you should follow the .NET
examples barring some specific need to do otherwise. In particular:

-- For each event a class declares, in that class create a protected
virtual method that is used to raise the event
-- In derived classes where you want to be able to raise the event,
call that method
-- In derived classes where you want to be able to modify the behavior
of the event, override that method

Note also the naming convention used in .NET: the event name would be
something like "EventAdded", while that protected method is named
"OnEventAdded". You should try to stick to the .NET conventions,
especially for public portions of your class, to make it easier for others
to read your code and to avoid confusion about how it works.

Pete
 

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