PDA

View Full Version : Dynamic class names


wrs
03-19-2009, 03:58 AM
Hi,

I'm attempting to create an object of a class to which the name is only known at runtime.

I'm most familiar with php, in which case I would do something like:


$className = $_POST['class'];
$obj = new $className;


How can I do this using AS3? I had thought maybe getDefinitionByName() would help, however it appears this is only for classes which have already been instantiated.

Peter Cowling
03-19-2009, 08:05 PM
Clarifying:

When you say 'object of a class to which the name is only known at runtime', do you mean that you want to:


write a class and instantiate it at runtime?
instantiate an object of a pre-written class at runtime?
you want to get the classname for a given object at runtime?


EDIT****
Or the one I missed out - alter a class at runtime.

mastermind81
03-20-2009, 01:02 PM
use this

var className = "YourClass";
var classDefination:Class = flash.system.ApplicationDomain.getDefinition(class Name) as Class;
var obj:classDefination = new classDefination();

wrs
03-20-2009, 06:04 PM
I believe instantiate an object of a pre-written class at runtime is the most appropriate.

mastermind, I have used similar code but recieve Error #1065: Variable myClass is not defined

Where 'myClass' is the name of the class i wish to create an object of.

If I understand correctly, that getDefinition() call aids in getting the class name of an already existing object. This isnt possible with my code as I am only trying to create objects on the fly... I shall try to explain..

The app is supposed to load and process several binary files, all of which have a similar structure ( a header, data block, and string table ) however the actual contents of the data block needs to be processed in various ways ( partially dependant on the header content ).

I have done this in PHP by writing code to first handle the opening and reading of the file as well as providing some generic sanity checking.

I then wrote a class file DBCFile providing methods for parsing the header, data, and string blocks. Basically anything which was generic for all the files I wished to process. This class is never instantiated directly, only used by others to extend off.

I then wrote several additional class files, all of which extended DBCFile and provided the real structure and processing logic for each individual file signature. It is these classes which are instantiated as needed, once the file signature has been identified ( at runtime ).

There are also occasions where I would need to call a method but without knowing the exact name of the method :|


There are probably much easier ways of doing this, i'm just trying to keep my code as similar to my existing php code as possible while still learning AS3.

wvxvw
03-20-2009, 06:11 PM
ApplicationDomain.getDefinition() requires you to provide the full class name (that is including the package name).
i.e. for MovieClip it'd be "flash.display::MovieClip".
Also, if you never import / use those classes you want to get through this method, it will return null. So, you have to make sure you compile those classes into SWF to which ApplicationDomain instance you're accessing belongs.
Also, if not sure if ApplicationDomain has definition for the class you can first try if (myApplicationDomain.hasDefinition("fully.qualified::ClasName")
{
// your code here
}

Peter Cowling
03-20-2009, 07:20 PM
Per the solutions by wvxvw and mastermind81: the adobe docs have some good examples (http://livedocs.adobe.com/flex/gumbo/langref/flash/system/ApplicationDomain.html) of working with applicationdomain.