I have a situation where I have someFunction(int), and I need to generate programmatically n buttons that will call it. What this means is that I want to create buttons B1, B2, … Bn that call someFunction(1), someFunction(2), … someFunction(n) when clicked.
This is how I attempted to do this (semi-pseudocode):
for (int i = 1; i <= n; i++) {
Button b = new Button();
b.Caption = "Value " + n; // non-WPF: b.Text = "Value " + n;
b.Click += (sender, event) => {
someFunction(i);
}
}
What bugs me about this is that when I click on the first button (B1), with a debugger over someFunction(i), it tells me that it’s calling someFunction(n + 1).
I’m not sure why this is, or how to fix it. The work-around I use is to use, instead of someFunction(i), someFunction(int.Parse(i.ToString()) (to create a copy of i). But this seems shady to me, because integers should be value types.
I believe you understand WHY this happens. The problem is it captures the variable
iitself, not its value. The workaround that seems better (without toString and int.parse) to me is to declare another local var that copiesiThis way the captured variable will be
localiand it will remain the same across the loop.