EventHandler question

  • Thread starter Thread starter Hilton
  • Start date Start date
H

Hilton

Hi,

Why does:

class A
{
public event EventHandler XYZPressed;
}

class B : A
{
void Bla ()
{
this.XYZPressed (this, EventArgs.Empty)
}
}

give this compiler error:

Error 8 The event 'ABC.DEF.XYZPressed' can only appear on the left hand side
of += or -= (except when used from within the type 'ABC.DEF')

Since B *is* an A (inheritence), you should be able to do this. Right?

This is in VS2003 and VS2005.

Thanks,

Hilton
 
Hilton said:
Why does:

class A
{
public event EventHandler XYZPressed;
}

class B : A
{
void Bla ()
{
this.XYZPressed (this, EventArgs.Empty)
}
}

give this compiler error:

Error 8 The event 'ABC.DEF.XYZPressed' can only appear on the left hand side
of += or -= (except when used from within the type 'ABC.DEF')

Since B *is* an A (inheritence), you should be able to do this. Right?

No. Class A is (nearly) equivalent to:

class A
{
private EventHandler XYZPressedHandlers;

public event EventHandler XYZPressed
{
add { XYZPressedHandlers += value; }
remove { XYZPressedHandlers -= value; }
}
}

Note that the delegate field is private, so you don't have access to it
from B.

Does that make more sense now? If not, read
http://pobox.com/~skeet/csharp/events.html
 

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

Back
Top