Clarus Dignus
Clarus Dignus

Reputation: 3897

Is it bad practice to handle multiple event types of the same action?

My problem:

I have .Enter and .TextChanged events that both fire the exact same code. I've combined these events under the one handler. Is this considered bad practice for any reason or in this particular instance, is it considered wise to streamline my code?

Sample Code:

Combined under one handler:

Private Sub TextBox_EnterChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox1.TextChanged, TextBox2.TextChanged
    'Some action.
End Sub

Default separate handlers:

Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter
    'Some action.
End Sub


Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
    'The same action.
End Sub

Upvotes: 1

Views: 338

Answers (1)

Justin Ryan
Justin Ryan

Reputation: 1404

While asking about "bad practice" can sometimes lead to overly large arguments based mostly on opinion, I would have done exactly the same. It's legal, functional, and mostly clear (unless you start handling a huge list of events). I would answer your question directly with another question:

Conversely, what would be the benefit of copy/pasting identical code into multiple methods?

So, no, I would not consider this bad practice.

Upvotes: 1

Related Questions