Home > Archive > C# > June 2006 > List<> classes and inheritance (C#)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
List<> classes and inheritance (C#)
|
|
|
| Hello,
this is a tricky one to explain in a post but here goes.
I need to create a collection of items in a class that will be a property of
another class. These three classes together form my three base classes as
follows:
public class MainClassBase
{
public ItemCollectionBase Items; // shortened from get/set etc. for
this post
}
public class ItemCollectionBase : List<Item> {}
public class ItemBase
{
// properties, methods, etc.
}
Once I have these, I then need to create further classes that inherit from
the three class structure, each with specific functionality.
The problem I have is that I want to override the Add() method of the
collection (List<> ) classes so that only the correct Item object types can
be added. So if I have a example of MainClassCar, CarItemCollection and
CarItem, it should only be possible to add CarItem objects to the collection
and not ItemBase objects.
Can I perform some override of Add that deals with this to only give the
option to add CarItem objects or do I need to override the Add(ItemBase) to
throw an exception?
Thanks
rdc
| |
|
| I think the answer to your question is the use of an interface that all
three classes would implement.
for example:
public interface IMyInterface
{
//some definition of common properties, methods and events
}
public class MyClass1 : IMyInterface
{
//implmentation of the interface
}
public class MyClass3 : IMyInterface
{
//implmentation of the interface
}
public class MyClass2 : IMyInterface
{
//implmentation of the interface
}
In your List property define it as follows:
public class CollectionClass : List<IMyInterface>
{
}
There may be no need at all to override the Add method at this point because
the common interface is used. All three classes in the example above could
be added to the CollectionClass regardless of their other properties,
methods because all three implement the interface. Implementing a common
Interface would be easier than overriding the Add method of a strongly typed
collection (which List<T> is) in my opinion.
Thanks,
Matt
"RDC" <nospam@forme.please> wrote in message
news:mHednXoylYF0aRzZSa8jmw@karoo.co.uk...
> Hello,
>
> this is a tricky one to explain in a post but here goes.
>
> I need to create a collection of items in a class that will be a property
> of another class. These three classes together form my three base classes
> as follows:
>
> public class MainClassBase
> {
> public ItemCollectionBase Items; // shortened from get/set etc. for
> this post
> }
>
> public class ItemCollectionBase : List<Item> {}
>
> public class ItemBase
> {
> // properties, methods, etc.
> }
>
>
> Once I have these, I then need to create further classes that inherit from
> the three class structure, each with specific functionality.
>
> The problem I have is that I want to override the Add() method of the
> collection (List<> ) classes so that only the correct Item object types
> can be added. So if I have a example of MainClassCar, CarItemCollection
> and CarItem, it should only be possible to add CarItem objects to the
> collection and not ItemBase objects.
>
> Can I perform some override of Add that deals with this to only give the
> option to add CarItem objects or do I need to override the Add(ItemBase)
> to throw an exception?
>
> Thanks
>
> rdc
>
|
|
|
|
|