NibblyPig
NibblyPig

Reputation: 52922

C# to VB - How do I convert this anonymous method / lambda expression across?

How would you convert this to VB (using .NET 4.0 / VS2010) ?

bw.DoWork += (o, args) =>
{
     Code Here
};

I thought maybe like this:

AddHandler bw.DoWork,
    Function(o, args)

        Code Here

    End Function

But it says Function does not return a value on all code paths.

Ideas?

Upvotes: 3

Views: 2169

Answers (6)

nepaluz
nepaluz

Reputation: 109

I tried the Sub way using .NET 4.0 and do not get any errors

AddHandler bw.DoWork,
    Sub(sender As Object, e As System.ComponentModel.DoWorkEventArgs)

        'Code(Here)

    End Sub

Upvotes: 2

Will Marcouiller
Will Marcouiller

Reputation: 24132

I met something alike when trying to use lambda expressions in VBNET2008.

As the keyword Function is used to indicate to the compiler that a lambda expression is used, the expression must return a value. In this scenario, the use of the keyword Sub would be more appropriate, but it is not possible yet in VBNET2008.

VBNET2010 should address this by allowing Subs within lambda expressions.

Here's an example of workaround that worked great for me at this question on SO: What would be the equivalent VB.NET code for this C#...

In short, instead of writing your lambda expression into the core of your method, you need to point the Sub you wish to perform the work with using the AddressOf Sub.

David Parvin did also write the workaround exactly as you need it.

EDIT:

You could always opt for the delegate solution. Private Delegate Sub MyMethodAsync()

Public Sub Button1_Click(...) Handles Button1.Click
    Dim myMethodAsync As MyMethodAsync = AddressOf MyDoWorkAsync

    _myBackgroundWorker.RunWorkerAsync(myMethodAsync)
    Dim loadingForm = New LoadingForm()
    loadingForm.ShowDialog()
End Sub

Private Sub _myBackgroundWorker_DoWork([parameters here...]) Handles _myBackgroundWorker.DoWork
    ' Do some stuff...
End Sub

I know it ain't anonymous method, thus you might have a real hard time trying to work this way in VBNET, even 2008. Such features are supposed to be part of VBNET2010, but I think they cut it out, but I'm really not sure enough to confirm it firmly.

Hope this helps!

Upvotes: 0

David Parvin
David Parvin

Reputation: 927

You can't make it that way, but you can make it work like:

  Sub New()    
    AddHandler bw.DoWork, AddressOf Stuff    
  End Sub

  Sub Stuff(ByVal o, ByVal args)    
    ' Code Here '   
  End Sub

You are basically adding an event that calls your code and no event returns a value except through the parameters passed into the routine. That makes it a Sub not a Function.

Upvotes: 3

Arnis Lapsa
Arnis Lapsa

Reputation: 47567

VB.NET lacks this

Interesting bits:

(..)VB does not have anonymous methods, only Lambda expressions (no way to declare an anonymous Action delegate).

Upvotes: 3

BlackICE
BlackICE

Reputation: 8916

You don't get multiline lambda's until 2010. If you have to convert multiline in VB9, then you have to do some gyrations, but it can be done. You end up having to create a holder object, set properties on it, then call a shared function, not nearly as elegant as it is in C#

Upvotes: 0

Nick
Nick

Reputation: 5945

The problem is that we need to know what "Code Here" does. A Function has to return a value (have a Return Statement). If you don't want to return a value, then your Function needs to be a Sub.

Upvotes: 1

Related Questions