1

I need to create somethink like this.

var s:String;
var b1:Boolean;
if (b1==true)
   s="Class1()"
else 
   s="Class2()";
var o:Object;
o = new [s]()   // create or new Class1(), or new Class2
4
  • 1
    Why does it need to be a string? Why not make an Interface that both Class1 and Class2 implement? Or a Class they both inherit from? Commented Mar 7, 2016 at 14:05
  • You can instead create a reference like: ref['myclass'] = MyClass and then simply do new ref['myclass'](); Commented Mar 7, 2016 at 18:12
  • Thanks, but i have error with this line "var classType:Class = somethingIsTrue ? Class1 : Class2;" 1120: Access of undefined property somethingIsTrue Commented Mar 9, 2016 at 6:25
  • Sorry, now i understend what is mean. It's like (Boolean) ? Class1 : Class2; Commented Mar 9, 2016 at 6:44

2 Answers 2

1

You haven't given much information so I don't know if you really need to do this, but there are a few good reasons you might, and it can be done using getDefinitionByName:

var className:String = somethingIsTrue ? "Class1" : "Class2";
var classType:Class = getDefinitionByName(className) as Class;
if (classType)
    trace(new classType());

Note that:

  • The class name must be fully qualified, meaning if your class is in a package you must include the package. Example: "path.to.my.stuff.Class1"
  • If there are no "real" references to the class in your code (a string with the class name doesn't count) it will not get compiled into your SWF, and therefor getDefinitionByName will not find it. The easiest way to solve this is to put a type declaration somewhere, such as var a:Class1, b:Class2. Another way is to put those classes in a swc library.
  • Using getDefinitionByName is very slow (thanks @Philarmon), so to be clear: avoid this unless you really have to do it.

EDIT: Here's an example of how you can do it without using a string:

var classType:Class = somethingIsTrue ? Class1 : Class2;
var instance:Object = new classType();

As you can see, you don't have to use a string if you actually know the class name ahead of time. In fact cases where you don't know the class name ahead of time is rare.

Even if you are starting with a string (say from JSON serialized user data), as long as you know the class names ahead of time you can map the strings to class references:

var classes:Object = {
    "alfa": Class1,
    "bravo": Class2
}

var className:String = "alfa";
var instance:Object = new classes[key];
Sign up to request clarification or add additional context in comments.

5 Comments

Keep in mind that getDefinitionByName() is dead slow though, so if you need to create a lot of stuff this way, it might be better to pass the class name directly or - if it have to be a string - just do a mapping class that will return a class name based on strings
Indeed, if there's a way to not rely on a string it's certainly better. The OP at this point hasn't made that an option.
Thanks, but i have error with this line "var classType:Class = somethingIsTrue ? Class1 : Class2;" 1120: Access of undefined property somethingIsTrue
Sorry, now i understend what is mean. It's like (Boolean) ? Class1 : Class2;
Yeah replace somethingIsTrue with your own condition. You don't need to use a ternary, either, you could just do classType = Class1
0

Without knowing the reasoning behind your situation, most likely what you are looking for is an Interface. First create an interface (see here for an example):

public interface MyInterface {
  //Include any methods the classes should implement here
}

Then each class should implement that interface:

public class Class1 implements MyInterface

And finally, when creating them, you can do it like this:

var o:MyInterface;
if (b1 == true)
  o = new Class1();
else 
  o = new Class2();

Comments

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.