1

I am trying to achieve the following, however I am not sure if it is even possible.

I have the following 'base' entities

public abstract class BaseObject {}
public abstract class BaseCollection <T> : ObservableCollection<T> where T : BaseObject {}

I also have

public class Obj_1: BaseObject {}
public class Obj_1_collection : BaseCollection <Obj_1>
{
    public Obj_1_Method_1 () {}
}

and

public class Obj_2: Obj_1 {}

Following the same logic as Obj_1, I want to have a Obj_2_collection. I want this collection to inherit from Obj_1_collection in order to access its methods e.g. Obj_2_collection.Obj_1_Method_1(). As I have it in my mind I am trying to do something like the following (in pseudocode):

public class Obj_2_collection : Obj_1_collection<Obj_2 extends Obj_1>

meaning that the Obj_2_collection can access Obj_1_collection, however the collection object it will be of type Obj_2

Athafoud
  • 2,898
  • 3
  • 40
  • 58
  • Did you try to define your `Obj_1_collection` as `public class Obj_1_collection : BaseCollection where T : Obj_1 {}` – Irwene Aug 01 '16 at 13:38
  • Obj_1 and Obj_2 need to inherit the same base class BaseObject. In your code you have Obj_2 inheriting Obj_1. – jdweng Aug 01 '16 at 13:40
  • 2
    @jdweng which means `Obj_2` also inherit from `BaseObject` – Irwene Aug 01 '16 at 13:41
  • @jdweng This is because I need Obj_2 inheriting Obj_1, that is a part of my logic. e.g base = vehicle, Obj_1 = car, Obj_2 = luxury car – Athafoud Aug 01 '16 at 13:43

1 Answers1

1

You're nearly there, your only problem is that you didn't define your Obj_1_collection as a generic.

To do so, you just have to use the syntax you used for the BaseCollection<T> and restrict the type T to Obj_1

That would give :

public class Obj_1_collection<T> : BaseCollection <T> where T : Obj_1 {}

Concerning the use of generics and the inheritance, you might want to read about the in and out modifiers keyword to enable covariance and contravariance.

As a side note, here are the C# naming conventions

Irwene
  • 2,807
  • 23
  • 48
  • After some quick testing your suggestion seem to work. I need to test it in a 'real world' example before accepting, but I am positive you will get the 'Check Mark'. For now +1 for the linked resources. And a disclaimer, I am familiar with the naming conventions, the naming and the code is just for example purposes. – Athafoud Aug 01 '16 at 13:56
  • @Athafoud That's all good then :) Better to have them linked when they are not useful than not link them while they are needed :p – Irwene Aug 01 '16 at 14:03
  • The only issue I can see is that every time I am using `Obj_1_collection`, I need to instantiate it as `new Obj_1_collection()`. This means a lot of refactoring, which is something I am trying to avoid. – Athafoud Aug 01 '16 at 14:34
  • @Athafoud It seems it is not possible to make C# infer the parameter type from the constructor, refer to this question : http://stackoverflow.com/questions/3570167/why-cant-the-c-sharp-constructor-infer-type for more informations. Sorry it seems you won't be able to do without refactoring one way or the other. – Irwene Aug 01 '16 at 15:26
  • While this is not the best approach, I am considering to duplicate the needed method(s) in both collections. At least for now coping and maintaining duplicate code, is 'less work' than the refactoring. – Athafoud Aug 02 '16 at 11:05