1

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?

2
  • You really should indent your code to make it more legible, I just did it for you , doesn't this look much nicer :) ? Commented Dec 3, 2010 at 7:56
  • thans PatrickS... yeah it is. Commented Dec 3, 2010 at 11:38

3 Answers 3

6
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!
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

It does not work because:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.