One Step Further: Inheriting a Base Class


One Step Further: Inheriting a Base Class

As promised at the beginning of this chapter, I have one more trick to show you regarding user-defined classes and inheritance. Just as forms can inherit form classes, they can also inherit classes that you've defined by using the Add Class command and a class module. The mechanism for inheriting a base (parent) class is to use the Inherits statement to include the previously defined class in a new class. You can then add additional properties or methods to the derived (child) class to distinguish it from the base class.

In the following exercise, you'll modify the My Person Class project so that it stores information about new teachers and the grades they teach. First you'll add a second user-defined class, called Teacher, to the Person class module. This new class will inherit the FirstName property, the LastName property, and the Age method from the Person class and will add an additional property named Grade to store the grade in which the new teacher teaches.

Use the Inherits keyword

  1. Click the Person.vb class in Solution Explorer, and then click the View Code button.

  2. Scroll to the bottom of the Code Editor so that the insertion point is below the End Class statement.

    As I mentioned earlier, you can include more than one class in a class module, as long as each class is delimited by Public Class and End Class statements. You'll create a class named Teacher in this class module, and you'll use the Inherits keyword to incorporate the method and properties you defined in the Person class.

  3. Type the following class definition in the Code Editor. (Type the statements formatted in bold italic below—Visual Studio adds the remaining statements automatically.)

     Public Class Teacher     Inherits Person     Private Level As Short     Public Property Grade() As Short         Get              Return Level         End Get          Set(ByVal value As Short)              Level = value         End Set      End Property  End Class

    The Inherits statement links the Person class to this new class, incorporating all of its variables, properties, and methods. If the Person class were located in a separate module or project, you could identify its location by using a namespace designation, just as you identify classes when you use the Imports statement at the top of a program that uses classes in the .NET Framework class libraries. Basically, I've defined the Teacher class as a special type of Person class—in addition to the FirstName and LastName properties, the Teacher class has a Grade property that records the level at which the teacher teaches.

    Now you'll use the new class in the Button1_Click event procedure.

  4. Display the Button1_Click event procedure in Form1.

    Rather than create a new variable to hold the Teacher class, I'll just use the Employee variable as it is—the only difference will be that I can now set a Grade property for the new employee.

  5. Modify the Button1_Click event procedure as follows. (You need to change the bold italic lines.)

    Dim Employee As New Teacher  Dim DOB As Date    Employee.FirstName = TextBox1.Text  Employee.LastName = TextBox2.Text  DOB = DateTimePicker1.Value.Date Employee.Grade = InputBox("What grade do you teach?") MsgBox(Employee.FirstName & " " & Employee.LastName _   & " teaches grade " & Employee.Grade)

    In this example, I've removed the current age calculation—the Age method isn't used—but I did this only to keep information to a minimum in the message box. When you define properties and methods in a class, you aren't required to use them in the program code.

    Now you'll run the program.

    TIP
    The revised Person Class program is located in the c:\vb05sbs\chap16\person class folder.

  6. Click the Start Debugging button to run the program.

    The new employee form appears on the screen.

  7. Type your first name in the First Name text box and your last name in the Last Name text box.

  8. Click the date time picker object, and scroll to your birth date.

  9. Click the Display Record button.

    Your program stores the first name and last name values in property settings and then displays the following input box, which prompts the new teacher for the grade he or she teaches:

    graphic

  10. Type 3, and then click OK to close the input box.

    The application stores the number 3 in the new Grade property and uses the FirstName, LastName, and Grade properties to display the new employee information in a confirming message box. You see this message:

    graphic

  11. Experiment with a few more values if you like, and then click the Close button on the form.

    The program stops, and the development environment returns. You're finished working with classes and inheritance in this chapter. Nice job!

Further Experiments with Object-Oriented Programming

If you've enjoyed this foray into object-oriented coding techniques, more fun awaits you in Visual Basic 2005, a truly object-oriented programming language. In particular, you might want to add events to your class definitions, create default property values, and experiment with a polymorphic feature called method overloading. These and other OOP features can be explored by using the Visual Studio online Help or by perusing an advanced book on Visual Basic programming. (See Appendix A, “Where to Go for More Information,” for a reading list.)



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net