Visual Basic.NET Language Enhancements: Inheritance
Author: Intelligent Solutions Inc.
Version Compatibility: Visual Basic.NET
Instructions: Copy the declarations and code below and paste directly into your VB project.
Inheritance Inheritance allows you to re-use code from a parent (or base) class in a child class. For instance, suppose you have a class called clsValidate that contains generic validation routines. One of its functions is called IsAlphanumeric. It takes a string as a parameter and returns true if all the characters in the string are either numbers or letters. Now suppose you have a second class called clsMyValidate that also contains validation routines but they are more specific to your particular business. You also will need generic validation functions in this class, so you want to include all methods and properties from clsValidate in your clsMyValidate class. In VB5 and VB6 you would have to create a new instance of clsValidate in order to use its properties and methods in your clsMyValidate class. From a conceptual standpoint, this is awkward since both classes are designed for validation. In Visual Basic.NET, all you will have to do is insert the following line at the top of clsMyValidate: Inherits clsValidate Now, clsMyValidate will contain all the methods and properties of clsValidate, including IsAlphanumeric, which will provide the same functionality in clsMyValidate as it does in clsValidate. Some experts don't think that using inheritance in this manner is good programming practice. They argue that someone could come along and change the base class and muck up all the child classes that inherit from it. But others (including myself) believe that, if used properly, it is a useful tool in promoting code reuse and designing complex systems. A demo of inheritance in VB.NET can be found at http://www.freevbcode.com/ShowCode.Asp?ID=2054. The rest of the series: Part II, Polymorphism Part III, Overloading Part IV, Constructors Part V, Free Threading