----- Original Message ----- 
From: "john fisher" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Saturday, October 18, 2008 1:40 AM
Subject: [flexcoders] Syntax of using a variable to make an object


>I am stuck on some basic AS syntax.
>
> Here is some Adobe sample code:
>
> [Bindable]
>        public var dpac:ArrayCollection =  new ArrayCollection([
>           {A:2000},{A:3000}, {A:6000} ]);
>
>        public function addDataItem():void {
>            var o:Object = {"A":2000};
>            dpac.addItem(o);
>        }
> ////////////////////////////////////////
>
> Inside a loop, I need to add a data item to an arraycollection, but I
> need to get the name of the data  and the result of the data from an XML
> file.
> OK I've got those two things working, but now my name and dataresult are
> variables not ints and strings.
>
> [Bindable]
>        public var dpac:ArrayCollection =  new ArrayCollection;
> for each ( var something:XML in myXML.children()) {
> var dataresult:Obj = something ;  // don't know type of result ahead of 
> time...
> var name:String = something.name ;
>
> var o:Object = { name + ':' + dataresult } // WRONG! ////////
>            dpac.addItem(o);
>
> }
>
> I am going wrong where I try to create an object out of method-results or 
> variables that can be additem'ed to an arraycollection.
> And I don't really get the curly brace syntax, as you can see...

The curly bracket syntax is for compile time declaration only where the 
compiler knows all of the values at the time of compilation. In your example 
variables are involved so the compiler gives you an error.

Generally speaking the variable equivalent of

var o:Object = { x: 1234};

would be

var o:Object = new Object();
o.x = 1234;

your code is trying to have the variable name as a vaiable itself, so the 
equivalent would be:

o["x"] = 1234;

var variableName:String = "x";
var variableValue:Number = 1234;

o[variableName] = variableValue.

I'm rather suspicious of your XML handling code, but I won't go there!

Paul





>
> Thanks for guidance here...
> John
>
> PS what is the proper name for this like 'passing by reference vs passing 
> by value' or?
>
>
>
> ------------------------------------
>
> --
> Flexcoders Mailing List
> FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> Alternative FAQ location: 
> https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847
> Search Archives: 
> http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups 
> Links
>
>
>
> 

Reply via email to