tags:

views:

120

answers:

3

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 
+1  A: 

Wherever you initialize your class:

AppServer.ReceiveRequest += ReceiveMessage;
Zach Johnson
or `AppServer.ReceiveRequest +=` and then press TAB if you are in VS ;-)
Paul Kohler
+1  A: 
public void SomeMethodOrConstructor()
{
  AppServer.ReceiveRequest += ReceiveMessage;
}

public void ReceiveMessage(RemoteRequest rr)
{
  //handle the event here
}
Derick Bailey
I appreciate the answer, very clean
Jim Beam
+1  A: 

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.

Gideon Engelberth