# New instance of an unknown type?

**URL:** <https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714>\
**Category:** flash\
**Created:** [July 11, 2008, 4:12pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714 "2008-07-11T16:12:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Groady](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/groady/32/3750_2.png) [@Groady](https://forum.kirupa.com/u/Groady)\
**Post date:** [July 11, 2008, 4:12pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/1 "2008-07-11T16:12:35Z")

</div>

I need to know how to find out what type an object is and then create a new object of that type. Can someone help me?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 11, 2008, 4:19pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/2 "2008-07-11T16:19:03Z")

</div>

see:  
[http://livedocs.adobe.com/flex/3/langref/flash/utils/package-detail.html](http://livedocs.adobe.com/flex/3/langref/flash/utils/package-detail.html)

Namely  
getQualifiedClassName  
and  
getDefinitionByName

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 4:21pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/3 "2008-07-11T16:21:47Z")

</div>

how would you do that though Sen? Got curious now.

I’m assuming this won’t work well

var sprite:Sprite = new Sprite();  
var k:\* = getQualifiedClassName(sprite);

k becomes a string, what’s the best approach of creating a new sprite from the definition inside the variable ‘k’ ?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 11, 2008, 4:39pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/4 "2008-07-11T16:39:50Z")

</div>

getQualifiedClassName will get you the string name of an instance. to go the other way around, you’d use getDefinitionByName. This takes a string and converts it to a class reference. Use that return value with new and you got yourself a new instance of that same class (assuming any required constructor parameters were included).

```auto
var sprite:Sprite = new Sprite();
var k:String = getQualifiedClassName(sprite);
var c:Class = getDefinitionByName(k) as Class; // cast since Object can also be returned
var sprite2:Object = new c(); // can't accurately type (normally) since the class is dynamic; Object or * is fine
trace(sprite2 is Sprite); // true
trace( getQualifiedClassName(sprite2) == k ); // true

```

---

<div class="post-metadata">

**Author:** ![Groady](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/groady/32/3750_2.png) [@Groady](https://forum.kirupa.com/u/Groady)\
**Post date:** [July 11, 2008, 4:43pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/5 "2008-07-11T16:43:25Z")

</div>

Thanks Sen,  
I figured it out 🙂

```auto

var myCustomObject:CustomObject = new CustomObject()
    
var type:String = getQualifiedClassName(myCustomObject);
var foundClass:Class = getDefinitionByName(type) as Class;
addChild(new foundClass());

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 4:45pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/6 "2008-07-11T16:45:04Z")

</div>

Sweet, very nice, albeit confusing why you’re casting sprite2 as an object. (“can’t accurately type (normally) since the class is dynamic”) \<- doesn’t quite click in my head as to why that is, but either way it’s working and thanks for the elaboration 🙂

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 11, 2008, 4:48pm UTC](https://forum.kirupa.com/t/new-instance-of-an-unknown-type/265714/7 "2008-07-11T16:48:30Z")

</div>

[QUOTE=sekasi;2356998]Sweet, very nice, albeit confusing why you’re casting sprite2 as an object. (“can’t accurately type (normally) since the class is dynamic”) \<- doesn’t quite click in my head as to why that is, but either way it’s working and thanks for the elaboration :)[/QUOTE]

If you knew what the class type was to begin with, you wouldn’t need to use getDefinitionByName, you’d use the class name. And since you can’t type with variables, there’s no way to type the instance you’re creating

```auto
var myType = Sprite;
var myValue:myType; // FAIL!

```
