Abdulla
Abdulla

Reputation: 1127

Check For Type with String in AS3

Say you have an object,

var obj:Object = someOtherObject;

And you need to check if it is of the type with the name stored in myString

var myString:String = someOtherString; // ex. "int", "Number", "CustomClass"

I have tried:

if(obj is getDefinitionByName(myString))

But that doesn't seem to work. How would you go about doing this?

Upvotes: 1

Views: 89

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

This works for me :

var obj:Object = 5;

var myString:String = "int";

var c:Class = getDefinitionByName(myString) as Class;
if (c && obj is c)
{
    trace("obj is of type "+myString);
}

The return type of getDefinitionByName is Object so casting it to Class seems to do the trick.

Upvotes: 1

Related Questions