0

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()))

2 Answers 2

4

Use Sub instead of Function:

ThreadPool.QueueUserWorkItem(New WaitCallback(Sub(obj) svc = svcft.CreateChannel()))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Marcin! That was it!
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.