2

Isn't there some way to re-write the following code, such that I don't need a gigantic switch statement with every conceivable type? Also, if I can replace the switch statement with some way to dynamically create new controls, then I can make the code smaller, more direct, and don't have to anticipate the possibility of custom control types.

Before:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
  layout="vertical">
  <mx:Script>
    <![CDATA[
      import mx.containers.HBox;
      import mx.controls.Button;
      import mx.controls.Label;
      public function CreateControl(event:Event):void {
        var Type:String=Edit.text;
        var NewControl:Object;

        switch (Type) {
          case 'mx.controls::Label':NewControl=new Label();break;
          case 'mx.controls::Button':NewControl=new Button();break;
          case 'mx.containers::HBox':NewControl=new HBox();break;
          ... every other type, including unforeseeable custom types
        }

        this.addChild(NewControl as DisplayObject);
      }
    ]]>
  </mx:Script>
  <mx:Label text="Control Type"/>
  <mx:TextInput id="Edit"/>
  <mx:Button label="Create" click="CreateControl(event);"/>
</mx:WindowedApplication>

AFTER:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
  layout="vertical">
  <mx:Script>
    <![CDATA[
      import mx.containers.HBox;
      import mx.controls.Button;
      import mx.controls.Label;
      public function CreateControl(event:Event):void {
        var Type:String=Edit.text;

        var NewControl:Object= *???*(Type);

        this.addChild(NewControl as DisplayObject);
      }
    ]]>
  </mx:Script>
  <mx:Label text="Control Type"/>
  <mx:TextInput id="Edit"/>
  <mx:Button label="Create" click="CreateControl(event);"/>
</mx:WindowedApplication>
Joshua
  • 6,643
  • 15
  • 55
  • 76
  • Seems this is a derivative of http://stackoverflow.com/questions/3001499/dynamically-casting-in-actionscript problem I will check back when a solution to your casting has been found. But most likely you will have to put the switch statement somewhere else maybe put in another Class if you want better readability of your code. cheers :D – phwd Jun 09 '10 at 00:04
  • 1
    Also you should look at http://stackoverflow.com/questions/2016201/dynamic-object-initiation-as3 @krichard 's method can work – phwd Jun 09 '10 at 00:12

1 Answers1

4

Take a look at flash.utils.getDefinitionByName().

I haven't run this code, but you should be able to do something along the lines of

public function CreateControl(event:Event):void {
    var Type:String=Edit.text;
    var controlClass:Class = getDefinitionByName(Type) as Class;

    var NewControl:Object= new controlClass();

    this.addChild(NewControl as DisplayObject);
}
krichard
  • 286
  • 2
  • 4
  • Important: Make sure that the controls you want to use are referenced somewhere in your code, or they won't be included in your compiled final product. If this happens, you'll get a runtime error when you try to construct something with getDefinitionByName. – Ender Jun 09 '10 at 02:18