views:

197

answers:

2

I am developing a Win Forms application using a MVP pattern. I would like to pass the button clicked tag value to the presenter. Because I want to get the button.tag property I need to sender argument to be of type button. How can I do this with out doing this:

private void button0_Click(object sender, EventArgs e) { if (sender is Button) { presenter.CheckLeadingZero(sender as Button); } }

I am having to downcast the object to a button in the method parameter.

Thanks!

+2  A: 

There is no point in checking the type using the is keyword if you're just going to use the as keyword, because as does an is check followed by an explicit cast anyway. Instead, you should do something like this:

Button button = sender as Button;
if (button != null)
{
  presenter.CheckLeadingZero(button);
}
Jeff Yates
A: 

That would work. Either way the cast has to be done.

Thanks.

Nick