2

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!

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

1 Answers1

5

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 S
  • 23,637
  • 2
  • 63
  • 78