Vb. NET Override event handlers

Source: Internet
Author: User
Tags inheritance integer
Program VB. NET Override event handlers

Introduced
In the inheritance of a class or control, a subclass or child control encapsulates all the functions of the parent class, including event handlers. This article will focus on how to treat the inheritance of event handlers correctly in Visual Basic. NET application design.

Reader requirements
This paper assumes that readers are familiar with object-oriented programming and inheritance of classes. If you are not familiar with them, see Component Polymorphism ("Polymorphism in Components").

Inheriting an event handler
When a component is inherited, all its members are encapsulated into subclasses. An event handler is a method that responds to a particular event, is one of the component members, and is therefore inherited. Take a look at a typical event handler:

Private Sub button1_click (ByVal sender as System.Object, ByVal E As _
System.EventArgs) Handles Button1.Click
Static Counter as Integer = 0
Counter + 1
MessageBox.Show ("This button has been clicked" & _
Counter.tostring () & "Times.")
End Sub

We see that the Handles clause at the end of the declaration associates the handler with a specific event. Specifically, the code above will run when the Button1.Click event occurs. This is the typical syntax for defining an event handler.

To implement override, you must use the Overridable keyword and redefine its visibility as Protected, Protected Friend, or public. Here's how to override event handlers:

Protected Overridable Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
Static Counter as Integer = 0
Counter + 1
MessageBox.Show ("This button has been clicked" & _
Counter.tostring () & "Times.")
End Sub

To inherit an event handler in an assembly

The override of an event handler is very similar to the override of other methods, but one has to mention that the Handles clause must be dropped when the event handler is override.

How to override an event handler for a component

1 Add the Overrides keyword in the declaration section of the event handler

Please note: Do not attach Handles clauses. Because the handler for the parent class already has a specific event associated with it, the subclass inherits all of this. In other words, the event associated with the parent class can activate the handler for the subclass, so the Handles clause here is superfluous.
The following example shows how to override the preceding event handler:

Protected Overrides Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs)
Static Counter as Integer = 0
Counter + 1
MessageBox.Show ("This inherited button has been clicked" & _
Counter.tostring () & "Times.")
End Sub

2 Why do not need Handles clause

As mentioned earlier, the Handles clause that we did not use associated events is not an omission, but is determined by the event handling mechanism of the. NET Framework. The Handles clause in the parent class associates an event handler to a specific event, which inherits the quilt class. Therefore, even if there is no Handles clause in the subclass, the event associated with the parent class can also activate the event handler for the subclass. If you add the Handles clause at this point, the handler is again associated to the event. The consequence of a duplicate association is that the handler will be activated two consecutive times. For example:

' Wrong code
Protected Overrides Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
Static Counter as Integer = 0
' In this case, the Handles clause is used,
' So whenever the button Button1 is clicked once,
' Variable Counter will be added two times;
Counter + 1
' Message box will pop up two times,
' The displayed content will also violate the design intent
MessageBox.Show ("This inherited button has been clicked" & _
Counter.tostring () & "Times.")
End Sub

Conclusion
In Visual Basic. NET, the override of event handlers can cause bugs that are difficult to debug. For example, when you use the Handles clause to associate an event handler, you need to be extra careful to avoid duplicate associations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.