How to Access Grandparent properties from child

How to Access Grandparent properties from child

Old forum URL: forums.lhotka.net/forums/t/7244.aspx


Todd_B posted on Wednesday, July 08, 2009


I have a child Editable Child.  I need to add a Validation Rule in the child object that compares a property in the child with a property in the GrandParent object.  I have seen an example in the forms on how to get the parent reference for one level up in a custom rule.

Is there a way to get the parent's parent GrandParent reference from a child object? 

tetranz replied on Wednesday, July 08, 2009

BusinessBase and BusinessListBase both have a reference to their immediate parent so you can just work your way up the chain.

I usually but something like this in my lists derived from BusinessListBase. Assuming my root is called Root

public Root MyParent
{
  get { return (Root) Parent; }
}

And then in my child class, something like this ...  assuming my list is MyList

public MyList MyParent
{
  get { return (MyList) Parent; }
}

Then in the child, to get the root do MyParent.MyParent

That can be repeated on further levels so in a grandchild you might do MyParent.MyParent.MyParent.MyParent to get the root. There are twice as many MyParents as you might expect because for the child, Parent refers to the list not the parent above it. Maybe you don't really need access to the lists so in the child do something like:

public Root MyParent
{
  get { return ((MyList) Parent).MyParent; }
}

That works for me but it's probably not the purest OO thing to do. You'd need to use an interface or something if your collections were not always under the same parent type.

Vinodonly replied on Thursday, July 09, 2009

In one of the bo's, I used LocalContext to store ref of Parent BO and accessed it from grandchild..

tetranz replied on Thursday, July 09, 2009

Vinodonly:
In one of the bo's, I used LocalContext to store ref of Parent BO and accessed it from grandchild..


Don't forget that you'll need to reset the reference after serializing / deserializing. The Parent property in BB and BLB "just works" because Rocky has taken care of all that tedious stuff.

Copyright (c) Marimer LLC