mizi_sk
mizi_sk

Reputation: 1007

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?

Upvotes: 3

Views: 1173

Answers (3)

JabbyPanda
JabbyPanda

Reputation: 871

The Stack Overflow discussion on the similar topic with some ideas that can be of interest to you:

Use of object vs string vs enum

Upvotes: 0

J_A_X
J_A_X

Reputation: 12847

'magic strings' are needed. Remember that this is a dynamic language that has pros and cons to everything. This is one of those cons.

There are a few things you can do to limit error like static properties.

Upvotes: 1

taskinoor
taskinoor

Reputation: 46027

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;

Upvotes: 4

Related Questions