tags:

views:

183

answers:

1

I am trying to make a function that opens a window but makes sure the same window is not already open. I want to be able to pass it a non-instantiated var or an instantiated var and it work either way. If the window is already open it closes it then reopens it.

So I need a way to pass a variable of type Window or a subclass if it, and instantiate the proper subclass.

I am looking for something like this:

public function openWindowOnce(window:Window):void
{
    if(isOpen(window))
    {
     closeIfOpen(window);
    }
    window = new Window(); /**<-- THIS LINE window can also be a sublcass of window, 
            *    I want to instatiate the correct sublass,
            *   I also want to make sure that it is a Window or a
            *    Sublcass of window before I instatiate it.
            */ 
    window.open();
}

Thanks!

+3  A: 

You can try using a combination of flash.utils.getDefinitionByName(), flash.utils.getQualifiedClassName() and ClassFactory to achieve the result.

var className:string = getQualifiedClassName(object); //returns the class name    
var classObj:Class = getDefinitionByName(className) as Class; //get a Class object
var factory:IFactory = new ClassFactory(classObj);// get a Class factory    
var newObj:Object = factory.newInstance();
Chetan Sastry