471,350 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

class inherit question

Hi,
I have two classes (ClassA and ClassB),

public ClassA {
public ClassA() {
this.MyButton.Clicked += new
System.EventHandler(this.MyButton_Click);
}

private void MyButton_Click(object sender, System.EventArgs e) {
MyFunc();
}

protected void MyFunc() {
}
}
public ClassB : ClassA {
.... some functions
}

What I need is, once the event is fired in ClassA, how can I make
inherited classes (e.g ClassB) to know base class fires an event?

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Jan 16 '06 #1
4 1356
Joe
Add a method called OnClick to class A. In the event handler, call this
method. In class B just override the method. Make sure class B calls
base.OnClick

Hope this helps.

-Joe

"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:u2***************@TK2MSFTNGP15.phx.gbl...
Hi,
I have two classes (ClassA and ClassB),

public ClassA {
public ClassA() {
this.MyButton.Clicked += new
System.EventHandler(this.MyButton_Click);
}

private void MyButton_Click(object sender, System.EventArgs e) {
MyFunc();
}

protected void MyFunc() {
}
}
public ClassB : ClassA {
.... some functions
}

What I need is, once the event is fired in ClassA, how can I make
inherited classes (e.g ClassB) to know base class fires an event?

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy

Jan 16 '06 #2
Hardy,
Add a method called OnClick to class A. In the event handler, call this
method. In class B just override the method. Make sure class B calls
base.OnClick
What I need is, once the event is fired in ClassA, how can I make
inherited classes (e.g ClassB) to know base class fires an event?


Although Joe provided a good answer to the question, "How does one invoke
the event handler of a superclass from one of its subclasses?", I believe
your question was, "How does one invoke the event handler of all subclasses
from one of its superclasses?" This is an interesting question and has many
complications. However, the psuedo code might look like this:

1. If the interesting event handler is triggered on an instance of ClassA
continue to step 2; otherwise exit
2. Use reflection to traverse the hierarchy of ClassA to find all classes
that inherit from ClassA
3. Find all instances of all of the classes found in step 2
4. Invoke the interesting event handler for each of the instances found in
step 3.

I thought I would respond with this much information to see if I understand
your question. We can discuss ways to accomplish each of these steps in
follow-up responses, if necessary.

Regards,

Randy
Jan 16 '06 #3
> 1. If the interesting event handler is triggered on an instance of ClassA
continue to step 2; otherwise exit
2. Use reflection to traverse the hierarchy of ClassA to find all classes
that inherit from ClassA
3. Find all instances of all of the classes found in step 2
4. Invoke the interesting event handler for each of the instances found in
step 3.


4a. Check somehow if instance is registered to that particular event.
Jan 17 '06 #4
Hi Hardy,

Welcome to MSDN newsgroup!

As Randy's suggestion if you want to let the child class knows the base
class event firing, you will use reflection in the runtime. Then, you can
use the iteration to get all child classes and fire a new event in the
child class.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support

Jan 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by ubergoober | last post: by
9 posts views Thread by mead | last post: by
10 posts views Thread by Joe | last post: by
10 posts views Thread by Joseph Turian | last post: by
4 posts views Thread by kikazaru | last post: by
13 posts views Thread by learning | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.