title: ActionScript - Passing & Instantiating Class Reference Containing Required Parameters?
body: <p>I'm unsuccessfully attempting to instantiate a reference of a class that is passed as a parameter to another class.</p>
  
  <p>in this example there are 3 classes: <strong>MainClass, Canvas, MyCircle</strong></p>
  
  <p>from the MainClass i am creating an instance of Canvas, which is passed a class reference of MyCircle as i want to create instances of MyCircle from within Canvas.  however, the MyCircle constructor contains required parameters that are created from within Canvas.</p>
  
  <p>how can i pass and instantiate a class reference with required parameters?</p>
  
  <p><strong>MyCircle:</strong></p>
  
  <pre><code>package
  {
      //Imports
      import flash.display.Shape;
  
      //Class
      public class MyCircle extends Shape
      {
          //Constructor
          public function MyCircle(color:uint, radius:uint)
          {
              graphics.beginFill(color, 1.0);
              graphics.drawCircle(0, 0, radius);
              graphics.endFill();
          }
      }
  }
  </code></pre>
  
  <p><strong>Canvas:</strong></p>
  
  <pre><code>package
  {
      //Imports
      import flash.display.DisplayObject;
      import flash.display.Sprite;
      import flash.utils.getDefinitionByName;
  
      //Class
      public class Canvas extends Sprite
      {
          //Constructor
          public function Canvas(circleClassReference:String, amount:uint)
          {
              var CircleReference:Class = getDefinitionByName(circleClassReference) as Class;
  
              for (var i:uint = 0; i &lt; amount; i++)
              {
                  var randomColor:uint = Math.random() * 0xFFFFFF;
                  var randomRadius:uint = Math.random() * 100 + 50;
  
                  var circleInstance:DisplayObject = new CircleReference(randomColor, randomRadius);
                  circleInstance.y = i * randomRadius;
  
                  addChild(circleInstance);   
              }
          }
      }   
  }
  </code></pre>
  
  <p><strong>MainClass:</strong></p>
  
  <pre><code>package
  {
      //Imports
      import flash.display.Sprite;
  
      //Class
      public class MainClass extends Sprite
      {
          //Constructor
          public function MainClass()
          {
              var myCanvas:Canvas = new Canvas("MyCircle", 10);
              addChild(myCanvas)
          }
      }
  } 
  </code></pre>
  
  <hr>
  
  <p><strong>[EDIT]</strong></p>
  
  <hr>
  
  <p>it seems that passing the actual Class instead of the name of the class avoids the need for the dirty fix that is mentioned below.</p>
  
  <p><strong>MainClass Constructor</strong></p>
  
  <pre><code>public function MainClass()
  {   
      var myCanvas:Canvas = new Canvas(MyCircle, 10);
      addChild(myCanvas)
  }
  </code></pre>
  
  <p><strong>Canvas Constructor</strong></p>
  
  <pre><code>public function Canvas(circleClassReference:Class, amount:uint)
  {
      var CircleReference:Class = circleClassReference;
  
      for (var i:uint = 0; i &lt; amount; i++)
      {
          var randomColor:uint = Math.random() * 0xFFFFFF;
          var randomRadius:uint = Math.random() * 100 + 50;
  
          var circleInstance:DisplayObject = new CircleReference(randomColor, randomRadius);
          circleInstance.y = i * randomRadius;
  
          addChild(circleInstance);   
      }
  }
  </code></pre>