Why is this working as expected:
list.ForEach(sub(x) x.Name = "New Name")
But this isn’t:
list.ForEach(function(x) x.Name = "New Name")
Anyone else confused?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
List(Of T).ForEach takes as an argument an Action (Sub) that doesn’t return a value not a Func (Function) that does return a value.
In VB the = sign is ambiguous. It can either be used for comparison or assignment. As a result to disambiguate the statement,
x.Name = "New Name"the team used the indicator Sub or Function to identify if this is a comparison or assignment. In the case ofSub(x) x.Name = "New Name", you are performing an assignment, or set the value of x’s Name parameter to “New Name”. In the case ofFunction(x) x.Name = New "Name"you are doing a comparison and returning if the Name parameter of x is the same as “New Name”. As a result, you have to be careful when you use Sub and Function.