1

I've got this classes:

public class MyClassContainer<T> where T:MyClass
{
    private List<T> _classList = new List<T>();

    public MyClassContainer(params MyClass[] myClasses)
    {
        for (int i = 0 ; i < myClasses.Length; ++i)
        {
              myClasses[i].container = this; // ERROR: Cannot implicitly cast...
              _classList.Add(myClasses[i]);
        }
    }
    ...
}

public abstract MyClass: IMyclass
{
    public MyClassContainer<MyClass> container
    {
        get;
        set;
    }
    ...
}

That code has an error in this line myClasses[i].container = this;. It said:

Cannot implicitly convert type MyClassContainer<T> to MyClassContainer<MyClass>

What did I do wrong here? Thanks.

PS: I'm not used to using generics so it's probably because of that although I don't know why it went wrong.

  • `public class MyClassContainer where T:MyClass` means that T must be a referenced type, not exactly `MyClass` for more https://msdn.microsoft.com/en-us/library/d5x73970.aspx – Sergio Feb 07 '17 at 06:20
  • nevermind my answer, i deleted it, since i confused `_classList and `` `myClasses` whos implementation is missing. could you add that missing and most important part of your code? – nozzleman Feb 07 '17 at 06:28
  • First error you can correct with this `myClasses[i].container = this as MyClassContainer;` – Sergio Feb 07 '17 at 06:30
  • And about second, with List... Maybe you tell as what you trying to do, and we can say how you can do that. – Sergio Feb 07 '17 at 06:32
  • You are right. When I tried `myClasses[i].container = this as MyClassContainer;` it worked. Weird though, because when I tried `myClasses[i].container = (MyClassContainer) this;` it said cast is redundant. My background is C++ so I didn't think about using the `as` keyword. This should be put as the answer so I can mark it as solved. Thanks a lot. – Bawenang Rukmoko Pardian Putra Feb 07 '17 at 06:36
  • And about what I'm trying to do is I'm just trying to make an object pool system where you can either do `myObjectPool.Store(myObject)` or `myObject.Store()` which in turn will call `myObjectPool.Store(this);` – Bawenang Rukmoko Pardian Putra Feb 07 '17 at 06:39
  • You are wellcome. Oh the second part is tricky :) Best of luck – Sergio Feb 07 '17 at 06:42
  • 2
    Why do you have a type specific method in the generic class that only refers to the specific type in the constraint? You could maybe just replace the signature with public MyClassContainer(params T[] myClasses) ... – Janne Matikainen Feb 07 '17 at 06:45
  • Because it was using a generic class that has implemented the IMyClass interface before. So it was like this : `public class MyClassContainer where T:class, IMyClass, new()` And then I decided that the object should know the container / pool where it's paired with instead of browsing the global static pool list for its paired container / pool. So with that by itself, I must define the `container` variable. Couldn't do that with interface only. And then there's the `Store()` method that's also has to be implemented, that I mentioned earlier. – Bawenang Rukmoko Pardian Putra Feb 07 '17 at 06:52
  • If you change the type of `List` from `T` to `MyClass` and header to `public MyClassContainer(MyClass[] myClasses)`, all will work =) – Sergio Feb 07 '17 at 07:01
  • @Sergio Well, yeah. I actually know that. I've made another class that's exactly like that before. And it worked quite nicely actually. With this new code though, I wanted to do it differently. – Bawenang Rukmoko Pardian Putra Feb 07 '17 at 07:04

1 Answers1

1

Try using as instead of cast

myClasses[i].container = this as MyClassContainer<MyClass>;
Sergio
  • 346
  • 1
  • 8
  • Yeah. I was thinking the same thing. – Bawenang Rukmoko Pardian Putra Feb 07 '17 at 07:04
  • Read on MSDN or simply use search http://stackoverflow.com/questions/702234/what-is-the-difference-between-casting-and-using-as-in-c – Sergio Feb 07 '17 at 07:54
  • this is not a correct solution, since it will return null at runtime, as explained in this [comment](http://stackoverflow.com/questions/42084389/why-does-as-instead-of-a-cast-work/42084476#comment71339549_42084476) for each meaningful use of the generic T that is not simply MyClass itself –  Feb 07 '17 at 12:05