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.
senderis not going to bebuttonStart_Click, it will simply be the button. So you can test for it.However, if you find yourself going down this route, you may end up seeing multiple
ifblocks each with different behaviors depending on the identity ofsender. 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 ofif / else if / else if / ....