I'm using a Tree. Each node from the XML of the tree contains a 
attribute with the name of the class that is to be instantiated when 
a change event occurs. 

The only way for me to use the String typed attribute containing the 
name of the class to be instantiated, along with a "new" statement 
would be with a switch statement to test the value of the attribute 
and then instantiate the appropriate class. I know that I can do 
that, but that also means that every time a new node is added to the 
XML for the tree, I also have to modify the source code for the 
function that handles the change event. If I can create the class 
from the string name of the class, I can add nodes to the XML 
without any further maintenance to the switch statement in the 
source code.


--- In flexcoders@yahoogroups.com, "Roger Gonzalez" <[EMAIL PROTECTED]> 
wrote:
>
> I'm not sure how you would expect this to work!
> 
> The linker follows the dependency chain of all classes referenced 
by
> your code, and bakes them into the SWF.
> 
> Strings are just strings, it wouldn't make sense for the linker to 
look
> "inside" one.
> 
> Button isn't actually a "built-in" class, there just happens to be 
a
> dependency link to it via Application.
> 
> getClassByName (which is just a wrapper around
> ApplicationDomain.getClass, btw) can only see classes that are 
linked
> in.  There is no way for it to find some class by string that 
hasn't
> been downloaded to the client - should it email the developer and 
say
> "please compile this and send me a copy"?  :-)
> 
> What are you trying to solve such that you can't just "new" your 
class?
> 
> If you really need total decoupling like this, you might want to 
look
> into building a separately compiled SWF that contains your other
> classes, and loading that SWF at runtime.
> 
> -rg
> 
> > -----Original Message-----
> > From: flexcoders@yahoogroups.com 
> > [mailto:[EMAIL PROTECTED] On Behalf Of pwhite40
> > Sent: Saturday, February 18, 2006 9:46 PM
> > To: flexcoders@yahoogroups.com
> > Subject: [flexcoders] Re: addChild from String
> > 
> > Did you ever get an answer on this? I'm running into the same 
> > problem with getClassByName(MyCustomClass) blowing up unless 
> > I create a dummy instance of it first. Even when I do create 
> > a dummy instance first, it only works the first time I call the
> > getClassByName(MyCustomClass) method. After that, I start 
> > getting "ReferenceError: Error #1065: Variable MyCustomClass 
> > is not defined" all over again.
> > 
> > Thanks,
> > Paul
> > 
> > 
> > --- In flexcoders@yahoogroups.com, "Brendan Meutzner" 
> > <bmeutzner@> wrote:
> > >
> > > Hey Jens,
> > > 
> > > Thanks for the help.  It's now working (sort of)...
> > > 
> > > If I create an instance of a built-in class (such as Button), 
the 
> > > method below works fine.  However, if I try creating a custom 
Class 
> > > (eg. MyCustClass which extends from Canvas) like so:
> > >  
> > > import custclasses.MyCustClass;
> > > var newComponent:Object = createInstance
> > ("custclasses.MyCustClass");
> > > 
> > > it doesn't work.  I get the run-time error message "Variable 
> > > MyCustClass is not defined".
> > > 
> > > However, if I create a dummy instance of the class in my
> > application
> > > like so:
> > > 
> > > import custclasses.MyCustClass;
> > > var dummyMyCustClass:MyCustClass = new MyCustClass();
> > > 
> > > and then create another instance using the createInstance 
method,
> > it
> > > does work.
> > > 
> > > Ideas?
> > > 
> > > Thanks,
> > > 
> > > Brendan
> > > 
> > > --- In flexcoders@yahoogroups.com, Jens Halm <[EMAIL PROTECTED]> 
wrote:
> > > >
> > > > 
> > > > > Hi All,
> > > > 
> > > > > I'd like to provide a String value which represents the
> > DisplayObject
> > > > > class I want to create dynamically with addChild.  Has 
anyone 
> > > > > accomplished this?
> > > > 
> > > > You mean like this?
> > > > 
> > > > public function createInstance (className : String) : Object 
{
> > > >     var MyClass : Class = getClassByName(className);
> > > >     return new MyClass();
> > > > }
> > > > 
> > > > 
> > > > Btw.: what I really miss is a method createInstance(args : 
> > Array) in
> > > > the Class class, so I could easily provide an arbitrary 
number of 
> > > > constructor arguments not known until runtime, like this:
> > > >     
> > > > public function createInstance (className:String, 
args:Array) : 
> > Object {
> > > >     var myClass : Class = getClassByName(className);
> > > >     return myClass.createInstance(args); }
> > > > 
> > > > Class.createInstance(args:Array) would be the constructor
> > equivalent
> > > > of Function.apply(scope:Object, args:Array). If I use
> > Function.apply
> > > > after I created an instance the constructor would be called 
twice 
> > > > which would be quite dirty.
> > > > 
> > > > Consider this as a feature request...  ;)
> > > > 
> > > > 
> > > > Jens
> > > > www.oregano-server.org
> > > >
> > >
> > 
> > 
> > 
> > 
> > 
> > 
> > --
> > Flexcoders Mailing List
> > FAQ: 
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> > Search Archives: 
> > http://www.mail-archive.com/flexcoders%40yahoogroups.com
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> >
>








--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
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