1
votes

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?

3
You really should indent your code to make it more legible, I just did it for you , doesn't this look much nicer :) ?PatrickS
thans PatrickS... yeah it is.Muhammad Husnain Ashfaq

3 Answers

6
votes
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!
1
votes

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";
0
votes

It does not work because:

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