Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Creating Flex Components In ActionScript
    primarykey
    data
    text
    <p>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.</p> <p>Before:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"&gt; &lt;mx:Script&gt; &lt;![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); } ]]&gt; &lt;/mx:Script&gt; &lt;mx:Label text="Control Type"/&gt; &lt;mx:TextInput id="Edit"/&gt; &lt;mx:Button label="Create" click="CreateControl(event);"/&gt; &lt;/mx:WindowedApplication&gt; </code></pre> <p>AFTER:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"&gt; &lt;mx:Script&gt; &lt;![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); } ]]&gt; &lt;/mx:Script&gt; &lt;mx:Label text="Control Type"/&gt; &lt;mx:TextInput id="Edit"/&gt; &lt;mx:Button label="Create" click="CreateControl(event);"/&gt; &lt;/mx:WindowedApplication&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload