Consider the following code example:
TempList.ForEach(Function(obj)
obj.Deleted = True
End Function)
And this one:
TempList.ForEach(Function(obj) obj.Deleted = True)
I would expect the results to be the same, however the second code example does NOT change the objects in the list TempList.
This post is more to understand why…? Or at least get some help understanding why…
It’s because you used
Functioninstead ofSub. Since aFunctionreturns a value, the compiler considers that the equals sign (=) is used as a comparison, not an assignment. If you changeFunctiontoSub, the compiler would correctly consider the equals sign as an assignment:If you had a multiline lambda; you wouldn’t have had this problem:
Obviously, for the ForEach method it makes no sense to use a
Functionbecause the return value wouldn’t be used, so you should use aSub.