views:

579

answers:

2

Is there a way to make "getDefinitionByName()" work with any Object type, I have only gotten it to work with a Class:

 var test:Class = getDefinitionByName("myClass") as Class;

I need something like:

var myNumber:Number = 10; 
var test:Number = getDefinitionByName("myNumber") as Number;

trace(test); //10

Or is there another method to achieve this?

A: 

No.

That's the short answer, getDefinitionByName gets the class definition. You can access public fields and function with the [] syntax, as in trace(this["myNumber"]);. You example seems to be with a local variable which really does not really make sense.

Perhaps you can explain what you want to do, this sounds like something you can design you way out of.

Lillemanden
I have a keycode class which is full of keyCode integers, I want to be able to access them from another class, by using a string. class keyCodes { public const A:int = 65; } class myClass { function useKeyCodes(key:String){ var keyCode:int = keyCodes.getDefinitionByName(key) as int; } } I want to be able to add new keys for the keyboard event listener without thinking about key codes
RValentine
Use this: obj.useKeyCodes(flash.ui.Keyboard.A);
Jet
+3  A: 

You should define your key codes as public static constants in your KeyCodes class:

class KeyCodes { 
    public static const A:int = 65;
}

You can then use them as arguments to your useKeyCodes function:

useKeyCodes(KeyCodes.A);

This way you get the benefit of not having to remember every key code.

Niko Nyman
This is a solution. Actually, one of things, why the constants themselves were invented, is to identify literal values like PI or KEY_UP etc, so you don't need to remember it's value - just constant name. And for keycodes there's a class 'Key' (read more in manual).
Jet