Possible duplicate:
Why is it bad to use an iteration variable in a lambda expression
Why do I get: "An iteration variable in a lambda expression can have unexpected results"? Suppose I have the following code:
Dim writeAbleColumns As String() = {"IsSelected", "IsFeeExpense", "IsSubscriptionRedemption"} With grid For Each column As DataGridViewColumn In .Columns column.ReadOnly = Not Array.Exists(writeAbleColumns, Function(arrElement) column.Name = arrElement) Next End With
I get a warning:
Warning 1 Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable.
I do not understand why changing my code to the following changes:
Dim writeAbleColumns As String() = {"IsSelected", "IsFeeExpense", "IsSubscriptionRedemption"} With grid For Each column As DataGridViewColumn In .Columns Dim c As DataGridViewColumn = column column.ReadOnly = Not Array.Exists(writeAbleColumns, Function(arrElement) c.Name = arrElement) Next End With
In principle, nothing changes except a warning. I just have another variable pointing to my variable. Why a warning? What unexpected things can happen?
Denis source share