C# Lambda to VB.Net
I'm trying to convert a function in C# to VB.Net 2008 and can't seem to make the Lambda expression work. The code is taken from a neat little C# SMTP server that saves emails to Azure blob storage
Any help would be appreciated greatly.
public void Run()
{
var mutex = new ManualResetEvent(false);
while (true)
{
mutex.Reset();
listener.BeginAcceptSocket((ar) 开发者_如何学编程=>
{
mutex.Set();
processor.ProcessConnection(listener.EndAcceptSocket(ar));
}, null);
mutex.WaitOne();
}
}
The lambda is basically just shorthand for an anonymous delegate.
so replace the
(ar)=> {//Do Stuff}
with
Sub(ar)
'Do stuff
End Sub
I managed to get it converted correctly for VB 2008 using InstantVB from Tangible Software
Public Sub Run()
Dim mutex = New ManualResetEvent(False)
Do
mutex.Reset()
listener.BeginAcceptSocket(Function(ar) AnonymousMethod1(ar, mutex), Nothing)
mutex.WaitOne()
Loop
End Sub
Private Function AnonymousMethod1(ByVal ar As Object, ByVal mutex As ManualResetEvent) As Object
mutex.Set()
processor.ProcessConnection(listener.EndAcceptSocket(ar))
Return Nothing
End Function
I infer you're using Visual Studio 2008 in which case you can't write multiline lambda statements in VS2008.
You'll have to be using VS2010 otherwise you'll have to use Anthony's answer.
精彩评论