C# to VB: Class.Event += (sender, args) => FunctionName(params)

那年仲夏 提交于 2019-12-20 02:58:14

问题


I'm trying to convert the C# code from this webpage to VB.

Everything seems to have converted pretty much fine using an online converter tool, but then I reach the following line:

fadeOutAnimation.Completed += (sender, args) => OnFadeOutAnimationCompleted(d, hostGrid, grid);

The fadeOutAnimation.Completed event produces an event with the signature (sender, args), and d, hostGrid and grid are variables local to the function containing this mysterious event handler assignment.

I think I can see that the instruction on this C# line is telling the code to execute the OnFadeOutAnimationCompleted function, using d, hostgrid and grid as parameters, when fadeoutAnimation.Completed occurs, but I have no idea what to even search for in order to replicate this behaviour in VB.net.

Can someone provide me with some terminology so I can better educate myself on whatever this is called?


回答1:


AddHandler fadeOutAnimation.Completed, Sub() 
    OnFadeOutAnimationCompleted(d, hostGrid, grid)
End Sub

It's been a while, but since you're not using the parameters in the Event Handler I don't think you need to include them (because of Relaxed Delegate Conversion). If so, it'll look more like:

AddHandler fadeOutAnimation.Completed, Sub(sender as object, args as EventArgs) 
    OnFadeOutAnimationCompleted(d, hostGrid, grid)
End Sub



回答2:


This is a lambda expression. Let me see how to do this in VB...

AddHandler fadeOutAnimation.Completed, Sub(sender, e) _
(OnFadeOutAnimationCompleted(d, hostGrid, grid))



回答3:


They keywork you have to look for is "lambda expression".



来源:https://stackoverflow.com/questions/5566702/c-sharp-to-vb-class-event-sender-args-functionnameparams

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