Older version

Iteration X of Y is either off-by-one and/or confusing :)

James Manning 6 years ago • updated by Lior Kerner 3 years ago 3
screen shot @ http://i.imgur.com/vYYqi.png (the image button in this editor doesn't support from-clipboard or from-url, but that's not a BugAid problem :)

In console app with breakpoint on the Console line, the first time hitting it the tooltip says 'Iteration 0 of 9'.  Insofar as it's trying to tell me that I'm in the first time through of 10, it seems like those should be 'Iteration 1 of 10', although I understand that's a little weird relative to zero-based indexing.

However, it should be clear that there are 10 iterations through the loop, so it seems like the latter number should be 10 regardless, which would seem to somewhat 'force' the former number to start at 1 instead (IMHO).

            var nums = Enumerable.Range(0, 10);            foreach (var num in nums)            {                Console.WriteLine(num);            }
Thank you for your feedback. Our initial reasoning for this was that using a 1-based index might be more confusing:  If we use "Iteration 1 of 4" and you tried, by mistake, to write "nums[1]" in the watch window, you would get the 2nd item, and not the 1st.

Regardless, since several users have complained about this, we're going to either change the word "Iteration" to "Index", which might be more helpful, or just make the plunge and switch to 1-based indexing, in one of the next updates.
IMHO, if you just include the current value of the foreach range variable (or whatever you'd call 'num' in the statement 'foreach (var num in ...)' then 1-based would be fine.  

If I were iterating over something like the below and hit a breakpoint on the Console.WriteLine, I'd like to see the UI say:       val="bar" [item 2 of 3]

Including the actual 'range variable' is really useful (IMHO, more useful than the X of Y bit, although both would be great), and it takes the focus off of the X of Y bit and makes it much less likely (IMHO) that someone would see that and decide to do someStrings[2] and expect to see 'bar'

var someStrings = new[] { "foo", "bar", "baz" };
foreach (var val in someStrings)