473,397 Members | 1,974 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,397 software developers and data experts.

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 1303
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<ChildAchildren;

or to this:

private List<ChildBchildren;

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<Childchildren = new List<Child>();

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

children.Add(goodChild);
children.Add(badChild);
}
}

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(goodChild);
if (badChild is ChildA) children.Add(goodChild);

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<ChildCurrentParentChildren;
public ParentChild(Parent CurrentParent){CurrentParentChildren =
CurrentParent.myKiddies;}
public List<ChildGetTheBrats(){return CurrentParentChildren};
}

class Parent {public List<ChildmyKiddies;}

abstract class Child {}

class ChildTypeA: Child {}

class ChildTypeB: Child{}
}
HTH
Bob
On Fri, 27 Jul 2007 15:17:51 -0500, "Flomo Togba Kwele"
<Fl***@community.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<ChildTypeAchildren;

or

List<ChildTypeBchildren;

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

class Parent
{
private List<ChildTypeAchildren;
}
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
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. ...
6
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...
4
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...
4
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...
23
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...
7
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...
1
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...
53
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,...
0
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...
53
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
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,...

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.