Help convert VB.NET Handles instruction to C #

I need help converting a VB.NET instruction to C #. This is vb

Private Sub ReceiveMessage(ByVal rr As RemoteRequest) Handles AppServer.ReceiveRequest 

'Some code in here

End Sub 
+3
source share
3 answers

Wherever you initialize your class:

AppServer.ReceiveRequest += ReceiveMessage;
+2
source
public void SomeMethodOrConstructor()
{
  AppServer.ReceiveRequest += ReceiveMessage;
}

public void ReceiveMessage(RemoteRequest rr)
{
  //handle the event here
}
+2
source

Along with the actual addition of a handler, first mentioned in other answers, the Handles statement causes VB to generate a property that automatically removes the handler from the old value and adds it to the new value. If the property never changes, it does not matter, but if you ever replace "AppServer", you will have to remember to update the event handlers.

+1
source

Source: https://habr.com/ru/post/1740855/


All Articles