Help converting a VB.NET "Handles" statement to C#
I need help converting a VB.NET handles statement 开发者_如何转开发to C#. This is the VB
Private Sub ReceiveMessage(ByVal rr As RemoteRequest) Handles AppServer.ReceiveRequest
'Some code in here
End Sub
Wherever you initialize your class:
AppServer.ReceiveRequest += ReceiveMessage;
public void SomeMethodOrConstructor()
{
AppServer.ReceiveRequest += ReceiveMessage;
}
public void ReceiveMessage(RemoteRequest rr)
{
//handle the event here
}
Along with the actual adding of the handler the first time mentioned in the other answers, the Handles statement causes VB to generate a property that will automatically remove the handler from the old value and add it to the new value. If the property never changes, this makes no difference, but if you are ever replacing the "AppServer", you will have to remember to update the event handlers.
精彩评论