# Using string to create class

**URL:** https://forum.kirupa.com/t/using-string-to-create-class/271294
**Category:** Uncategorized
**Created:** [September 21, 2008, 12:07am UTC](https://forum.kirupa.com/t/using-string-to-create-class/271294 "2008-09-21T00:07:19Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Sage\_of\_Fire](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sage\_of\_Fire](https://forum.kirupa.com/u/Sage_of_Fire)
#### Post date: [September 21, 2008, 12:07am UTC](https://forum.kirupa.com/t/using-string-to-create-class/271294/1 "2008-09-21T00:07:19Z")

</div>

Here’s a real head-scratcher for all you AS3 buffs out there. I have an XML file containing elements representing levels and enemies in a game I’ve been programming. I want to be able to create enemies based on information stored in their XML entries. Specifically, I want to be able to choose the class of the enemy based on a string from the XML.

However, I can’t seem to cast the string representing the enemy’s class as a class and create a new object from it. Here’s the code:

```auto

for each (var enemyInfo:XML in levelXML.level.(@num == levelNum).*) {
	var EnemyType:Class = enemyInfo.@type as Class;
	var enemy:Enemy = new EnemyType();
	enemy.x = enemyInfo.@x;
	enemy.y = enemyInfo.@y;
	addEnemy(enemy);
}

```

Is there any way to make this work, and if not, is there another way to get a class by its name? (something like getClassByName(“ClassName”)) Thanks much.
