This subject has been discussed many times (either here or on
FlashCoders).

The main problem lies in the fact that the class must be compiled into
the swf. There are several ways to do this. One is to have a swf for
each class with only one line of code on the root, to make sure the
class is compiled:

myPackage.myClass;

Then u can load the swf into your main movie and have the class
instantiated.

Another option is to have a list with possible classes in a class file:

class Components
{
        public function Components()
        {
                myPackage.MyClass;
                myPackage.MyOtherClass;
        };
};


To call the class dynamicly u can use code like this:

function getInstance(class_str:String):Object
{
        
        var classRef:Object = _global;
        var classPath_array:Array = class_str.split(".");
        
        for (var i = 0; i < classPath_array.length; i++)
        {
                classRef = classRef[classPath_array];
        };

        return new classRef();
};

var instance = getInstance("myPackage.MyClass");


Greetz Erik



-----Original Message-----
From: Robert Stuttaford [mailto:[EMAIL PROTECTED] 
Sent: vrijdag 25 maart 2005 12:53
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Silly question


Probably not specifically Flex related.

Can I do what the following is accomplishing, using Actionscript 2, with
out statically mapping it out?

switch ( className ) {
        // base
        case "ClassA":
                return new ClassA();
                break;
        case "ClassB":
                return new ClassB();
                break;
}

I'd love to be able to

var x = new [ className ]();

But obviously the compiler doesn't like that. 

I'm looking to have classes instantiated based on nodenames from XML,
but without the convertor actually having any knowledge of what classes
need to be instantiated.

Any ideas? :)

Robert



 
Yahoo! Groups Links



 





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to