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;
}
}
A form of filial therapy, child-parent relationship theory (CPRT) teaches parents to use child-centered play therapy (CCPT) skills with their children. Based on attachment theory, CPRT espouses that a secure bond between parent and child is mandatory for children's healthy development.
The term “parent-child relationship” refers to the unique and significant affiliation between a parent and child. Legally, the parent-child relationship is defined as the relationship between an individual and their biological offspring or between an individual and a child he or she has legally adopted.
have certain qualities that remain constant. They are built on safety, unconditional love, mutual respect, acceptance and flexibility.
Positive relationships between parents and children are important for all areas of children's development. Positive relationships with children are based on being in the moment, spending quality time and building trust. Your relationship with your child will change and develop as your child grows and develops.
This seems to compile, though it's rather hairbrained:
class ParentClass<C, T> where C : ChildClass<C, T>
{
public void AddChild(C child)
{
child.SetParent(this);
}
}
class ChildClass<C, T> where C : ChildClass<C, T>
{
ParentClass<C, T> myParent;
public void SetParent(ParentClass<C, T> parent)
{
myParent = parent;
}
}
This solution makes use of a recursively bound type parameter, which approximates the "self-type".
I'm obligated to link to Eric Lippert's article on this pattern: Curiouser and curiouser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With