Returning a list or array of objects

Jan 6, 2010 at 9:31 PM

First off, wanted to thank you for posting this project out there.

Trying to modify Example 3 to do the following...  I would like to return either an IList<StockData> or StockData[].  My call seems to be working, but I am not able to pull apart the result in actionscript.

My WCF code looks like this...

public Contract.StockData[] GetItemList()
        {
            return new Contract.StockData[] {
                new Contract.StockData { Name = "MSFT", Price = 14.00, Quantity = 4 },
                new Contract.StockData { Name = "ADB", Price = 20.00, Quantity = 6 }
            };
        }

AS2 code looks like this...

function handleStockDataReturn(re:ResultEvent) {
    trace("Got result of type "+typeof(re.result[0]));
    trace(re.result[0].Name);
    trace(re.result[1].Name);

Any help would be greatly appreciated. 

Coordinator
Jan 8, 2010 at 4:35 PM

Hmm, I hadn't tried returning an array of objects.  I thought it would just work as you have described it, but if it doesn't... I'll open it up shortly and check it out.

Unfortunately, I'm not really a flash developer, so after a bit of stumblling around, hopefully I can figure something out for you.

Coordinator
Jan 8, 2010 at 4:46 PM

As it turns out, the answer was easier to find than I realized.  Remember that re.result is an array of your MessageBody parts - re.result[0] is the value you're actually returning in Example 3.  So, try using this, instead:

 

function handleStockDataReturn(re:ResultEvent) {
    var i;
    for (i = 0; i < re.result[0].length; i++) {
        trace("Got result of type "+typeof(re.result[0][i]));
        trace(re.result[0][i].Name);
        trace(re.result[0][i].Price);
        trace(re.result[0][i].Quantity);
    }
}

 

Hope this answer found you in time!

Jan 8, 2010 at 7:07 PM

mdekrey, you are a genius!  Works like a champ.  I was looping through the 1st level of the array.

Thanks again.

Coordinator
Jan 8, 2010 at 7:59 PM

I'm glad you enjoy the project.  I've added the code to Example 3 to include both the single item and the array.