deriving a delegate

E

Eric T.

I’ve got a class B that has delegates for users to hook up to.

class B
{
public delegate void KeyHandler (object sender, EventArgs e);
public virtual event KeyHandler OnKey;
....
Internally, in method F, I might do:

if (OnKey != null)
OnKey(this, whatever);
....
}

Now I want to derive from this class, and call it D. I still want the user
to hook to the delegate, but some of the derived features need access to it
as well. How do I do that? I tried the following (note the virtual on the
event in the base-class):

class D : B
{
public override event KeyHandler OnKey;

.... Internally, in method FF, I might do:

if (OnKey != null)
OnKey(this, whatever);
....
}

The user then connects as follows:

D od = new D();
od.OnKey += new KeyHandler(some function X);

When method FF executes, the function X is called, but when method F is
called (in the base class), it thinks OnKey is null, and X is not called.

F needs to call X as well... I’m not allowed to access the OnKey event from
a derived class into a base-class, I get an error message: B.OnKey' can only
appear on the left hand side of += or -= (except when used from within the
type 'B').

Any suggests are most welcome!
 
M

Michael C

Eric T. said:
I've got a class B that has delegates for users to hook up to.

class B
{
public delegate void KeyHandler (object sender, EventArgs e);
public virtual event KeyHandler OnKey;
...
Internally, in method F, I might do:


The general method MS use is to create a protected OnKey method

protected void OnKey(EventArgs e)
{
if (OnKey != null)
OnKey(this, whatever);
}

Michael
 
P

Pavel Minaev

The general method MS use is to create a protected OnKey method

protected void OnKey(EventArgs e)
{

}

To clarify further, this method is typically virtual:

protected virtual void OnKey(EventArgs e);

The base class should _always_ use this method when it needs to raise
the event. Then, derived classes can call that method too, and
override it to intercept event raise and mess with it if needed.
 
A

Anthony Jones

Eric T. said:
I’ve got a class B that has delegates for users to hook up to.

class B
{
public delegate void KeyHandler (object sender, EventArgs e);
public virtual event KeyHandler OnKey;
...
Internally, in method F, I might do:

if (OnKey != null)
OnKey(this, whatever);
...
}

Now I want to derive from this class, and call it D. I still want the
user
to hook to the delegate, but some of the derived features need access to
it
as well. How do I do that? I tried the following (note the virtual on
the
event in the base-class):

class D : B
{
public override event KeyHandler OnKey;

... Internally, in method FF, I might do:

if (OnKey != null)
OnKey(this, whatever);
...
}

The user then connects as follows:

D od = new D();
od.OnKey += new KeyHandler(some function X);

When method FF executes, the function X is called, but when method F is
called (in the base class), it thinks OnKey is null, and X is not called.

F needs to call X as well... I’m not allowed to access the OnKey event
from
a derived class into a base-class, I get an error message: B.OnKey' can
only
appear on the left hand side of += or -= (except when used from within the
type 'B').

First off lets clear up the standard structure used for events:-

class B
{
public delegate void KeyHandler (object sender, EventArgs e);
public event KeyHandler Key;

protected virtual void OnKey(EventArgs e)
{
if (Key != null)
Key(this, e);
}
}

Note events in .NET are not usually prefixed with "On", its the protected
virtual method which invokes them that has the "On" prefix.

I'm not sure I grasp why you want to make the event itself virtual so that
"some of the derived features can access it", since the event is public
anyway. However ordinarily a derived class would hook the event by
overriding the OnXXXX virtual method that invokes the event and would invoke
the event itself by calling the same OnXXXX virtual method. Hence there
isn't any need to fiddle with the event itself.

class D : B
{
protected override void OnKey(EventArgs e)
{
//Do your stuff
base.OnKey(e);
}
}
 

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