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 
Unknown Coder
  • 6,625
  • 20
  • 79
  • 129

3 Answers3

2

Wherever you initialize your class:

AppServer.ReceiveRequest += ReceiveMessage;
Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
2
public void SomeMethodOrConstructor()
{
  AppServer.ReceiveRequest += ReceiveMessage;
}

public void ReceiveMessage(RemoteRequest rr)
{
  //handle the event here
}
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
1

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
  • 6,095
  • 1
  • 21
  • 22