2 Replies Latest reply on Dec 8, 2007 4:29 PM by m_hartnett

    How to get a reference to the owner of a class instance?

    BIOSMonkey
      Within a method of a class, how can I get a reference to the object containing the class instance?

      To be clear: I have class B that contains a method, say "myfunc()". Class A (say, the application itself or a custom component) instantiates a new instance of Class B : myclassB=new ClassB()

      Now, from within myfunc() can I get a reference to Class A?

      The simplest way here is to pass a "this" reference when calling myfunc(), i.e. "myclassB.myfunc(this)" but I would prefer not to have to remember to always use 'this'.
        • 1. Re: How to get a reference to the owner of a class instance?
          Gregory Lafrance Level 6
          Can you try to access the "parent" property of the instantiated object, as in: myclassB.parent ? Or possible the parentApplication object.
          • 2. Re: How to get a reference to the owner of a class instance?
            m_hartnett Level 3
            Are these objects within each other. Does classA own classB? If that is the case, then Greg is correct and it should be available in parentDocument.

            In projects in the past we have created a central refObjectLocator object that is available to all objects.

            Mostly we use events to communicate between objects. Dispatch an event and let whoever listen for it.

            Here is a copy of our reflocator if you are interested.

            package com.goconfigure.model {

            import mx.collections.ArrayCollection;
            import com.adobe.cairngorm.model.ModelLocator;
            import com.goconfigure.util.HashMap;

            [Bindable]
            public class RefObjectLocator implements ModelLocator {

            // this instance stores a static reference to itself
            private static var refObject : RefObjectLocator;

            public var refObjectHM : HashMap = new HashMap();

            // singleton: constructor only allows one model locator
            public function AppLocator() : void {
            if ( RefObjectLocator.refObject != null )
            throw new Error( "Only one RefObjectLocator instance should be instantiated" );
            }

            // singleton: always returns the one existing static instance to itself
            public static function getInstance() : RefObjectLocator {
            if ( refObject == null )
            refObject = new RefObjectLocator();
            return refObject;
            }

            public function addRefObject( pRefObject : Object, pName : String ) : void {
            refObjectHM.put(pName,pRefObject);
            }

            public function getRefObject( pName : String ) : Object {
            return refObjectHM.getValue(pName);
            }

            public function removeRefObject( pName : String ) : void {
            refObjectHM.remove(pName);
            }

            public function clearRefObject() : void {
            refObjectHM.clear();
            }

            }
            }