473,772 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with object design

I am having trouble with the following design. I want an object containing a Parent object and a
List of its Child objects. The Child object is abstract, so the List must contain concrete objects
derived from it.

When I instantiate the ParentChild object and add Child-derived objects to the List, I will be able
to add any object derived from Child. However, I want the List to contain only a single type
derived from Child, and I want methods using children to behave polymorphically .

I don't think this snippet will work. Can anyone suggest an alternative?

Thanks, Flomo
namespace Test {

class ParentChild {
private Parent parent;
private Child<Tchildren ;
}

class Parent {}

abstract class Child {}

class ChildTypeA: Child {}

class ChildTypeB: Child{}
}
--

Jul 27 '07 #1
4 1325
Flomo Togba Kwele wrote:
I am having trouble with the following design. I want an object containing a Parent object and a
List of its Child objects. The Child object is abstract, so the List must contain concrete objects
derived from it.

When I instantiate the ParentChild object and add Child-derived objects to the List, I will be able
to add any object derived from Child. However, I want the List to contain only a single type
derived from Child, and I want methods using children to behave polymorphically .

I don't think this snippet will work. Can anyone suggest an alternative?

Thanks, Flomo
namespace Test {

class ParentChild {
private Parent parent;
private Child<Tchildren ;
}

class Parent {}

abstract class Child {}

class ChildTypeA: Child {}

class ChildTypeB: Child{}
}
I'm not positive I follow your question, but if you want the list to
contain only a single type derived from Child then wouldn't you just
change this:

private Child<Tchildren ; // This is invalid, BTW.

to this:

private List<ChildAchil dren;

or to this:

private List<ChildBchil dren;

depending upon which type you want to be in the list?

If you really do want a list of type List<Childbut limit which Child
types can be added to it then you could do something like this:

public class Mother
{
List<Childchild ren = new List<Child>();

public Mother()
{
ChildA goodChild = new ChildA();
ChildB badChild = new ChildB();

children.Add(go odChild);
children.Add(ba dChild);
}
}

public class GoodList : List<Child>
{
new public void Add(Child item)
{
if (item is ChildB) return;
base.Add(item);
}
}

public abstract class Child { }
public class ChildA : Child { }
public class ChildB : Child { }

Or, better yet, since there really not point in limiting the members of
the list if you want those members to be polymorphic, implying the types
will be different (even though they'll have the same base type), just
filter them when you make the list:

if (goodChild is ChildA) children.Add(go odChild);
if (badChild is ChildA) children.Add(go odChild);

and you won't have to override the List.Add method.

A list can contain any type derived from the list's declared type and if
you only want specific subclasses of that declared type to be in the
list then one of the above two methods should work.

The Parent type is easy enough to deal with and seems to just add noise
to your question.

--
-glenn-
Jul 27 '07 #2
bob
Hi Flomo,
Wouldn't class parent have a list of its children?

Something like
namespace Test {
>
class ParentChild {
private Parent parent;
// private Child<Tchildren ;
private List<ChildCurre ntParentChildre n;
public ParentChild(Par ent CurrentParent){ CurrentParentCh ildren =
CurrentParent.m yKiddies;}
public List<ChildGetTh eBrats(){return CurrentParentCh ildren};
}

class Parent {public List<ChildmyKid dies;}

abstract class Child {}

class ChildTypeA: Child {}

class ChildTypeB: Child{}
}
HTH
Bob
On Fri, 27 Jul 2007 15:17:51 -0500, "Flomo Togba Kwele"
<Fl***@communit y.nospamwrote:
>I am having trouble with the following design. I want an object containing a Parent object and a
List of its Child objects. The Child object is abstract, so the List must contain concrete objects
derived from it.

When I instantiate the ParentChild object and add Child-derived objects to the List, I will be able
to add any object derived from Child. However, I want the List to contain only a single type
derived from Child, and I want methods using children to behave polymorphically .

I don't think this snippet will work. Can anyone suggest an alternative?

Thanks, Flomo
namespace Test {

class ParentChild {
private Parent parent;
private Child<Tchildren ;
}

class Parent {}

abstract class Child {}

class ChildTypeA: Child {}

class ChildTypeB: Child{}
}
Jul 28 '07 #3
Hi Flomo,
However, I want the List to contain only a single type derived from
Child,

To make a list to contain only a single type, you could use a strongly
typed collection, such as List<Tand specify the type T as the type you
want. For example,

List<ChildTypeA children;

or

List<ChildTypeB children;

In addition, you could contain the list in the Parent class for brief, such
as the following:

class Parent
{
private List<ChildTypeA children;
}
and I want methods using children to behave polymorphically .
Could you please tell me what you mean in the above sentence? What the
"mothods using children" are you're referring to?

Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 30 '07 #4
I was trying to do too much too soon, learning C# and polymorphism. I've gone back to basics (not
VB).

Thanks for all your help - Flomo
--

Aug 8 '07 #5

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

Similar topics

34
7111
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. Yensao
6
7146
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open in design view. This happens once I restarted Visual Studio adn opened teh solution Any ideas on how to fix this would be appreciated.. BR ::Ben
4
1349
by: Michael Klatt | last post by:
I need to design a container similar to a std::set, but where the stored objects may be modified via a non-const iterator. In some cases the modification will effect the sort order. Here's an example: class Object { // an object from the database }; class Sorter { // functor that determines sort order for Objects };
4
10621
by: keepyourstupidspam | last post by:
Anyone know of a reliable design for a Windows C++ Task Scheduler Class. The scheduler will expose a member function that will add schedules, its parameters will be an interval to run the tasks and a function pointer. This function pointer will be a void* function in other objects that will use the scheduler. So when another object calls the addSchedule function the scheduler will run these tasks at each interval provided. There may...
23
3284
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
7
2360
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a WinForms interface and then need to port that to a web app. I am kinda stuck on a design issue and need some suggestions / direction. Basically I have a business layer that I want to use to process any dataentry logic (row focus changes, data...
1
9356
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
53
4757
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
0
5576
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
53
8416
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script language="javascript" type="text/javascript">
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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...
1
10038
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
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
6713
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.