How to create the class object using String with its name?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create the class object using String with its name?

shalmoo
For example I have several classes, say
TheClass1
TheClass2
TheClass3
...
TheClassN

each class extends TheBaseClass. I need to create the object, but I have its name in a String. I.e.
   var class_name:String = 'TheClass2';
When I export HaXe to JS, it is easy using eval:
   var object:TheBaseClass;
   untyped eval('this.object = new '+class_name+'();');
But how can I do it when exporting to flash9?


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: How to create the class object using String with its name?

clemos
Hi

I think you must use the Type class :
http://haxe.org/api/type

For example:
var class_name:String = "TheClass2";
var object:TheBaseClass = Type.createInstance( Type.resolveClass(
class_name ), [] );

But maybe you should try to find some other OO strategy than naming
your classes like that...
+++++++
Clément

On Wed, May 6, 2009 at 12:48 PM, Shalmoo Yakoubov <[hidden email]> wrote:

> For example I have several classes, say
> TheClass1
> TheClass2
> TheClass3
> ...
> TheClassN
>
> each class extends TheBaseClass. I need to create the object, but I have its
> name in a String. I.e.
>    var class_name:String = 'TheClass2';
> When I export HaXe to JS, it is easy using eval:
>    var object:TheBaseClass;
>    untyped eval('this.object = new '+class_name+'();');
> But how can I do it when exporting to flash9?
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: How to create the class object using String with its name?

shalmoo
Thank you very much, Clément.

Don't worry, it does not affect my OO strategy, I'm gonna use it only for attaching symbols from imported swf file library (I have a lot of numbered symbols)

On Wed, May 6, 2009 at 3:13 PM, clemos <[hidden email]> wrote:
Hi

I think you must use the Type class :
http://haxe.org/api/type

For example:
var class_name:String = "TheClass2";
var object:TheBaseClass = Type.createInstance( Type.resolveClass(
class_name ), [] );

But maybe you should try to find some other OO strategy than naming
your classes like that...
+++++++
Clément

On Wed, May 6, 2009 at 12:48 PM, Shalmoo Yakoubov <[hidden email]> wrote:
> For example I have several classes, say
> TheClass1
> TheClass2
> TheClass3
> ...
> TheClassN
>
> each class extends TheBaseClass. I need to create the object, but I have its
> name in a String. I.e.
>    var class_name:String = 'TheClass2';
> When I export HaXe to JS, it is easy using eval:
>    var object:TheBaseClass;
>    untyped eval('this.object = new '+class_name+'();');
> But how can I do it when exporting to flash9?
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: How to create the class object using String with its name?

clemos
Hi

Usually, attaching symbols from imported swf library can be done this way :
var mc:MovieClip = flash.Lib.attach(symbol_id);
flash.Lib.current.addChild(mc); // add the symbol to the main movieclip

+++++++++
Clément

On Wed, May 6, 2009 at 3:48 PM, Shalmoo Yakoubov <[hidden email]> wrote:

> Thank you very much, Clément.
>
> Don't worry, it does not affect my OO strategy, I'm gonna use it only for
> attaching symbols from imported swf file library (I have a lot of numbered
> symbols)
>
> On Wed, May 6, 2009 at 3:13 PM, clemos <[hidden email]> wrote:
>>
>> Hi
>>
>> I think you must use the Type class :
>> http://haxe.org/api/type
>>
>> For example:
>> var class_name:String = "TheClass2";
>> var object:TheBaseClass = Type.createInstance( Type.resolveClass(
>> class_name ), [] );
>>
>> But maybe you should try to find some other OO strategy than naming
>> your classes like that...
>> +++++++
>> Clément
>>
>> On Wed, May 6, 2009 at 12:48 PM, Shalmoo Yakoubov <[hidden email]>
>> wrote:
>> > For example I have several classes, say
>> > TheClass1
>> > TheClass2
>> > TheClass3
>> > ...
>> > TheClassN
>> >
>> > each class extends TheBaseClass. I need to create the object, but I have
>> > its
>> > name in a String. I.e.
>> >    var class_name:String = 'TheClass2';
>> > When I export HaXe to JS, it is easy using eval:
>> >    var object:TheBaseClass;
>> >    untyped eval('this.object = new '+class_name+'();');
>> > But how can I do it when exporting to flash9?
>> >
>> >
>> > --
>> > haXe - an open source web programming language
>> > http://haxe.org
>> >
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org