2 Replies Latest reply on Jan 21, 2009 6:18 AM by Richard_Abbott

    Dynamically create unknown object

    Richard_Abbott Level 3
      Hi all,
      would appreciate any thoughts on the following. I have an interface where the layout, background image, colour/alpha scheme, presence/absence of some controls etc is driven by an XML descriptor file, so that different menu choices lead to suitable reorganisation of the interface.
      I would like to add the facility to have optional controls where I specify the type in the descriptor file - for example one choice might use a text area while another one an image. In that simple case I could obviously have a flag stored in the descriptor and a switch statement, but I would prefer not to put that choice too baldly into the code. The more likely situation is that I have a number of custom components all subclassed from, say, panel or canvas, and make a choice between them.
      So what I want to do is something like:
      [xml file]
      <state id='1' component='mycustom1' />
      >state id='2' component='mycustom2' />
      var comp: Object
      //...read XML... suppose we are in state 2
      comp = new Object() as mycustom2;
      // ... etc, now assuming that comp is a mycustom2 object

      Hope that makes sense. Is that at all possible? I am happy to have mycustom1 etc all closely related to each other via subclassing.
      Thanks for any ideas in advance,
        • 1. Re: Dynamically create unknown object
          Cesare Rocchi Level 2
          you can do something like this (very quickly written code):

          import flash.utils.getDefinitionByName;

          var className:String = "MyClassName

          // From name to Class
          var cls:Class = getDefinitionByName(className) as Class;

          // now use cls as you prefer
          var instance:Canvas = new cls();

          Hope this helps,

          • 2. Re: Dynamically create unknown object
            Richard_Abbott Level 3
            thanks for this - it set me on exactly the right path, though in a few cases you have to add another call. The Flex cookbook article "Create Component Instance from Class Name" and the Developer Connection article "ActionScript and .NET client-server class mappings" filled in the gaps. Basically your code will work if you have an object of the required type already instantiated somewhere, but the compiler won't have bothered to link the code in if all you have is a reference somewhere. So inside a button click handler I did:

            var ClassReference:Class;
            var instance:Object;
            ClassReference = getDefinitionByName("mx.controls.Button") as Class;
            instance = new ClassReference();
            instance.x = 100;
            instance.y = 50;
            instance.label = "Hello world";

            ClassReference = getDefinitionByName("mx.controls.TextArea") as Class;
            flash.net.registerClassAlias( "mx.controls.TextArea", mx.controls.TextArea )
            instance = new ClassReference();
            instance.x = 200;
            instance.y = 10;
            instance.text = "Hello world";

            The registerClassAlias call is necessary in the second case because I do not have a real TextArea anywhere in my code, whereas I do have a real button. With the extra call all works splendidly.
            Your help much appreciated,