Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent/Child Generics Relationship

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;
    }
}
like image 602
Jay Avatar asked May 31 '13 00:05

Jay


People also ask

What is parent/child relationship theory?

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.

What is the relationship between a parent and a child called?

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.

What are the characteristics of a parent-child relationship?

have certain qualities that remain constant. They are built on safety, unconditional love, mutual respect, acceptance and flexibility.

Why is parental relationship important?

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.


1 Answers

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

like image 133
Paul Bellora Avatar answered Oct 01 '22 20:10

Paul Bellora