1

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

1 Answer 1

5

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think you have to cast it as a Class to assign it to a strongly typed variable like that. var type:Class = Class(int), but other than that it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.