user2434400
user2434400

Reputation: 619

Lambda Expression from c# to vb

I have a simply lambda expression in c# that works. Now I need to convert it into vb but couldn't get it to work - get an error that says "oprator '=' is not defined for types 'T' and 'T'. Can someone tell me what I am doing wrong?

C# code that works:

ThreadPool.QueueUserWorkItem(new WaitCallback(
(obj) =>
{
    svc = svcft.CreateChannel()
}))

My VB convrsion that doesn't work:

ThreadPool.QueueUserWorkItem(New WaitCallback(Function(obj) svc = svcft.CreateChannel()))

Upvotes: 0

Views: 557

Answers (2)

Jim Wooley
Jim Wooley

Reputation: 10398

A bit more information. In C#, the lambda doesn't care wither the body has a return value or not. In VB, you have to be explicit in your Lambda's just as you do in your method signatures. For example, in VB you can't do the following:

Public Sub Foo() As String
End Sub

Because if you have a return type, it's a Function, not a Sub. Similarly with Lambda's, you have to use the Sub or Function keywords depending on if you have a return value or not. This has a subtle difference in terms of comparison vs. assignment. Consider the following two lambdas:

Dim y as Integer
Dim assign = Sub(x) y = x
Dim compare = Function(x) y = x

In the first case, y will be assigned the value of x. In the second case, the lambda will return true/false depending on if y and x are the same.

Upvotes: 2

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Use Sub instead of Function:

ThreadPool.QueueUserWorkItem(New WaitCallback(Sub(obj) svc = svcft.CreateChannel()))

Upvotes: 4

Related Questions