Code Question

599
6
09-10-2010 10:02 AM
JamesFaron
Occasional Contributor
I am trying to create a variable that will show the string value of an Array object deliminated with a comma. In short, I have created an ArrayCollection from FeatureSet.Attributes, modified it as in the code below (from Dan yesterday), and then applied a 'for each' statement to create the string values. All well and good, but the values that are returned are not delimited (I need a comma separating the values so that I can use it as a variable in a script). I created an Array composed of the string output (myString in code), and I applied the conventional methods to return comma separated values (params.toString(); and then params.join(","), but the results are the same: I get the values of the Array strung together without a delimiter.
function onRbeResult(featureSet:FeatureSet, token:Object = null):void        
      {
       try
       {
        for each (var gra:Graphic in featureSet.features)                    
        {   
        var obj:Object = gra.attributes;
        obj["gid"] = gid;
        gid +=1;
        }
        gridDataProvider = featureSet.attributes;
        LLdata.dataProvider = gridDataProvider;
        var recAC:ArrayCollection = createRbeRecordData(featureSet,rbeID);
        wRepeater.dataProvider = recAC;

        var stopAC:ArrayCollection = new ArrayCollection;
        var newObj:Object;
        var j:int=0;
        for(j=0;j<featureSet.attributes.length; j++)
        {
         newObj = featureSet.attributes;
         delete newObj.NAME;
         delete newObj.POLICY;
         stopAC.addItem(newObj);
        }


        for each( var obj:Object in stopAC )
        {
         for each( var obj2:Object in obj )
         {
         myString += String( obj2 );
         }
        }

        trace(myString);
        var params:Array = new Array(myString);
//        var paramsCSV:String = params.toString();//didn't work
        var paramsCSV:String = params.join(",");//didn't work either
        Alert.show(paramsCSV);
        trace(paramsCSV);


The result of the trace is always as follows (the results change dynamically, but no change in format): null0642473168584021869369384069146424735691143668584076662168642473

What I am looking for is e.g.642473,685840,1869369,etc.

Perhaps the null value is creating a problem, or the code is wrong. Any help or pointers will be greatly appreciated, as always.

Jim Faron
Tags (2)
0 Kudos
6 Replies
DasaPaddock
Esri Regular Contributor
It looks like your params:Array only has one value in it. It needs to have more than one value for join() to concatenate them. You could try pushing values into the array inside your for loop, or just creating the string the way you want manually.
0 Kudos
JamesFaron
Occasional Contributor
Dasa,

I changed the relevant code according to your suggestion (params.push(myString), and it is starting to do what I expect. However the results now appears as "null642473,null6424730,null6424730685840,null64247306858401,null642473068584011869369etc'. Each value being with 'null' contains the first value with an incremental increase from the next value (with the addition of a 0). What I am aiming at is to return 642473,685840,11869369 etc(as underlined above). 
I can't manually create the string because it is the result of dynamic function, so that the results will change everytime the function is called.
It seems that the 'myString' loop is the problem. It is intended to display the text value of the ArrayCollection objects (otherwise I would trace [Object,object].
var stopAC:ArrayCollection = new ArrayCollection;
        var newObj:Object;
        var j:int=0;
        for(j=0;j<featureSet.attributes.length; j++)
        {
         newObj = featureSet.attributes;
         delete newObj.NAME;
         delete newObj.POLICY;
         stopAC.addItem(newObj);
        }
var params:Array = new Array;

        for each( var obj:Object in stopAC )
        {
         for each( var obj2:Object in obj )
         {
         myString += String( obj2 );
         params.push(myString);
         }
        }
var paramsCSV:String = params.join(",");
trace(paramsCSV);


Seems like I'm getting closer, but I am definitely missing something.

Thanks,
Jim Faron
0 Kudos
DasaPaddock
Esri Regular Contributor
If I understand correctly, you want a comma separated list of of values of one of the features' attributes? Is this correct? What is the name of the attribute?

One note is that it's best to only call featureSet.attributes once since this creates a new Array every time it's called:
http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/FeatureSet.html#attributes
0 Kudos
JamesFaron
Occasional Contributor
Yes, you are correct. The featureset.Attributes has three fields: Name, Policy, and Stop_id. I only want the Stop_id in the comma separated list variable. I intend to use that variable to send to a Webservice in a script. I can make the Webservice and script work by using the selectedItem of a list (I define the labelField=Stop_ID). I want to send multiple values, in fact all of the values each time the function is run. If there is another way to do this, I would love to know. For example, if there is a better way to get the text value of the one field (Stop_ID) that is contained in featureSet.attributes, that would be fantastic. I would rather not, if I don't have to, limit featureSet.Attributes to one field only in the function (the fields are defined in the config.xml), as that would affect its use as a dataProvider for a grid.
0 Kudos
DasaPaddock
Esri Regular Contributor
Here's how I would do this. You could use featureSet.attributes, but I think looping through the features makes the code clearer.

var ids:Array = [];
for each (var graphic:Graphic in featureSet.features)
{
    ids.push(graphic.attributes["Stop_ID"]);
}
var paramsCSV:String = ids.join();
0 Kudos
JamesFaron
Occasional Contributor
Dasa,

That was it! Thanks again for your help!

Jim Faron
0 Kudos