I have this code:
for (int i = 0; i < 7; ++i)
Roption.DropDownItems.Add(string.Concat("Item &", i), null, delegate { Reset(i); });
where Reset()
is called when the dropdown item is clicked.
The problem is that no matter which dropdown item is clicked, the argument passed to Reset
is 7 (the value of i
after the loop terminates), not the value of i
when the Roption.DropDownItems.Add
function is called. What causes this behavior and how can it be fixed? (I don't really want to hard code 0 through 6 in seven otherwise identical statements).