Access Parent Object from Child Object

Thread is closed for posting
2 posts, 1 answers
  1. Justin Woolich
    Justin Woolich avatar
    1 posts
    Member since:
    Dec 2009

    Posted 15 Dec 2009 Link to this post


    Is there any way to access the parent object from within a child object.

    So for example i have a class ParentClass with a member variable IList<ChildClass> both of which are Persitent classes.

    Is there an easy way to get a reference to the ParentClass from the ChildClass. So for example if i call a method on the child class and i want to access some of the properties on the Parent Class, what is the best way to do this?

  2. Answer
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 15 Dec 2009 Link to this post

    Hello Justin,

    If you have a close relationship between the classes, like en Order-OrderLine, you can have the OrderLine reference back to the Order by using a field, say order of type Order.

    public class Order
      private IList<OrderLine> orderLines;

      // Public properties goes here

    public class OrderLine
      private Order order;

      // Public properties goes here

    When you do a forward mapping (classes to tables) of the Order class, the wizard will ask you what the reverse field of the OrderLines list will be... here you choose the order field.... If you didn't define the order field, the only option would be {Auto}, meaning...anonymous from an OO perspective.

    Hope this helps.



Back to Top