I need help converting a c# anonymous method to vb.net

∥☆過路亽.° 提交于 2019-12-29 09:08:29

问题


provider.OptionsSet += delegate
{
  provider.FinishedLoading();
};

回答1:


Nice demonstration how converters get this dramatically wrong, they have for a long time. The += operator isn't VB.NET syntax, AddHandler is required to subscribe events. Where the Do comes from is anybody's guess. The lambda can't be a Function, except for the very rare cases where the delegate type returns a value. Three bugs in one line, you don't stand a chance. You need VS2010 to write a Sub lambda. Like this:

Module Module1
    Sub Main()
        Dim obj As New Test
        AddHandler obj.example, Sub(sender As Object, e As EventArgs)
                                    '' etc...
                                End Sub
    End Sub
End Module

Class Test
    Public Event example As EventHandler
End Class

For earlier versions, you'll need a little non-anonymous helper method. Like this:

    AddHandler obj.example, AddressOf helper
...
Sub helper(ByVal sender As Object, ByVal e As EventArgs)
    '' etc..
End Sub

Human 1, machine 0.




回答2:


Pete Brown has also has an example with inline sub:

http://10rem.net/blog/2010/04/16/tip-anonymous-event-handlers-in-vbnet




回答3:


provider.OptionsSet += Function() Do
    provider.FinishedLoading() 
End Function 

This is taken from http://www.developerfusion.com/tools/convert/csharp-to-vb/, so I haven't tested it. It might be more helpful if you were able to provide more context. What is this being used for?



来源:https://stackoverflow.com/questions/5279477/i-need-help-converting-a-c-sharp-anonymous-method-to-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!