PDA

View Full Version : getDefinitionByName problem


john-paparis
11-26-2008, 06:59 PM
hi
i am loading class names through an xml file. i use the full package name for example
site_classes.pages.graphic_space.Graphic_space is the string of one class i am trying to load. inside the main class of the application i use the following code to instantiate this class, after having imported the classes as well.

var new_class_name:Class=getDefinitionByName(class_nam e) as Class;
var my_obj:Object = new new_class_name();
( class_name stores the string name of the class);

when i do this i get the following error :
ReferenceError: Error #1065: Variable Graphic_space is not defined.

but if i hardcode it > var my_obj =new Graphic_space ();
everything works fine.

to make things even more complicated if before the call to getDefinitionByName i place a trace to the full package path of the class for example trace(site_classes.pages.graphic_space.Graphic_spa ce as Class) it works !!!!!!!!!!!!
why the getDefinitionByName function throws an error ?????? if there is no trace before ??????
can someone help me ?

john-paparis
11-26-2008, 07:43 PM
well john paparis with the biiggggg papari, thats a flash bug not your problem. your class in not compiled because you imported it. the trace statement makes your class to be imported therefore you can instantiate it. a fast solution is to use a var inside your class with a reference to the class your calling but thats really stupid if you want to call tha class through a reference from a xml file..............

pj-co
11-26-2008, 08:48 PM
see: http://www.actionscript.org/forums/showthread.php3?t=188325&

Joony
11-27-2008, 12:50 AM
To cut a long story short, all you need is a reference to that class somewhere in your code so the compiler knows to include it.

If your class is called MyClass, you would put this somewhere:

MyClass;

You don't need to instantiate it, the compiler just needs that reference.

I have a separate "IncludeClasses" Class that I call using

IncludeClasses;

in my document class just to keep it tidy, but you can put the references anywhere.