Maurice Raguse
Maurice Raguse

Reputation: 4577

AS3, dynamic conversion from String to datatype x

is there a way to convert dynamicly?

that's the default way to convert a String:

var toVal:* = int("5");
var toVal:* = Boolean("true");

but I wan't to do this:

var type:String = "int";
var toVal:* = type("5"); // <<<<< how can I do this

Upvotes: 1

Views: 743

Answers (1)

Markavian
Markavian

Reputation: 84

Try one of:

var type:Class = int;
// or
var type:Class = flash.utils.getDefinitionByName("int") as Class;

var toVal:* = type("5");

Example program:

var test:* = "5";

var type:Class = flash.utils.getDefinitionByName("int") as Class;

var toVal:* = type("5");

if(test is String) {
    trace("Test is a string"); // traces
}

if(test is int) {
    trace("Test is an int"); // ignored
}

if(toVal is String) {
    trace("toVal is a string"); // ignored
}

if(toVal is int) {
    trace("toVal is an int"); // traces
}

Upvotes: 5

Related Questions