# Dynamically create class inside a class?

**URL:** https://forum.kirupa.com/t/dynamically-create-class-inside-a-class/308233
**Category:** Uncategorized
**Created:** [April 19, 2010, 6:17pm UTC](https://forum.kirupa.com/t/dynamically-create-class-inside-a-class/308233 "2010-04-19T18:17:06Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@Uli](https://forum.kirupa.com/u/Uli)
#### Post date: [April 19, 2010, 6:17pm UTC](https://forum.kirupa.com/t/dynamically-create-class-inside-a-class/308233/1 "2010-04-19T18:17:06Z")

</div>

Hello!

This is what i’m trying to do :

I have 1 class that extends movieclip, let’s call it Class0.  
I have 1 class that extends movieclip, let’s call it Class1.

I create an instance of Class0 on stage like :  
var myClip0:Class0 = new Class0(this, “Class1”);

The class Class0 when initializing, will create a new movieclip (myClip1) and add it as child, as an instance of Class1, like myClip0.myClip1. But it’s when i create that clip, i want to tell it, it’s an instance of Class1, like :

private var myClip1:Class1 = new Class1(this);

Where Class1 could be any class.

How do i pass Class1 to Class0 so it can create it dynamically?? Do i have to use the apply function??

EDIT :

inside Class0, i have a function that does :

```auto
if(subClass){
initSubclass(subClass);
}

private function initSubclass(type:Class):void {			
var subClass = new type(this);
addChild(subClass);			
}
}

```

But that gives me the error :

TypeError: Error #1034: Type Coercion failed: cannot convert subClass to Class

And if i try :

```auto
private function initSubclass(type:String):void {			
var _class:Class = getDefinitionByName(type) as Class;

```

I get ReferenceError: Error #1065: Variable subClass is not defined.

But it works if i do a simple :

```auto
var test:subClass = new subClass(this);

```

(where subClass here is the name of the Class)
