<p>My question is pertaining to the best practice for accessing a child object's parent. So let's say a class instantiates another class, that class instance is now referenced with an object. From that child object, what is the best way to reference back to the parent object? Currently I know of a couple ways that I use often, but I'm not sure if A) there is a better way to do it or B) which of them is the better practice</p>

<p>The first method is to use getDefinitionByName, which would not instantiate that class, but allow access to anything inside of it that was publicly declared.</p>

<pre><code>_class:Class = getDefinitionByName("com.site.Class") as Class;
</code></pre>

<p>And then reference that variable based on its parent to child hierarchy.<br>
Example, if the child is attempting to reference a class that's two levels up from itself:</p>

<pre><code>_class(parent.parent).function();
</code></pre>

<p>This seems to work fine, but you are required to know the level at which the child is at compared to the level of the parent you are attempting to access.</p>

<p>I can also get the following statement to trace out [object ClassName] into Flash's output.</p>

<pre><code>trace(Class);
</code></pre>

<p>I'm not 100% on the implementation of that line, I haven't persued it as a way to reference an object outside of the current object I'm in.</p>

<p>Another method I've seen used is to simply pass a reference to this into the class object you are creating and just catch it with a constructor argument</p>

<pre><code>var class:Class = new Class(this);  
</code></pre>

<p>and then in the Class file</p>

<pre><code>public function Class(objectRef:Object) {
    _parentRef = objectRef;
} 
</code></pre>

<p>That reference also requires you to step back up using the child to parent hierarchy though.</p>

<p>I could also import that class, and then use the direct filepath to reference a method inside of that class, regardless of its the parent or not.</p>

<pre><code>import com.site.Class;
com.site.Class.method();
</code></pre>

<p>Of course there the parent to child relationship is irrelevant because I'm accessing the method or property directly through the imported class.</p>

<p>I just feel like I'm missing something really obvious here. I'm basically looking for confirmation if these are the correct ways to reference the parent, and if so which is the most ideal, or am I over-looking something else?</p>

[{'answer_id': 60074, 'author': 'dagorym', 'author_id': 171, 'author_profile': 'https://Stackoverflow.com/users/171', 'pm_score': 2, 'selected': False, 'text': "<p>I've always used your second method, passing a pointer to the parent object to the child and storing that pointer in a member variable in the child class.  To me that seems to be the simplest method for the child to communicate back to the parent.</p>\n"}, {'answer_id': 60411, 'author': 'Antti', 'author_id': 6037, 'author_profile': 'https://Stackoverflow.com/users/6037', 'pm_score': 3, 'selected': True, 'text': "<p>It's generally good to have the class as it's own instance and reduce tight coupling to something else (as in this case, it's parent). If you do something like parent.doSomething() it's not possible to use that class in container that doesn't have the doSometing() method. I think it's definitely better to pass in whatever the class may need and then inside the class it doesn't have to do any parent.parent etc anymore.</p>\n\n<p>With this if you in the future want to change the structure, it's very easy to just pass in a new reference; the implementation of the child class doesn't have to change at all.</p>\n\n<p>The third alternative you have here is also very different, it's accessing a class level static method (you don't have to type the whole class path when accessing that method), not an instance method as in the first two.</p>\n"}, {'answer_id': 60427, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 1, 'selected': False, 'text': '<p>I like to pass the parent as an interface (so the class can be contained in any parent implementing that interface) or implement the reference as an event/function pointer/delegate which the parent can hook onto.</p>\n'}, {'answer_id': 64085, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>I like setting up a global class to handle references to classes that need to be accessed by any other class, not necessarily the child.</p>\n\n<p>The global class simply consists of static getters and setters like so:</p>\n\n<pre><code>private static const class:Class;\n\npublic static function setClass(_class:Class){\nclass = _class;\n}\npublic static function getClass(void):Class{\nreturn class;\n}\n</code></pre>\n\n<p>The nice thing about this is that you don't have to import the class you are returning from the global class, just the global class itself.</p>\n\n<p>The class that needs to be referenced adds itself to the global list.\nThe Other cool thing is that you can easily dispatch events from this centralized location.</p>\n\n<p>Most of the time if a class needs to be referenced by child classes or any other class, i do it through an interface.</p>\n"}, {'answer_id': 67526, 'author': 'mikechambers', 'author_id': 10232, 'author_profile': 'https://Stackoverflow.com/users/10232', 'pm_score': 3, 'selected': False, 'text': '<p>In general, if you need a child to communicate with a parent, you should look at having it do so by broadcasting events. This decouples the child for the parent, and makes it possible to have other classes work with the child.</p>\n\n<p>I would not recommend passing in a reference to the parent class into the child.</p>\n\n<p>Here is a a simple example (I have tested / compiled this so there may be some typos).</p>\n\n<pre><code>//Child.as\npackage\n{\n    import flash.events.EventDispatcher;\n    import flash.events.Event;\n\n    public class Child extends EventDispatcher\n    {\n        public function doSomething():void\n        {\n            var e:Event = new Event(Event.COMPLETE);\n            dispatchEvent(e);\n        }\n\n        public function foo():void\n        {\n            trace("foo");\n        }\n    }\n}\n\n\n//Parent.as\npackage\n{\n    import flash.display.Sprite;\n    import flash.events.Event;\n    public class Parent extends Sprite\n    {\n        private var child:Child;\n        public function Parent():void\n        {\n            c = new Child();\n            c.addEventListener(Event.COMPLETE, onComplete);\n            c.foo();//traces foo\n\n            c.doSomething()\n        }\n\n        public function onComplete(e:Event):void\n        {\n            trace("Child broadcast Event.COMPLETE");\n        }\n\n    }\n}\n</code></pre>\n\n<p>In most cases, you would dispatch custom events and pass data with them.</p>\n\n<p>Basically:</p>\n\n<p>Parent has reference to Child and communicates via method calls.\nChild does not have reference to Parent, and communicates (to anyone) via dispatching events.</p>\n\n<p>hope that helps...</p>\n\n<p>mike chambers</p>\n\n<p>mesh@adobe.com</p>\n'}, {'answer_id': 1077501, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If these objects are in the DisplayList, then you have some more options. If I have a ParentClass and a ChildClass, in the child class, you seem to be able to access the parent if you cast the request as the ParentClass. e.g. </p>\n\n<pre><code>ParentClass(parent).parentFunction();\n</code></pre>\n\n<p>I know for sure it works if the ParentClass is the Document Class. As the Document Class is always the first item in the display list, this works:</p>\n\n<pre><code>ParentClass(stage.getChildAt(0)).parentFunction();\n</code></pre>\n\n<p>In my case, they were both members of the same package, so I did not even need to import anything. I haven't tested it in all circumstances, but it worked when I needed it to.</p>\n\n<p>Of course 'parent' and 'getChild...' only work if these objects are in the DisplayList, but that's been good enough for me.</p>\n"}]
['https://Stackoverflow.com/questions/60051', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1945/']