Nick
Nick

Reputation: 19664

Override Button_click event handler

I am developing a WinForms application using the 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 the 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.

Upvotes: 0

Views: 1555

Answers (1)

Jeff Yates
Jeff Yates

Reputation: 62377

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);
}

Upvotes: 3

Related Questions