473,399 Members | 2,159 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,399 software developers and data experts.

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 6495
On Tue, 03 Apr 2007 14:46:02 -0700, randy1200
<ra*******@discussions.microsoft.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<Parent_grparentChildren;

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

foreach (Parent parentChild in _grparentChildren)
{
strReturn = strReturn + parentChild.Talker();
}

return strReturn;
}

class Child : Parent
{
public Child()
{
_grparentChildren.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
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...
4
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...
4
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...
5
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...
1
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...
25
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: ...
10
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...
5
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.