Variable Object Property Names in As3 flex?

I need to create variable object property names for use with the data grid component.

This works:

 data = new Object();
 data.some_name = "the data";

But this does not:

 data = new Object();
 colName = "some_name";
 data[colName] = "the data";

Can anyone help me? Can object property names be variable?

Upvotes: 1

Views: 2298

Answers (3)

Adrian Pirvulescu
Adrian Pirvulescu

Reputation: 4340

It does not work because:

colName is a variable, which means is a pointer to the address where the string "some_name" is placed

Upvotes: 0

PatrickS
PatrickS

Reputation: 9572

Maybe you forgot to assign the some_name property! the following should work...

 var data:Object = new Object();
 data.some_name = "the data";
 colName = "some_name";
 data[colName] = "the data";

Upvotes: 1

Chunky Chunk
Chunky Chunk

Reputation: 17237

var colName:String = "Column Title";
var colNameNoSpace:String = "ColumnTitle"

var dataObject:Object = new Object();
dataObject[colName] = "What's the problem?";
dataObject[colNameNoSpace] = "There's no problem!"

trace(dataObject["Column Title"]);  //What's the problem?
trace(dataObject[colName]);         //What's the problem?
trace(dataObject.ColumnTitle);      //There's no problem!
trace(dataObject[colNameNoSpace]);  //There's no problem!

Upvotes: 6

Related Questions