473,769 Members | 4,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base Method Calling Child Methods

The code below is pretty simple. Calling Talker() in the parent returns
"Parent", and calling Talker() in the child returns "Child".

I'm wondering how I can modify the code so that a call to the Talker() in
Parent will call the Talker() method in every child class. The kicker is that
I have many different Child classes, and not all Child classes will be loaded
when Talker() in the Parent is called.

Thanks,
Randy
class Parent
{
public virtual string Talker()
{
return "Parent";
}
}

class Child : Parent
{
public override string Talker()
{
return "Child";
}
}
Apr 3 '07 #1
1 6534
On Tue, 03 Apr 2007 14:46:02 -0700, randy1200
<ra*******@disc ussions.microso ft.comwrote:
The code below is pretty simple. Calling Talker() in the parent returns
"Parent", and calling Talker() in the child returns "Child".

I'm wondering how I can modify the code so that a call to the Talker()in
Parent will call the Talker() method in every child class. The kicker is
that I have many different Child classes, and not all Child classes will
be loaded when Talker() in the Parent is called.
There is no language construct that you can use to do this implicitly.
Also, your question isn't very clear. In your example, you return a
value. What does it mean for a single caller to the Talker() method to be
returned multiple values? Do you mean to concatenate the results? Also,
do you want every instance of every child class to be called? Or do you
simply want a single static method that is only called when at least one
child instance exists? Do you also want the Parent class to be included
or not? Or something entirely different from any of those options?

As an example solution, let's assume that you want every child instance to
be called, you _don't_ want the Parent included, and that you want to
concatenate the return values:

You could certainly write your code so that the Parent class has a static
member that is a collection of references to each instance of every Child,
and then in the Talker() method you would iterate over that collection.
But that would require each of the classes derived from Parent to
cooperate and add the constructed instance to the Parent's collection
whenever you create one.

For example (ignoring the inefficiency of directly concatenating the
strings rather than using the StringBuilder class):

class Parent
{
static protected Collection<Pare nt_grparentChil dren;

public virtual string Talker()
{
string strReturn = "";

foreach (Parent parentChild in _grparentChildr en)
{
strReturn = strReturn + parentChild.Tal ker();
}

return strReturn;
}

class Child : Parent
{
public Child()
{
_grparentChildr en.Add(this);
}

public override string Talker()
{
return "Child";
}
}
}
Apr 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1850
by: Matthias Kaeppler | last post by:
Sorry if this has been discussed before (I'm almost certain it has), but I didn't know what to google for. My problem is, I have a class, a gtkmm widget, and I want it to serve as a base class now, but I'm not sure if I'm taking the proper steps in order to not break the whole class. Are there any guidelines what I have to watch out for? One question would e.g. be:
4
9783
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base class are the private components internal to the base class itself. What I want are ALL components for the entire class no matter how many levels of sub-classing this particular control contains. I do not want to have to force the child classes to...
4
12876
by: Néstor Marcel Sánchez Ahumada | last post by:
In a method declaration the 'sealed' keyword must be used with the 'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why? This would be a good enhancement for C# 3.0? Any comments? Néstor
5
20041
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding it somewhat confusing when I look at the code later, because I expect to find a method called MyMethod() in the derived class. I think C# would let me call a base class method from a derived class like
1
8357
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text books say “neither the source nor the target types of a conversion can be a base type of the other, since a conversion would then already exist”. But this is not really true, whilst automatic (implicit) conversions do occur from the derived...
25
3810
by: lucas_denoir | last post by:
I'm having some serious trouble accessing a virtual method of a base class - that is not the immidate base class. This is the basic situation that I have: =========================================================== class A { public virtual string PrintMe() {
10
1547
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the base class method is not to be abstract, and will be called if the instance was created as a 'generic' base class instance. It's sort of like I want the method to be abstract to children, but concrete at the base level. That way I can refer to...
5
2625
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
19
2236
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
0
9579
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7393
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2810
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.