1

I need to create a generic collection of generic collections, that should contain a generic class. I've tried hard, but haven't found any answers.This is how my realizated collection looks like: https://github.com/Infatum/PMC-Data-Model/blob/master/WTF/Position.cs This is the collection of position of a generic class Point. I need to create an indexed collection of Positions called Matrix, indexed collection of Matrix called Container and an indexed collection of Containers, named Containers. Please, help me!

public class Matrix<T> : ICollection<T> where T : Position<T>
{
    protected ArrayList matrix;
    protected bool isReadOnly;

    public Matrix()
    {
        matrix = new ArrayList();
    }
// ...
}

The trouble : The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Position'. There is no implicit reference conversion from 'T' to 'WTF.Point'

This is actually the task, that was given to me: https://docs.google.com/document/d/1ZYXXAjrh0OYNLUuFy0HT6ZuAzISIecE73cgA4QoRDlc/edit#heading=h.nwbuqfwpq3gk

  • sorry, my class is too long, to post it here so I've shared a link on my project with all necessary classes – Epam Student Aug 30 '15 at 19:44
  • 1
    Perhaps you can shrink the code down to fit within the question? Just include the parts that are giving you trouble. – Sam Axe Aug 30 '15 at 19:47
  • public class Matrix : ICollection where T : Position this is where th trouble is : The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Position'. There is no implicit reference conversion from 'T' to 'WTF.Point'. – Epam Student Aug 30 '15 at 19:51
  • Please post the code as an edit to the question. Use the toolbar buttons of the editor to make sure its properly formatted. – Sam Axe Aug 30 '15 at 19:53
  • The problem is in `where T : Position` part. This means any type given as T should be a subclass of `Position` for instance if you give it Foo, Foo should be class `Foo : Position` which is impossible – Alireza Aug 30 '15 at 20:02
  • I don't know, how to realizate a collection of Position. The task is : The PMC data model is comprised of the following elements: Point - the simplest element is a data point. This is a generic type describing the simplest shape in 1D, 2D or 3D. It may be of any supported numerical C# type (e.g. integer, double, decimal). This should be implemented as immutable. Position – an indexed collection of points. Matrix – an indexed collection of positions. Container – an indexed collection of matrices. Containers – an indexed collection of Containers. – Epam Student Aug 30 '15 at 20:12

1 Answers1

2

Your type constraint on T is recursive - which I don't think you want it to be. (And neither does the compiler: The constraint enforces T to be some sort of Position<T>. this means that Position<T> is actually Position<Position<T>> - which violates your type constraint on the generic argument of Position, it expects WTF.Point instead of Position)

Solution:

public class Matrix<T> : ICollection<Position<T>>

But let me elaborate this a little more: In a sense, you want something like

public class Matrix<T> : ICollection<T> where T : Position<U>

so there is no more recursion. You want T to be "some kind of Position". But now we would have a new problem: where does U (let's call it the "element-type") come from? You should use that as your generic type argument of Matrix:

public class Matrix<U> : ICollection<T> where T : Position<U>

Now, the constraint can be collapsed directly into the interface type, i.e. ICollection<Position<U>> giving exactly the solution I gave above (modulo names).

olydis
  • 3,192
  • 13
  • 28
  • Now I have a new error: The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) – Epam Student Aug 30 '15 at 20:20
  • and this also :'Matrix' does not define type parameter 'T' – Epam Student Aug 30 '15 at 20:21
  • That sounds like you did not copy my solution but rather one of the explanatory steps ;-) There is no `Matrix` in my solution – olydis Aug 30 '15 at 20:23
  • I've tried this decission earlier(I've even tried the same with List), but it didn't help. It seems, like the task is incorrect – Epam Student Aug 30 '15 at 20:33
  • not sure what you mean ;) the task you described in your question sounds quite straight forward – olydis Aug 30 '15 at 20:34