I have got this error
No default member found for type 'VB$AnonymousDelegate_0(Of SqlDataReader,String,Object)'.
My Code is below
dsBranch.Tables.Add(GetDataTableFromSQLReader(dr, "")) – Calling
Private Function GetDataTableFromSQLDataReader(ByVal dr As SqlDataReader, ByVal TableName As String)
Dim dt As New DataTable(TableName)
dt.Load(dr)
Return dt
End Function
Dim GetDataTableFromSQLReader = Function(dr As SqlDataReader, TableName As String) GetDataTableFromSQLDataReader(dr, TableName)
You should set the
Option StricttoOn. It would help you to declare the correct types. For instance, your Function had no return type declared. This was probably the main source of the error.Your Lambda assignment was also not correct. Lambda expressions always have to be assigned to a concrete type. This enables the compiler to infer their exact signature.
EDIT (explanation of delegates and lambda expressions):
With
you define a variable, which can hold a function, or more precisely its address, in a specialized class called a ‘delegate’. In terms of .NET this variable is a delegate which refers to a method (unless it is
Nothing). According to the declaration above, this function must accept one parameter of typeSqlDataReaderand one of typeString. The return value must be of typeDataTable.You can assign any function, which has this requested signature to the variable:
is a valid assignment. (I could have simplified the example in my original answer this way.) Now you can use the variable as if it was a function:
In reality this calls the function that was assigned to it, namely
GetDataTableFromSQLDataReader. This makes sense, if we want to get the data tables in different ways. We could assign another function, sayGetDataTableInADifferentWayto our variable. The callGetDataTableFromSQLReader(dr, "")would then call this other function without the need of having an If-Then-Else-statement when we call it.Now, to the lambda expressions with a detour over delegates. Let us take an example that is more suitable. We want to print a table with function values:
As you can see, we print the squares of 1 to 10. However, what about printing other functions? We would have to change our
PrintFunctioneach time we need to print another function. Here delegates come into the game. Let us change the declaration toNow let us declare a square function and a reciprocal function
Now we can print two different value tables with
We have not used lambda expressions yet. They are simply a very concise way of declaring delegates (or functions, if you prefer) on the fly. Instead of declaring a square and a reciprocal function, we can print the tables like this: