class inherit question

H

Hardy Wang

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?
 
J

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
 
R

Randy A. Ynchausti

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

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
 
M

MyName

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.
 
Y

Yuan Ren[MSFT]

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
 

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