tags:

views:

62

answers:

3

I'm trying to convert a function in C# to VB.Net 2008 and can't seem to make the Lamda 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();
        }
    }
+1  A: 

The lambda is basically just shorthand for an anonymous delegate.

so replace the

(ar)=> {//Do Stuff}

with

Sub(ar)
 'Do stuff
End Sub
Doobi
But it's an input parameter on listener.BeginAcceptSocket so it can't be a sub.
Anthony
+1  A: 

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
Anthony
A: 

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.

Alex Essilfie