0

Imagine a method like this ( in Win Forms):

//First method
private void buttonStart_Click(object sender, EventArgs e)
{
       //I call another method here
       this.GetData(sender, null)
}

//Second method
private void GetData(object sender, EventArgs e)
{
       //how to check IF calling method is buttonStart_Click ???
       if(sender.Equals == buttonStart_Click) 
       {
            //DO BLAH BLAH
       }
}

I hope I was clear, that is I want to know which method is calling 'GetData'. note I know I can have a global variable and set it to something, but I want to know if there is a DIRECT way to do this?

Thanks.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Dumbo
  • 13,555
  • 54
  • 184
  • 288

3 Answers3

1

sender is not going to be buttonStart_Click, it will simply be the button. So you can test for it.

if (sender != null && sender.Equals(buttonStart))
{
   // work with this information
}

However, if you find yourself going down this route, you may end up seeing multiple if blocks each with different behaviors depending on the identity of sender. If that is the case, you'd be better served with a different approach. Have a different handler for each event, encapsulate the differing logic via a delegate, etc. Do not end up with a page full of if / else if / else if / ....

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • Thanks, would you mind giving a delegate example for this case? – Dumbo Apr 19 '11 at 14:14
  • @Sean, nothing that isn't purely contrived. The point I'm making is to be sure about your approach and investigate alternatives. You want to avoid ending up with a kitchen-sink method that is doing way too much and making decisions that are only tangentially related. Maybe all you need to do is pass in a parameter, maybe `buttonStart_Click` actually needs to do work rather than letting `GetData` handle everything, or maybe `buttonStart_Click` needs to give `GetData` the means to do what it needs to do without `GetData` knowing what those means are (e.g, a delegate). – Anthony Pegram Apr 19 '11 at 14:18
  • (And this delegate could in fact be an `Action<>` or `Func<>`.) Here's a suggestion. Continue working on whatever you're doing. Once you have more code, perhaps ask a new question about how you can refactor it into something more manageable (if warranted). – Anthony Pegram Apr 19 '11 at 14:19
0

If you have to do something different because you called the method from some other method, it's probably best to just call a different method:

//First method
private void buttonStart_Click(object sender, EventArgs e)
{
       //I call another method here
       this.SpecialGetData(sender, null)
}

//Second method
private void GetData(object sender, EventArgs e)
{
     // Do regular stuff
}

//Special second method
private void SpecialGetData(object sender, EventArgs e)
{
    //DO BLAH BLAH
}
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
0

Not sure why you do this way, but if you need... http://www.csharp411.com/c-get-calling-method/

VikciaR
  • 3,324
  • 20
  • 32
  • And please that into consideration the following sentence from the documentation: "StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization." – R. Martinho Fernandes Apr 19 '11 at 13:51