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

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 he says that the function does not return a value to all code paths.

Ideas?

+3
source share
6 answers

VB.NET is missing this

Interesting bits:

(..) VB has no anonymous methods, only lambda expressions (there is no way to declare an anonymous Action delegate).

+3
source

You cannot do it this 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

, , , , . Sub .

+3

Sub way .NET 4.0

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

        'Code(Here)

    End Sub
+2

, , " ". ( ). , Sub.

+1

2010 . VB9, , . -, , , , #

0

- - VBNET2008.

Function , , . Sub , VBNET2008.

VBNET2010 , Subs -.

, SO: VB.NET #...

, , - ​​ , Sub, , SubOp AddressOf.

, .

EDIT:

.      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

, , , VBNET, 2008 . VBNET2010, , , , .

, !

0
source

Source: https://habr.com/ru/post/1734088/


All Articles