Inheritance and Windows Forms - Carrying Out the Scenario
(Page 4 of 5 )
Well, here is the scenario that I want to carry out:
1) I want to add a manager.
2) I want to validate the user inputs.
3) If the user inputs are valid, then I want to add the manager.
The same scenario above should be valid for the technicians.
The most logical way to carry out the previous scenario for the manager is to validate the inputs when the user clicks the “Add” (btnAdd). But if we handle it on the “ManageEmployees” form, then how should I determine if what I am adding is a Manager or a Technician? One of the answers will be to put an enum on the form and set it to the type of employees I am working with.
Shouldn’t I check all the time about this enum, no matter what I do? We will thank the “ManageEmployees” form for giving us the common functionality and our “ManageManagers” form should be taking over from now on.
But before we take over, we will be asking our nice “ManageEmployees” form a final request, that please, when you receive the click event of button “btnAdd” we want to handle that event.
So, if you double click on the “btnAdd” on the “ManageEmployees” from the editor will open to this sub.
Private Sub btnAdd_Click
(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnAdd.Click
End Sub
As you can tell, the “btnAdd_Click” is private, and according to the Object Oriented principles, this means that the sub will be only available to the class itself.
Should we stop here (for sure not!!) we will do a little change on this sub to look just like this:
Protected Overridable Sub btnAdd_Click
(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
End Sub
Now the sub looks much better. It is not only available to the “ManageEmployees” form only, but also to all the forms that are inheriting from the “ManageManagers” and “ManageTechnicians” (yes you are right, it is Protected). Above all, it is ready to be taken control off by the inheriting classes (it is Overridable).
So let’s get back to our “ManageMangers” form, and let’s override the “BtnAdd_Click” sub. Let’s add the following to the “ManageManagers”:
Protected Overrides Sub btnAdd_Click
(ByVal sender
As System.Object, ByVal e As System.EventArgs)
'Handles btnAdd.Click
End Sub
Next: Explanation >>
More Visual Basic.NET Articles
More By Mohammed Qattan