Skip to main content
Inspiring
July 3, 2008
Question

getDefinitionByName

  • July 3, 2008
  • 6 replies
  • 761 views
Hi all,
I have a library of custom classes and I want to use an array of strings to create instances of them. The array takes the form of:
var arr:Array = [["Class1","Label1"],["Class2","label2"]...["Classn","Labeln"]];
I then loop through the array and create instances like this:
var classRef:Class = getDefinitionByName("com.domain.application.library."+libData[0]) as Class;
var obj:Object = new classRef();

This is what I have seen in the help files and tutorials but when this runs I get ReferenceError: Error #1065: Variable Class1 is not defined.
I was about to give up and then found that if I add 'new Class1();' before the loop then two instances are created, one for the manual call and one for the getDefinitionByName call. Can anyone explain what is happening here and what I could do to make this work without having to manually create an instance first?

Cheers for any help
This topic has been closed for replies.

6 replies

Inspiring
July 3, 2008
That's a good point kglad, for classes that can be exported from the library - display related classes. I had forgotten about that.
kglad
Community Expert
Community Expert
July 3, 2008
you don't.

the class just needs to be compiled. class members in your library is the typical situation where instantiating instances via strings is useful.
Inspiring
July 3, 2008
Thanks, this is all good stuff. I will try using the multiple external swf approach but as I was only doing this to try and be clever I will probably end up just doing it manually :)

As an aside, can anyone explain how getDefinitionByName is useful if you have to manually reference the class name before using it?

Thanks again
Inspiring
July 3, 2008
See this post to find some explanation and classes that will help you to handle force classes import with flash:
http://www.ultrashock.com/forums/oop/as3-force-class-import-104606.html

As far as I'm aware, you don't have any choice in Flash.

If you have a way to create a SWF on the fly, Grant skinner wrote a class (FlashLib) to get the content of a SWF with loadBytes (so you have access to your classes).
http://www.gskinner.com/blog/archives/2007/03/using_flash_sym.html

If you don't compile with Flash but with the Flex SDK, you can:
- add each class in mxmlc parameter (not great).
and the solution I'm using:
- I create an Ant task that is creating first a SWC with compc, I include a folder (the package of all the classes that might be used with getDefinitionByName). And then, I compile my SWF with mxmlc including this SWC.

I hope it helps.

Romu
www.soundstep.com



Inspiring
July 3, 2008
I understand the dilemma.
But you need to reference a class in code in order to compile it, so unless I am mistaken there's no way around that - you need to force the class to be compiled into the swf somehow. I think I've read somewhere that you don't need to define a variable and you can just force inclusion like:

Class1;

I just tested it and it seems to work from the flash timeline.

I guess the other situation would be if you were loading classes from external swfs at runtime (in which case the classes would still need to be referenced in code for the external swfs that they were compiled into), and preloaded before you used the getDefinitionByName approach for instantiation. I've not tried that yet, and perhaps there's a bit more to it, but it would seem to be a good use-case for the type of approach you're taking.
Inspiring
July 3, 2008
You need to reference the classes somewhere to force them to be compiled in the swf.

so you can just try:

var forceClass1:Class1;

And it should work (without creating an instance).
Inspiring
July 3, 2008
That does work and does only create the one instance but it still means that I will have to manually reference any class that I might want to create.
My reason for going for this approach is that in the future I want to be able to create a new class file within the package, add the new class name to a list in an XML file then load that XML and go through it creating and instance of each listed class.
Using the method I have so far I will also have to go into the AS of the main app, add var forceNewClass:NewClass; and recompile my project.
Is it possible to create my new class instances without directly referencing them by name at all?
Not sure if this explanation gets it across completely but any suggestions will be helpful.

Cheers