views:

68

answers:

3

Hi,

I have an class named Foo. This class contains a collection of child objects of type FooChildBase, but I also have a further class of type FooChildTwo which inherits from FooChildBase.

Class Foo
  Public Children As IList(Of FooChildBase)
End Class

Class FooChildBase
  Public Info As String
End Class

Class FooChildTwo
  Inherits FooChildBase

  Public ExtraInfo As String
End Class

This all works fine. Now I need to use a specialisation of my Foo object with extra information.

Class FooSpecial
  Inherits Foo

  Public SpecialInfo As String
End Class

Class FooChildSpecial
  Inherits FooChildBase

  Public SpecialChildInfo As String
End Class

What I would like to do is have my FooSpecial Class treat it's Children collection as if they were FooChildSpecial objects, but still be able to add FooChildTwo objects to it. Is this possible and if so how can it be done?

EDIT I think my original question was incorrect. I need to FooChildSpecial class to wrap any of the objects in the Children collection with the extra values, whether they are FooChildBase or FooChildTwo or XXX.

Hope this makes sense! Please let me know if more clarification is needed.

James

+1  A: 
JustLoren
Thanks for the reply. However, I don't think I explained it correctly. See my edit above
James
Thanks again for replying. Is there a way around this using composition rather than inheritance. Perhaps something like a decorator?
James
If you implemented a wrapper (decorator) pattern, FooChildSpecial could wrap an IFooChild object, but that requires you to create the IFooChild object first then pass it in to the FooChildSpecial object. Additionally, you would not be able to directly cast your FooChildSpecial object to a FooChildTwo - instead, you would have to access the underlying wrapped object and cast that.See: http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_wrapper302122006080905AM/csharp_wrapper3.aspx?ArticleID=2dc1959d-83a0-4d88-ac5b-70dddc54380c
JustLoren
A: 

What do you mean by "treat as if they were FooChildSpecial" objects? Do you mean access methods that exist only in FooChildSpecial objects?

In this case you will need to cast them to FooChildSpecial.

However, it is better to just let FooSpecial treat them as FooChildBase objects, but have in FooChildSpecial and FooChildTwo that override the base methods to get different behaviors for the two subclasses.

I also have a feeling that you really don't need the FooSpecial class at all, but I could be wrong. I suspect that the extra information and special information could just be combined into "information" and if they need different types of information to initialize the class wtih the different types.

Larry Watanabe
A: 

You could purhaps do it with generics that you have a base class which contains a list of Type T. When you define Foo and FooSpecial you spesify what T is.

Shiraz Bhaiji
If he implemented it with public IList<T> Children, then had FooSpecial : Foo<FooChildSpecial>, he couldn't add objects of type FooChildTwo to the IList in FooSpecial.
JustLoren