Ayush
Ayush

Reputation: 42440

How can I know which method called event handler

I have two methods:

private void RvListen_OPT()

private void RvListen_FUT()

On a certain event, both call:

void OnRvMessageReceived(object sender, SigRvMessageEventArgs args)

When OnRvMessageReceived is called, how can I check which of the two methods called it? I know it can be done using the object sender, but I'm not sure how to do it.

Upvotes: 1

Views: 634

Answers (3)

Gabe
Gabe

Reputation: 50493

Set sender to a string if you can't change SigRvMessageEventArgs to take an additional property... But the best approach would be to modify SigRvMessageEventArgs if possible.

private void RvListen_OPT()
{
   OnRvMessageReceived("RvListn_OPT()", new SigRvMessageEventArgs())
}

private void RvListen_FUT()
{
   OnRvMessageReceived("RvListn_FUT()", new SigRvMessageEventArgs())
}

void OnRvMessageReceived(object sender, SigRvMessageEventArgs args)
{
   if(sender.ToString() == "RvListn_OPT()"){
      // do work
   }
   else if(sender.ToString() == "RvListn_FUT()"){
      // do work
   }
}

Upvotes: 1

Bryce Fischer
Bryce Fischer

Reputation: 5442

sender will (usually) give you the object that called the event. It may not because its up to the caller to actually set this.

That said, I"m not sure it should matter. If the call depends on who called it, maybe they need to be setup as separate events... Or, as Jackson mentioned, the args variable could be set to allow the OnRvMessageReceived event can respond to that.

Upvotes: 1

Jackson Pope
Jackson Pope

Reputation: 14640

If you can edit the SigRvMessageEventArgs class you could add a field to it which you set differently in the two calls.

Upvotes: 3

Related Questions