I have a form with a button and the following code:
Output in when I do: dataRows.ForEach(AddressOf SetRate)
Output when I do: dataRows.ForEach(Function(dr) dr("Rate") = 9999)
I was assuming both the codes are equivalents, but they don't seem to be so. Though there are no compilation errors, the inline-function way doesn't work.
Anyone knows the reason why? Is it because the lambda expressions are evaluated when they are first used? Or is there any other reason?
vb.net Code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dt As New DataTable dt.Columns.Add("CustomerId", GetType(Integer)) dt.Columns.Add("Rate", GetType(Integer)) For i = 0 To 9 dt.Rows.Add(i, 0) Next Dim dataRows = dt.Rows.Cast(Of DataRow)().ToList dataRows.ForEach(AddressOf SetRate) '<-- this works ' dataRows.ForEach(Function(dr) dr("Rate") = 9999) '<-- this doesn't work! For Each dr In dt.Rows Console.WriteLine(dr("CustomerId") & " " & dr("Rate")) Next End Sub Sub SetRate(ByVal dr As DataRow) dr("Rate") = 9999 End Sub End Class
Output in when I do: dataRows.ForEach(AddressOf SetRate)
Code:
0 9999
1 9999
2 9999
3 9999
4 9999
5 9999
6 9999
7 9999
8 9999
9 9999
Code:
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
Anyone knows the reason why? Is it because the lambda expressions are evaluated when they are first used? Or is there any other reason?