# Dynamic class instance by name?

**URL:** <https://forum.kirupa.com/t/dynamic-class-instance-by-name/298181>\
**Category:** flash\
**Created:** [October 14, 2009, 8:17pm UTC](https://forum.kirupa.com/t/dynamic-class-instance-by-name/298181 "2009-10-14T20:17:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![psych](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/psych/32/2839_2.png) [@psych](https://forum.kirupa.com/u/psych)\
**Post date:** [October 14, 2009, 8:17pm UTC](https://forum.kirupa.com/t/dynamic-class-instance-by-name/298181/1 "2009-10-14T20:17:52Z")

</div>

Hi guys,

Lets say i have a simple ClassB which just draws a rect:

```auto
package
{
    import flash.display.Sprite;
    
    public class ClassB extends Sprite
    {
        public function ClassB()
        {
            super();
            
            this.graphics.beginFill(0x000000);
            this.graphics.drawRect(0,0, 500, 500);
            this.graphics.endFill();
        }
    }
}

```

And in my other ClassA i am trying to instantiate ClassB by its name - string “ClassB” with following code but that doesn’t work:

```auto
package
{
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;
    
    public class ClassA extends Sprite
    {
        public function ClassA ()
        {
            var clazz:Class = getDefinitionByName("ClassB") as Class;
            var instance:Object= new clazz();
            addChild(instance);
        }
    }
}

```

I keep getting following error so i must be missing something?

```auto
Error #1065: Variable ClassB is not defined.
    global/flash.utils::getDefinitionByName [no source]

```

Thanks,  
Best regards
