473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 1399
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ubergoober | last post by:
Hi, I have Class A, and I would like my Class B to inherit from Class A because they are conceptually common classes. My problem is that Class A has a property that I don't want Class B to have. ...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
13
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
10
by: Joseph Turian | last post by:
Is it possible to allow a class to be converted to bool? For example, if I have: class foo { private: unsigned i; }; and I want to evaluate: if (foo)
4
by: kikazaru | last post by:
Can I write methods in one class A, that use methods in another base class B, in such a way that I can make a class C1 that inherits A and B1 (a child of B) so that the methods in A use the...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.