473,567 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class inherit question

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

public ClassA {
public ClassA() {
this.MyButton.C licked += new
System.EventHan dler(this.MyBut ton_Click);
}

private void MyButton_Click( object sender, System.EventArg s 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 1411
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*******@news groups.nospam> wrote in message
news:u2******** *******@TK2MSFT NGP15.phx.gbl.. .
Hi,
I have two classes (ClassA and ClassB),

public ClassA {
public ClassA() {
this.MyButton.C licked += new
System.EventHan dler(this.MyBut ton_Click);
}

private void MyButton_Click( object sender, System.EventArg s 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
1285
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. Is there any way to hide, or not inherit, properties of the parent class? Btw, I'm coding in C#, in case that makes a difference. My intuition...
9
5870
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
4977
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 pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived...
10
667
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 circumstacnes does one implement an interface? TIA, -- Joe VBA Automation/VB/C++/Web and DB development
13
1548
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. In the first I new an integer and assign it to i, in the second one I don't bother. In both cases, an integer is created and I can use it. If I...
10
1690
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
1843
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 implementations of B's methods provided by B1? (And also make C2 inheriting A and B2 (another child of B) and C3 etc.). Is it possible to do this entirely...
26
5343
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... ...
13
20751
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 & I don't want to copy + paste code. How can I inherit those member variables? Thanks
0
7690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7654
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6273
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5216
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.