469,299 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

passing an event as a parameter

Let's say I have a base class BaseClass that defines an event
'ValueChanged' and then I have inherited classes that have specific
'SomeValueChanged', 'SomeOtherValueChanged' events.
I want to have 'ValueChanged' fired every time one of the value change
event fires in the subclasses. My first thought was to have a method in
the base class that would take a reference to an event as a parameter
and then connect that event with a handler that would then fire the
ValueChanged event. Instead, now I connect the event in the subclasses
which felt like a less "cleaner" way to accomplish this. I wanted the
base class to actually connect the event to its handler. Is there any
way to achieve this?
Nov 16 '05 #1
4 6863
Hi Vadim,

For what you are doing, it sounds like a delegate would be the most
appropriate choice. Events are essentially delegates that are type members
with the additional protection of preventing invocation from outside the
type and assignment. Delegates can be passed as parameters, may be
multicast (have multiple handlers), and can be invoked from anywhere as
specified by their access modifier.

Joe
--
http://www.csharp-station.com

"Vadim Berezniker" <va***@berezniker.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Let's say I have a base class BaseClass that defines an event
'ValueChanged' and then I have inherited classes that have specific
'SomeValueChanged', 'SomeOtherValueChanged' events.
I want to have 'ValueChanged' fired every time one of the value change
event fires in the subclasses. My first thought was to have a method in
the base class that would take a reference to an event as a parameter
and then connect that event with a handler that would then fire the
ValueChanged event. Instead, now I connect the event in the subclasses
which felt like a less "cleaner" way to accomplish this. I wanted the
base class to actually connect the event to its handler. Is there any
way to achieve this?

Nov 16 '05 #2
public class BaseClass
{
public event ValueChanged;

protected void OnValueChanged(BaseClass sender)
{
if (ValueChanged != null)
{
ValueChanged(sender, System.EventArgs.Empty);
}
}
}

public class DerivedClass : BaseClass
{
public event SomeValueChanged;

protected void OnSomeValueChanged()
{
if (SomeValueChanged != null)
{
SomeValueChanged(this, System.EventArgs.Empty);
}
base.OnValueChanged(this);
}
}

Is this the effect you wanted? The ValueChanged event from the base
class now fires every time the SomeValueChanged event fires in the
child class.

Nov 16 '05 #3
By the way... in my sample code OnValueChanged() does not need to take
an argument. You can just always pass this to ValueChanged:

ValueChanged(this, System.EventArgs.Empty);

When called from the child class, "this" will be the child class
object. D'oh! It's early. I'm not awake yet. :)

Nov 16 '05 #4
Joe Mayo wrote:
Hi Vadim,

For what you are doing, it sounds like a delegate would be the most
appropriate choice. Events are essentially delegates that are type members
with the additional protection of preventing invocation from outside the
type and assignment. Delegates can be passed as parameters, may be
multicast (have multiple handlers), and can be invoked from anywhere as
specified by their access modifier.

Joe


Thanks. I think that's the proper solution.
Don't know why I didn't think of that =)
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Pavils Jurjans | last post: by
7 posts views Thread by Ken Allen | last post: by
reply views Thread by Phillip Vong | last post: by
2 posts views Thread by diffuser78 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.