Anthony
Anthony

Reputation: 58

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

Upvotes: 2

Views: 570

Answers (3)

Alex Essilfie
Alex Essilfie

Reputation: 12613

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.

Upvotes: 0

Anthony
Anthony

Reputation: 58

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

Upvotes: 1

Doobi
Doobi

Reputation: 4842

The lambda is basically just shorthand for an anonymous delegate.

so replace the

(ar)=> {//Do Stuff}

with

Sub(ar)
 'Do stuff
End Sub

Upvotes: 1

Related Questions