So, I'm trying to have a parent/child class relationship like this:
class ParentClass<C, T> where C : ChildClass<T>
{
public void AddChild(C child)
{
child.SetParent(this); //Argument 1: cannot convert from 'ParentClass<C,T>' to 'ParentClass<ChildClass<T>,T>'
}
}
class ChildClass<T>
{
ParentClass<ChildClass<T>, T> myParent;
public void SetParent(ParentClass<ChildClass<T>, T> parent)
{
myParent = parent;
}
}
But, this is a compile error. So, my second thought was to declare the SetParent
method with a where
. But the problem is that I don't know what type declare myParent
as (I know the type, I just son't know how to declare it.)
class ParentClass<C, T> where C : ChildClass<T>
{
public void AddChild(C child)
{
child.SetParent(this);
}
}
class ChildClass<T>
{
var myParent; //What should I declare this as?
public void SetParent<K>(ParentClass<K,T> parent) where K : ChildClass<T>
{
myParent = parent;
}
}