flashapache-flexactionscript-3reflectionmagic-string

ActionScript 3 object's property name to string?


I want to eliminate usage of magic strings in these:

BindingUtils.bindProperty(obj1, "propertyName", obj2, ["childObj", "anotherProperty"]);

or

var ddl:DropDownList = new DropDownList();
ddl.labelField = "propertyName";

it would be sweet to just type something like:

ddl.labelField = GetPropertyName(ComplexType.propertyName);

It would allow easy refactoring and would eliminate runtime errors when property name changes.

Any ideas?


Solution

  • Not sure whether I understand your problem correctly. You can easily define static constants in a separate class to eliminate all magic string.

    // In class ConstantContainer
    
    public static const PROPERTY_NAME:String = "propertyName";
    
    // In anywhere else
    ddl.labelField = ConstantContainer.PROPERTY_NAME;