all groups > flash actionscript > september 2006 >
You're in the

flash actionscript

group:

how to reference the class that instantiated you


how to reference the class that instantiated you SPGAnne
9/30/2006 7:21:11 PM
flash actionscript:
I have a feeling this is a circular problem and maybe can't be done.

I have a class (myOwnerClass) that instantiates a whole pile of other objects,
one of those objects (myManagerClass) needs to reference many of the objects
that are instantiated by the myOwnerClass. So right now I instantiate
myManagerClass with an argument, "this" to refer back to the myOwnerClass
object so that the myManagerClass can invoke methods on the various other
objects that myOwnerClass had instantiated. I can't seem to get the thing to
compile and I have a feeling it is because in the myManagerClass I need to
import the myOwnerClass which is importing the myManagerClass and so I have
some wild circle going. Is that right?

At any rate, the reason I was doing this in the first place is that I want to
make myOwnerClass not have so much junk in it (since myManagerClass has a fair
amount of functionality having to do with enabling and disabling various
components instantiated in the myOwnerClass). Any thoughts on the best way to
handle this issue? Thanks very much in advance for any help you gurus out
there can provide.
Re: how to reference the class that instantiated you LuigiL
10/1/2006 12:00:00 AM
Still too little information. You can't get the thing to compile: meaning, you
get errors in the output panel or the thing doesn't work as expected?
When myOwnerClass instantiates the whole shebang with something like:
var owner=new myOwnerClass();
how are you invoking methods in myManagerClass?
What do mean with 'need to import'? Remember, the import statement doesn't
physically import anything. The import statement is just convenient when you
use packages so you don't have to type the fully qualified path time and again
in your code. But, does this refer to some error you get when checking the
code? If so, that's an inconvenient feature of the compiler but at runtime you
won't get that error.
This is too abstract to give advice and it would help to see some code. To
start I've attached an example of an approach but - as always - there are more
(and better) ways. The attached approach would require your pile of objects to
be public and as you know, that's not something we want in OOP.



class myOwnerClass {

//one of the pile of objects
public var myTest:myTestClass;
//the manager class
private var myManager:myManagerClass;

public function myOwnerClass(){
trace("myOwnerClass instantiated.");
myTest=new myTestClass();
//pass the current instance of myOwnerClass to the myManagerClass
myManager=new myManagerClass(this);
}

//return the manager
public function getManager():myManagerClass{
return myManager;
}
}

class myManagerClass {

private var myOwner:myOwnerClass;

public function myManagerClass(inst:myOwnerClass){
trace("myManagerClass instantiated.");
myOwner=inst;
}

public function callTrace():Void{
myOwner.myTest.doTrace();
}
}

class myTestClass {

public function myTestClass(){
trace("myTestClass instantiated.");
}

public function doTrace():Void{
trace("doTrace in myTestClass called.");
}
}

//in the fla
var test:myOwnerClass=new myOwnerClass();
//get the instance of myManagerClass and invoke a method in myTestClass
var manager=test.getManager();
manager.callTrace();
Re: how to reference the class that instantiated you SPGAnne
10/2/2006 9:01:55 PM
Hello LuigiL-
Nice to hear from you again. I am being sidetracked on another project now
and will have to return to this in a while. I am going to study what you
provided and see where I might be going wrong and perhaps also come up with a
much better design, because as you point out I would need to make a bunch of
stuff public that I probably shouldn't be.

AddThis Social Bookmark Button