OOP With Visual
OOP With Visual
OBJECT-ORIENTED
PROGRAMMING WITH
VISUAL BASIC.NET
MICHAEL MCMILLAN
Pulaski Technical College, North Little Rock, Arkansas
iii
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-FM CB670-McMillan-v2 January 13, 2004 16:10
C Michael McMillan 2004
Typefaces ITC Berkeley Oldstyle 11/13.5 pt. and ITC Franklin Gothic System LATEX 2ε [TB]
A catalog record for this book is available from the British Library.
iv
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-FM CB670-McMillan-v2 January 13, 2004 16:10
Contents
Preface page ix
Chapter 1
An Overview of the Visual Basic.NET Language 1
NET Programs 1
Data Types and Variables 7
Arithmetic, String, and Relational Operators 15
Summary 42
Exercises 42
Chapter 2
An Overview of Object-Oriented Programming 44
OOP Defined 44
The Characteristics of an OOP Language 46
OOP as an Abstraction Mechanism 52
Abstract Data Types 55
Designing Object-Oriented Programs 59
Summary 62
Exercises 63
Chapter 3
Structures 64
Using Structures 64
v
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-FM CB670-McMillan-v2 January 13, 2004 16:10
vi CONTENTS
Chapter 4
Classes 93
Building a Class 93
Class Constructors 96
Copy Constructors 120
Summary 135
Exercises 135
Chapter 5
Access Modifiers 136
Chapter 6
Abstract Classes and Interfaces 158
Chapter 7
Implementing the IEnumerable and IComparable Interfaces 179
Contents vii
Chapter 8
Designing and Implementing Exception Classes 192
Chapter 9
Design Patterns and Refactoring 202
Chapter 10
Object Internals: Reflection and Attributes 223
Chapter 11
Object Persistence: Serialization 252
viii CONTENTS
Chapter 12
Building a Windows Application 260
Chapter 13
Database Programming Using ADO.NET 277
References 291
Index 293
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
C HAPTER 1
NET PROGRAMS
There are two ways to build programs in VB.NET. One is to use the Vi-
sual Studio.NET Integrated Development Environment (IDE). The other is
to use the command-line compiler packaged as part of the .NET Framework
Software Development Kit (SDK). In this section we’ll discuss developing
programs with the command-line compiler, since this software is free and
can run on any of the modern Windows operating sysems (Windows 98 and
beyond).
1
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
With VB.NET, you can write many different kinds of programs. A VB.NET
program that makes use of a graphical user interface (GUI) is a Windows
application. A VB.NET program that uses the command-prompt console for
input and output is called a Console application. You can also write Internet
applications, Windows Services applications, and other types of applications.
In this book we will focus on Console and Windows applications, though we
will look at examples of Windows Services and Internet (ASP.NET) applica-
tions in the last few chapters.
You do not have to be running Visual Studio.NET to compile and run VB.NET
programs. A command-line compiler is shipped with the .NET Framework
and can be used for any VB.NET programs you want to develop.
To get to the compiler, find the Microsoft.NET subdirectory. It is usu-
ally found in the Winnt or Windows (for Windows 98) directory. Then
change directories to the Framework subdirectory. The compiler resides in
yet another subdirectory. The name of the subdirectory depends on which
version of the .NET Framework you are using. The current .NET Frame-
work version stores the compiler in the v1.0.3705 subdirectory, but be sure
to check this on your own system since your version may be different.
The path to the compiler for a typical computer running Windows 2000
is c:\winnt\Microsoft.NET\Framework\v1.0.3705.
Using the compiler is quite simple. First, create a source file using the text
editor of your choice. Make sure the file you create has a .vb extension. Let’s
look at an example of a simple VB.NET program, a program that displays the
text “Hello, world!” on the screen:
Imports System
Module HelloWorld
Sub Main()
Console.WriteLine("Hello, world!")
End Sub
End Module
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
NET Programs 3
The first line indicates that the program needs to use a class found in the
System namespace. A namespace is a tool used to group related classes and
other types together. Namespaces also allow different classes to share the same
name. Using the keyword Imports allows us to use a class from the specified
namespace (System in this case) without using the namespace name first. We
can just as easily leave the first line of the program out altogether and type in
the fully qualified name of the class:
System.Console.WriteLine("Hello, world!")
Console.WriteLine("Hello, world!")
To display text on the computer’s console, you have to call the Console class
and the proper method for writing text to the console, one of which is the
WriteLine method. This method displays the text passed to it as the argument
on the console and then writes a newline character so that any more text will
be written on the next line.
To end this section, we’ll look at writing the same HelloWorld program
as a class rather than as a module. The codes are similar, and to be honest,
the two techniques are virtually identical. However, because in this book we
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
use classes to define special types, we’ll write all our Console applications as
modules.
Here’s the HelloWorld class code:
Imports System
Class HelloWorld
Shared Sub Main()
Console.WriteLine("Hello, world!")
End Sub
End class
To compile your program (assuming the source file name is test.vb), issue
the following command:
vbc test.vb
If your program compiles successfully, you can simply run the executable file
(test.exe) to run it. If your program has errors in it, the compiler will return
the errors to your console.
One of the surprising things about VB.NET is that you don’t have to use Visual
Studio.NET to build a Windows application. Unlike previous versions of the
language, VB.NET gives the programmer the ability to build a GUI directly
from code. Although you probably won’t want to use this feature all that often,
there will be situations when building a GUI from scratch will be necessary.
This is certainly true if you are using Windows 98 or ME and can’t run Visual
Studio.NET.
To demonstrate how to write a Windows application, we’ll rewrite the
HelloWorld program so that the text is displayed in a label on a form. First,
let’s look at the code:
Imports System
Imports System.Drawing
Imports System.Windows.Forms
NET Programs 5
Controls.Add(lblHelloLabel)
End Sub
End Class
You’ll notice first that there are two new namespaces imported into the pro-
gram. These namespaces are needed for building Windows applications. The
next line is just the definition of the class that holds the program. The follow-
ing line
Inherits Form
tells the compiler that the HelloWorld class is inheriting the Form class, which
is found in the Systems.Windows.Forms namespace. Inheritance is a powerful
technique in object-oriented programming and we will spend at least one
chapter discussing it later in the book.
The next line declares a label for displaying the “Hello, world!” text. Follow-
ing this declaration is the Main subroutine. Be sure to use the Shared modifier
in the heading since we have to use a class for a Windows application.
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
Application.Run(New HelloWorld())
Application.Run(New HelloWorld())
(Note that the command would be all one line when typed, but here it is
broken into two lines for readability.)
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
The first thing you notice is the switch—/reference. We have to add ref-
erences to the different namespaces we use in this program for creating a
Windows application. We didn’t need this switch in the Console application
because the compiler automatically includes the System.dll file. The other
files (including System.dll), though, must be referenced specifically.
The last part of the command tells the compiler to build a Windows appli-
cation (winexe). A Console application is compiled to just an .exe file. If you
look at the file created by the compiler, though, it still displayed as wtest.exe.
The compiler adds data internally to the file to enable it as a Windows appli-
cation.
Now we’re ready to run the program and examine the output (see
Figure 1.1).
VB.NET contains the standard data types for storing numeric, string, char-
acter, and object values, as well as special data types for times, dates, and
monetary values. The primary data types in VB.NET are the following:
Variable Declaration
The reason we use the Dim keyword when declaring a variable dates back to
the early days of the Basic language. In those days, variables did not have to be
declared; they could just pop into existence when needed. Arrays, however,
had to declared first with the dimension of the array. The Dim keyword, then,
identified a variable as an array and not just a plain variable. The use of Dim
has continued through the many different versions of the language right up
to VB.NET.
Multiple variables of the same type can be declared on the same line by
separating each variable with a comma, like this:
Dim num1, num2, num3, num4 As Single
Initializers
Named Constants
There are two ways to perform data type conversions in VB.NET. One way
is to simply let the compiler do it for you. This is the easiest way and the
one that is most likely to lead to both subtle and not-so-subtle errors in your
programs. As an example, let’s look at a simple code fragment that converts a
Single value to an Integer:
Here an Integer variable storing the value 256 is assigned to a Double variable,
so that the value 256.0 is stored in the Double. These types of conversions
are called implicit conversions because the compiler performs the conversion
behind the scenes.
Although implicit conversions are allowed, as just shown, that’s not to say
we should prefer allowing the compiler to make conversions for us. There will
be situations when implicit conversions are made that are not what we want
to happen, leading to logical errors or worse. The VB.NET compiler allows
implicit conversions to take place when the Option Strict switch is off.
This switch tells the compiler whether or not to perform strict type check-
ing. When Option Strict is off, implicit conversions will be performed; when
Option Strict is on, a design-time error is flagged when an implicit conver-
sion is attempted. Most, though certainly not all, programmers consider it
good programming practice to set the Option Strict switch on so that any
conversions that take place must be explicitly performed using a conversion
function.
The Option Strict switch is set by writing either Option Strict On or Option
Strict Off at the beginning of your program. In fact, the statement must precede
any declarations or Imports statements, like this:
Option Strict On
Imports System
Module Module1
Sub Main()
' Code here
End Sub
End Module
One more word of caution on leaving the Option Strict switch off. It can
lead to slower code. A simple example will illustrate the problem:
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
Dim n As Object
Dim names As String
For Each n In NameList
names & = n & ","
Next
In this code, NameList is an ArrayList that holds a list of names. The loop
builds a comma-delimited string of the names in the ArrayList. With Option
Strict off, this code compiles and runs because the compiler will convert each
value of n to a String before appending it to names. And that’s the problem with
leaving Option Strict off. Each conversion will take more time than necessary
because the compiler has to perform a test of the data types and then perform
the conversion. An explicit conversion via a conversion function will speed
this up considerably. In the next section we’ll examine how to perform explicit
type conversions using VB.NET’s type conversion functions.
VB.NET has a full set of built-in conversion functions for performing ex-
plicit type conversions. The following list shows each function and the type
converted to:
r CBool: Boolean
r CByte: Byte
r CChar: Char
r CDate: Date
r CDbl: Double
r CDec: Decimal
r CInt: Integer
r CLng: Long
r CObj: Object
r CSng: Single
r CStr: String
There are many other type conversions you can perform that are not as
intuitive as these. For example, you can convert from a Boolean value to a
String. The Boolean values True and False become “True” and “False” after
the conversion. You can convert an Integer to Boolean—zero converts to False
and a nonzero value converts to True.
Arrays
There are many times when you need to store related values within one vari-
able name. Since regular variables only allow you to store one value in them
at a time, you have to use something else—an array.
An array is a variable that stores multiple values of the same data type. Each
value in an array (also called an element) is indexed by number. Arrays are
created by specifying an array name, the number of elements to store, and the
data type of the elements. The general form for an array declaration is
The compiler automatically sizes the array based on the number of items in
the initialization list. Putting a number inside the parentheses after the array
name will lead to an exception.
Array objects are treated like class instances in VB.NET. There is a set of
methods associated with arrays you can use in your programming. One of the
most useful of these methods is GetUpperBound. This method returns the
last index number (referencing the last element) in an array. You can use this
method when looping through an array, which is demonstrated later in this
chapter when we discuss repetition statements.
There are also array methods that perform tasks that used to take spe-
cially written code to perform, such as sorting an array and reversing an
array. The two methods for these operations are Sort and Reverse. Here’s an
example:
Imports System
Module Array
Sub Main()
Dim names() As String = {"Mike", "Francis", "Ed", _
"Joan", "Terri"}
names.Sort(names)
Dim name As String
For Each name In names
Console.Write(name & " ")
Next
names.Reverse(names)
Console.WriteLine()
For Each name In names
Console.Write(name & " ")
Next
End Sub
End Module
Multi-dimensional Arrays
Arrays are not limited to one dimension. You can create arrays of multiple
dimensions, though it is uncommon to see arrays of more than three dimen-
sions. The most common multidimensional arrays are two-dimensional arrays
that model a table of data.
P1: FCH/FFX P2: FCH/FFX QC: FCH/FFX T1: FCH
CB670-01 CB670-McMillan-v3 January 13, 2004 11:9
For example, the following code declares an Integer array with five rows and
six columns:
Array elements are accessed by referencing their position in the array by index
number. For example, the 0th element of a single-dimensional array named
grades is accessed like this:
current_grade = grades(0)