Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

400 Part III Designing the User Interface


The Button3_Click event procedure that you added is also a member of the new class.
But recall for a moment that the Form1 class itself relied on the System.Windows.Forms.Form
class for its fundamental behavior and characteristics. So the last exercise demonstrates
that one derived class (Form2) can inherit its functionality from another derived class
(Form1), which in turn inherited its core functionality from an original base class (Form),
which is a member of the System.Windows.Forms namespace in the Microsoft .NET
Framework.

In addition to the Inheritance Picker, Visual Studio offers the Inherits statement, which
causes the current class to inherit the properties, procedures, and variables of another
class. To use the Inherits statement to inherit a form, you must place the Inherits statement
at the top of the form as the first statement in the class. Although you might choose to use
the Inheritance Picker for this sort of work with forms, it is useful to know about Inherits
because it can be used for classes and interfaces other than forms, and you will probably
run into it now and then in your colleagues’ program code. You’ll see an example of the
Inherits statement near the end of this chapter.

Recognizing that classes are such a fundamental building block in Visual Basic programs,
you might very well ask how new classes are created and how these new classes might
be inherited down the road by subsequently derived classes. To ponder these possibilities,
I’ll devote the remainder of this chapter to discussing the syntax for creating classes in
Visual Basic 2010 and introducing how these user-defined classes might be inherited later
by still more classes. Along the way, you’ll learn how very useful creating your own classes
can be.

Nerd Alert
There’s a potential danger for terminology overload when discussing class creation
and inheritance. A number of very smart computer scientists have been thinking about
these OOP concepts for several years, and there are numerous terms and definitions
in use for the concepts that I plan to cover. However, if you stick with me, you’ll find
that creating classes and inheriting them is quite simple in Visual Basic 2010 and that
you can accomplish a lot of useful work by adding just a few lines of program code to
your projects. Understanding OOP terminology will also help you make sense of some
of the advanced features of Visual Basic 2010, such as covariance and contravariance,
Language Integrated Query (LINQ), anonymous types, extension methods, and
lambda expressions, which facilitate the use of classes, objects, and methods, and are
sometimes emphasized in marketing announcements and new feature lists.
Free download pdf