Re: List<> classes and inheritance (C#)



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@xxxxxxxxxxxx> wrote in message
news:mHednXoylYF0aRzZSa8jmw@xxxxxxxxxxxxxx
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



.



Relevant Pages

  • Re: Focuse all derived classes to implement a interface specified by the base class
    ... classes derived from it implement a interface. ... public class MyDerivedClass: MyBaseClass ... public override void Foo ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Architecture question
    ... > Create a Base class and declare evrything virtual so you can override it ... > protected virtual string CommonMethod() ... > public class ClassA:BaseClass ... >>> class ClassA: IMyInterface ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Casting Generic Classes - Possible Solution
    ... It leaves me with the open option to use generics or not when I ... This works as saying "accept a store of anything that are Record ... public class Record { ... So I tried adding an interface into the structure: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to hide an interface in C#
    ... public class A, IMyInterface ... void IMyInterface.MafonctionTresPrivee ... reference to the interface). ... > internal Interface ImyInterface ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Circular Referencing in C#
    ... I used this problem as an interview question about a month ... I configure it using config files. ... put the interface into the base code libraries. ... > public class A1C1 ...
    (microsoft.public.dotnet.languages.csharp)

Quantcast