CH 01
CH 01
Object-Oriented
Programming
with Visual Basic .NET
J.P. Hamilton
Object-Oriented Programming
with Visual Basic .NET
J.P. Hamilton
2 | Chapter 1: Introduction
Abstraction
A radio has a tuner, an antenna, a volume control, and an on/off switch. To use it,
you don’t need to know that the antenna captures radio frequency signals, converts
them to electrical signals, and then boosts their strength via a high-frequency amplifi-
cation circuit. Nor do you need to know how the resulting current is filtered,
boosted, and finally converted into sound. You merely turn on the radio, tune in the
desired station, and listen. The intrinsic details are invisible. This feature is great
because now everyone can use a radio, not just people with technical know-how.
Hiring a consultant to come to your home every time you wanted to listen to the
radio would become awfully expensive. In other words, you can say that the radio is
an object that was designed to hide its complexity.
If you write a piece of software to track payroll information, you would probably
want to create an Employee object. People come in all shapes, sizes, and colors. They
have different backgrounds, enjoy different hobbies, and have a multitude of beliefs.
But perhaps, in terms of the payroll application, an employee is just a name, a rank,
and a serial number, while the other qualities are not relevant to the application.
Determining what something is, in terms of software, is abstraction.
In object-oriented software, complexity is managed by using abstraction. Abstrac-
tion is a process that involves identifying the crucial behavior of an object and elimi-
nating irrelevant and tedious details. A well thought-out abstraction is usually
simple, slanted toward the perspective of the user (the developer using your objects),
and has probably gone through several iterations. Rarely is the initial attempt at an
abstraction the best choice.
Remember that the abstraction process is context sensitive. In an application that
will play music, the radio abstraction will be completely different from the radio
abstraction in a program designed to teach basic electronics. The internal details of
the latter would be much more important than the former.
Inheritance
Inheritance is the ability to define a new class that inherits the behaviors (and code)
of an existing class. The new class is called a child or derived class, while the original
class is often referred to as the parent or base class.
Inheritance is used to express “is-a” or “kind-of” relationships. A car is a vehicle. A
boat is a vehicle. A submarine is a vehicle. In OOP, the Vehicle base class would pro-
vide the common behaviors of all types of vehicles and perhaps delineate behaviors
all vehicles must support. The particular subclasses (i.e., derived classes) of vehicles
would implement behaviors specific to that type of vehicle. The main concepts
behind inheritance are extensibility and code reuse.
4 | Chapter 1: Introduction
Polymorphism
Polymorphism refers to the ability to assume different forms. In OOP, it indicates a
language’s ability to handle objects differently based on their runtime type.
When objects communicate with one another, we say that they send and receive mes-
sages. The advantage of polymorphism is that the sender of a message doesn’t need
to know which class the receiver is a member of. It can be any arbitrary class. The
sending object only needs to be aware that the receiving object can perform a partic-
ular behavior.
A classic example of polymorphism can be demonstrated with geometric shapes.
Suppose we have a Triangle, a Square, and a Circle. Each class is a Shape and each
has a method named Draw that is responsible for rendering the Shape to the screen.
With polymorphism, you can write a method that takes a Shape object or an array of
Shape objects as a parameter (as opposed to a specific kind of Shape). We can pass
Triangles, Circles, and Squares to these methods without any problems, because
referring to a class through its parent is perfectly legal. In this instance, the receiver is
only aware that it is getting a Shape that has a method named Draw, but it is ignorant
of the specific kind of Shape. If the Shape were a Triangle, then Triangle’s version of
Draw would be called. If it were a Square, then Square’s version would be called, and
so on.
6 | Chapter 1: Introduction
The term .NET means many things to many different people. When
the term is used in this book, it always refers to the .NET Frame-
work—the Common Language Runtime and the .NET class library.
In the past, passing a string from a component written in VB to one written in C++
(or vice versa) could be frustrating. Strings in VB weren’t the same as the strings in
C++. In fact, under some circumstances, using a component written in C++ from VB
was downright impossible because of issues involving data types. VB just doesn’t
know what to do with an LPSTR! Every language under .NET uses the same data
types defined in the base class library, so interoperability problems of the past are no
longer an issue.
This book touches on several major areas of the library and focuses on the develop-
ment of components using VB.NET. However, if you follow the examples, you might
be surprised at just how much you know.
8 | Chapter 1: Introduction
A First-Class Citizen
VB has always been easy to learn, but the power of simplicity came with a price. The
language itself has never gotten the respect it deserves because it always hid so much
from the developer; getting under the hood required a sledgehammer. This is no
longer true. While VB is still a great language and is relatively painless to learn and
use, you are no longer restricted in how “low you can go.”
One of the most important concepts behind .NET is that all languages are on a level
playing field; the choice of language should be determined more by your style than
anything else. This is probably the reason why you prefer VB over other languages:
you like the syntax of Visual Basic and appreciate its simplicity. No longer is choice
of language a concern, because VB.NET is just as fast as C# and it does a few things,
such as event declaration and conditional exception handling, better. But for the
most part, any language that runs under .NET will provide you with the tools to
develop cutting edge software. Thus, it truly is a matter of style. VB.NET is no more
or no less of a language than any other in the .NET Framework.