Spot The Mistake 1

August 21, 2010 – 18:30

This one had me puzzled for about an hour, I shall endeavour to post the answer in the comments in short order.

foreach (var id in CharacterIdentities)
    (x) => OnSkillInTrainingUpdated(x, id));
Be Sociable, Share!
    1. 4 Responses to “Spot The Mistake 1”

    2. Not knowing the calls or object model, I’m going to guess that the id.CharacterId is incorrect and should just be id because you got the id from Character Identities?

      By Kirith Kodachi on Aug 22, 2010

    3. As pointed out by Kirith Kodachi the bug is obscured somewhat by the object model I am working with, however you would get the same bug from the following code:

      for (var i = 0; i < 5; i++)
      	DoSomethingAsync(x => DoSomething(i));

      The result would be DoSomething would be called with the parameter == 4, five times because the variable existed within it’s initial scope, then kept in scope by the Lambda expression.

      In the end the resolution was to use the following code:

      for (var i = 0; i < 5; i++)
      	int temp = i;
      	DoSomethingAsync(x => DoSomething(temp));

      Which results in the DoSomething being called five times, with the number 0..4.

      By Richard Slater on Aug 23, 2010

    4. Ah right, I’ve run into that before. Resharper usually reminds me not to do that 🙂

      By Kirith Kodachi on Aug 24, 2010

    1. 1 Trackback(s)

    2. Aug 21, 2010: Tweets that mention Spot The Mistake 1 --

    Post a Comment

    Please leave these two fields as-is: