Help with object design

F

Flomo Togba Kwele

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<T> children;
}

class Parent {}

abstract class Child {}

class ChildTypeA: Child {}

class ChildTypeB: Child{}


}
--
 
G

G.Doten

Flomo said:
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<T> children;
}

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<T> children; // This is invalid, BTW.

to this:

private List<ChildA> children;

or to this:

private List<ChildB> children;

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

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

public class Mother
{
List<Child> children = 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.
 
B

bob

Hi Flomo,
Wouldn't class parent have a list of its children?

Something like
namespace Test {
class ParentChild {
private Parent parent;
// private Child<T> children;
private List<Child> CurrentParentChildren;
public ParentChild(Parent CurrentParent){CurrentParentChildren =
CurrentParent.myKiddies;}
 
L

Linda Liu [MSFT]

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<T> and 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
{
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

Flomo Togba Kwele

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
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top