I'll leave it to other to explain why (Robert did a good job I think).
I fix it like this:

            foreach (KeyValuePair<string, int> item in murderStats)
            {
                var localItem = item;

                var se = new StringElement(localItem.Key,
localItem.Value.ToString());


                se.Tapped += delegate {
                    StatisticsDrillDownMap( se ); // This works...
                    StatisticsDrillDownMap( se.Caption ); // This does
NOT work, and only holds the last item.Key
                };
                sec.Add(se);
            }


On Fri, Nov 4, 2011 at 22:48, Dominique Louis
<[email protected]> wrote:
> Hi all,
>
> I'm not sure if this is by design or not, so hence the post.
>
> I'm assigning an anonymous delegate within a foreach loop and within the
> delegate I'm calling a function and passing a paramater to it. So far
> everything is fine. If I pass a simple string to the called function
> within the delegate, it always holds the last value of the itereration.
> If I pass an object, the passed parameter is correct.
>
>
>             foreach (KeyValuePair<string, int> item in murderStats)
>             {
>                 var se = new StringElement(item.Key,
> item.Value.ToString());
>                 se.Tapped += delegate {
>                     StatisticsDrillDownMap( se ); // This works...
>                     StatisticsDrillDownMap( se.Caption ); // This does
> NOT work, and only holds the last item.Key
>                 };
>                 sec.Add(se);
>             }
>
> where Caption is a string property.
>
> I expected both ways to work and I've not tried this sort of thing on
> Windows before hence the question.
>
> So, is this by design?
>
> Dominique.
> _______________________________________________
> MonoTouch mailing list
> [email protected]
> http://lists.ximian.com/mailman/listinfo/monotouch
>



-- 
Nic Wise
t.  +44 7788 592 806 | @fastchicken | http://www.linkedin.com/in/nicwise
b. http://www.fastchicken.co.nz/

Nearest Bus: find when the next bus is coming to your stop. http://goo.gl/Vcz1p
mobileAgent (for FreeAgent): get your accounts in your pocket.
http://goo.gl/IuBU
Trip Wallet: Keep track of your budget on the go: http://goo.gl/ePhKa
London Bike App: Find the nearest Boris Bike, and get riding! http://goo.gl/Icp2
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to