Вы находитесь на странице: 1из 10

03 572x CH01 11/29/01 2:13 PM Page 11

CHAPTER
1
Introduction to Visual Basic .NET
In this chapter
Understanding Programming and Visual Basic’s Role 12
The Importance of Application Design 14
Tips for a Good Design 15
Big Changes in Visual Basic .NET 18
From Here . . . 19
ISBN: 0-558-13856-X

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 12

12 Chapter 1 Introduction to Visual Basic .NET

Thank you for deciding to make this book part of your library. We hope that it will serve as
your guide to the exciting new world of Microsoft Visual Basic .NET.

Understanding Programming and


Visual Basic’s Role
Before we jump into the technical details of Visual Basic .NET, we would like to answer
some common questions about computer programming in general, and how Visual Basic fits
into the overall picture. We also want to get you thinking about some important concepts
that apply to computer programming in any language.

What Is a Computer Program?


A computer program (also known as an application) is a series of instructions that enables a
computer to perform a specific task. A programming language such as Visual Basic is used to
translate instructions, as we humans understand them, into the steps that the computer can
comprehend and follow. A computer program can be designed to perform a single, basic
task, such as calculating conversions between different units of measure or keeping track of
your grocery list. Other computer programs may be far more complex and perform very
specialized tasks, such as tracking the satellites orbiting Earth at any given moment.
Traditionally, computer programs have fallen into two very broad categories—packaged
and custom.
Packaged programs are those that you can purchase in a software store, via mail order, direct
from a manufacturer, and so on. Packaged programs (sometimes known as canned programs)
are predesigned to accomplish one or more specific tasks. For example, you may purchase
Microsoft Word to meet your word processing needs, Symantec’s WinFax Pro to enable
your computer to send and receive faxes, or McAfee’s VirusScan to help prevent viruses
from attacking your system. You also may be interested in game programs such as Maxis’
The Sims or Microsoft’s Flight Simulator. All these programs are among the thousands
upon thousands of software packages that are available for Windows-based personal com-
puters (PCs).
Custom programs (also known as proprietary programs) are usually designed for a specific pur-
pose within a particular organization. For example, a company may need an application that
tracks product orders from the time they are placed until they are actually shipped. If there
is no packaged program available that meets the company’s specific needs, a custom program
could be developed. One advantage of a custom program in this case is that the program can
be continually modified as the company’s needs change—packaged programs generally can’t
be modified by the user.
Over the past few years, as use of the Internet has exploded, a new category of software is
emerging. This new category of software, known as distributed applications, utilizes the Internet
ISBN: 0-558-13856-X

(or other large-scale connectivity, such as a local intranet) to allow several different layers of an
application to run at different locations. Application Service Providers (ASPs) allow companies

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 13

Understanding Programming and Visual Basic’s Role 13

to access programs and store data on their servers. Thanks to the Internet, these services are
accessible to nearly everyone. Visual Basic .NET, as part of the larger Visual Studio .NET Part
infrastructure, is Microsoft’s attempt to bring these types of applications within the domain of
the VB programmer. For example, you can create a Web service with Visual Basic .NET and
I
Ch
provide an enormous amount of functionality to distributed applications.
Many distributed applications are known as multi-tier, or n-tier, applications. This term 1
relates to the distribution of the processing power required to execute the program. A multi-
tier application involves several “layers” of computers, each of which is responsible for pro-
viding a specific portion of the processing that creates or responds to the end user’s
experience. For example, a user may access a Web-based e-commerce application such as
eBay or Amazon.com through his Web browser; in this case, his computer provides the
front-end interface. In turn, his Web browser connects to other computers that generate
catalog pages, perform product searches, and manage order entry and credit card processing.
Typically, in addition to the client’s PC, one or more Web servers, database servers, and
other applications may be involved. In a case such as this, several layers (tiers) of processing
provide the overall experience to the user.

A Brief Programming Language Primer


When you get down to a computer’s most basic level, the microprocessor that is at the core
of the computer’s functionality doesn’t understand anything other than numeric instructions.
To make matters worse, the only instructions that the processor can understand are incredi-
bly simplistic commands, most of which have to do with moving numbers around between
memory locations. These commands that the processor understands are known as machine
language, or the most basic language that the machine (the PC) can use.
Machine language is known as a low-level language, because it’s all the way down at the
processor’s level of understanding. As you might imagine, writing programs in machine lan-
guage is an incredibly daunting task. Fortunately, you don’t have to get down to that level to
create computer programs. Several higher-level programming languages have been devel-
oped to enable us to write programs. These programming languages enable programmers to
write instructions in something resembling English; the instructions are then converted into
a program containing machine-language instructions that the processor can understand.
Some examples of programming languages that have developed over the years include
ForTRAN and COBOL, which are generally used with mainframe computers, as well as
BASIC, Pascal, C, and C++, which are commonly used to write programs at the personal
computer level.

The Evolution of Visual Basic


Visual Basic is a descendant of BASIC, which has been around for several decades. BASIC
(an acronym for Beginners’ All-Purpose Symbolic Instruction Code) was originally developed at
Dartmouth University in 1964 as a language for beginning programmers. BASIC was often
ISBN: 0-558-13856-X

the first language that programmers learned to become familiar with programming basics
before moving on to more powerful languages.

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 14

14 Chapter 1 Introduction to Visual Basic .NET

However, Visual Basic is much more significant than just an upgrade to BASIC. To under-
stand why, you only need to understand the changes that Windows brought to the comput-
ing world. Most readers are familiar with the Windows operating system, which is what
made Bill Gates rich and famous. The core idea (which was not invented by Gates but by
Xerox) is a graphical operating system using metaphors for computer concepts that the aver-
age person can understand. For example, computer files are represented as graphical icons
stored in file folders on a desktop the user can view. Each open window has certain expected
behavior, such as the ability to scroll or change positions. Although we pretty much take it
for granted today, Windows made computers much more intuitive to use and helped bring
personal computing to the masses.
As easy as it was to use Windows, writing programs for it in a traditional language could be
a major headache. One common test of a programming language is to print the text “Hello
World” on the screen. Prior to Windows, this could be accomplished in most languages
with just one or two lines of code. However, an early Windows programming book (for the
C language) described several screens of code (more than 80 lines) just to create a “Hello
World” window. This extra code was required to create a window that interacted and
behaved appropriately with the operating system.
Visual Basic, which has evolved into an extremely powerful application development tool,
takes a lot of complexity out of programming for the Windows environment. Rather than
write code to draw a window or process operating system events, you can graphically design
windows using the mouse. Since its introduction, Visual Basic has gradually left its reputa-
tion as a beginners’ language far behind and become one of the most popular—and
powerful—Windows programming languages. The latest version of Visual Basic, Visual
Basic .NET, taps into the world of distributed Web applications and represents perhaps
Visual Basic’s most ambitious leap forward.
One very nice feature of Visual Basic is that you can use it to create a solid application very
quickly. As you’ll see throughout this book, Visual Basic makes short work of what would
normally be very time-consuming programming tasks. This frees up the programmer to
spend his time developing the application’s functionality, rather than spending time on mun-
dane, repetitive programming tasks. Visual Basic is often referred to as a Rapid Application
Development (RAD) tool.

The Importance of Application Design


A college English professor of mine was once describing different types of novelists: “A tra-
ditionalist author usually orders his story beginning-middle-end, a modernist might reverse
that order, and a post-modernist would only include two of the three parts.”
Unfortunately, authors of computer programs don’t (usually) have the luxury of such flexi-
bility. Although it is true that with a visual programming system such as Visual Basic it is
easy to just sit down and start hacking out code, with most computer programs this is not a
ISBN: 0-558-13856-X

good idea! Like anything in life, a program that is built on a shaky foundation can be

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 15

Tips for a Good Design 15

plagued with problems. Your program’s purpose is to solve a specific problem, and the
design determines how it will solve that problem. It is very important to spend time on Part
design before you start coding. Investing a significant amount of time in program design will
benefit both you and the users of your programs.
I
Ch

How Design Fits into the Programming Process 1


The life of a computer program generally begins when an end user asks someone to create
it. From the end user’s requirements, the programmer must determine how the program will
accomplish the intended goal. Although the purpose of this book is to teach you the techni-
cal aspects of creating programs with Visual Basic, we would be remiss if we did not men-
tion another important step: the design of the program.
Regardless of the programming language used, the following types of questions must be
addressed when designing your programs:
■ What are the specific tasks the program needs to perform?
■ Who are the users of this program?
■ What will the user interface (screens) look like?
■ What type of architecture should I use (Web-based, client-server)?
■ What will the databases look like?

The ideas presented in this section are by no means all-inclusive, but rather one set of gen-
eral guidelines. Your technique, of course, may be totally different.

Tips for a Good Design


How do I come up with a good design and how do I know it is good? There is no magic
series of steps that will guarantee a successful application. Following I have listed some tips
from personal experience that will hopefully get you started thinking about how you cur-
rently design programs and how you can improve.

TIP 1: Understand the Requirements in Detail


Obviously, you have to know what problem the application is supposed to solve before you
can write the application. The person for which you will be writing the application, or end
user, will know non-technical business requirements such as “I need to track customer
orders” and hopefully even have some detailed specifications or processes in mind. As the
designer of an application, it is your responsibility to expand the level of detail in the end
user requirements enough so that an appropriate software system can be developed.
Bridging the gap between the end user’s business knowledge and your technical knowledge
is very important. Keep in mind, end users don’t know programming or understand the sig-
nificance of making different types of changes. For instance, a typical user might not dare to
ISBN: 0-558-13856-X

ask you to change a field caption (which is an easy change in Visual Basic), but on the other

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 16

16 Chapter 1 Introduction to Visual Basic .NET

hand they might involuntarily omit a critical requirement of the system, which totally invali-
dates your design! The symptom of this problem is after delivery of your program the end
user has a lot of questions that begin with “But what about . . . ?”
The way to avoid these problems is to spend a lot of time planning and discussing the
requirements of the program with the end user to make sure that both of you understand
them completely. Altering a design during the planning stages of a project is a lot cheaper
and faster than altering it after the project is finished. A good rule of thumb is to ask ques-
tions until you are tired of it, then keep on going! Even though you may not understand all
aspects of the user’s business, and they will certainly not understand all the technical aspects
of programming, it is important to keep an open line of communication.
One design tool that may help everyone understand the application is the use of a prototype.
A prototype is a partially functional or nonfunctional version of the application to give the
user an idea of what the final product will look like. This is easy to do in Visual Basic and
can be useful in extracting more requirements from the user.

TIP 2: Make Your Design Flexible


Flexibility refers to the ability of your programs to adapt to a changing environment. End
users will often come up with new requirements as the problem or process the software sup-
ports changes over time. A flexible program is able to accommodate minor changes in
requirements with minimal or no code changes. For example, suppose you are writing a
database application that needs to store when a customer order is placed and shipped. You
could store these dates in the following database table:
Order# OrderDate ShipDate
12345 1/1/99 1/2/99

Suppose in the future, the users of the program want to add a delivery date. With the previ-
ous table design, you will have to add a new column to the database, altering the table
design. However, suppose you had originally created the table as follows:
Order# DateType AssociatedDate
12345 ORDER 1/1/99
12345 SHIP 1/2/99
12345 DELIVERY 1/3/99

With this more generic table design, the additional requirement of a delivery date is accom-
plished by simply adding another record to the database. (Of course you may still need to
modify the user interface, but these modifications will be easier because it is based on the
same underlying database.)
➔ Please see Chapter 20, “Database Basics,” for more information on database design.
Although the second design is more flexible, the first one has the potential to be faster,
because everything about an order is stored on one database record instead of three. The
key is to understand the requirements well enough to achieve the appropriate balance of
flexibility and speed.
ISBN: 0-558-13856-X

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 17

Tips for a Good Design 17

TIP 3: Keep Code Maintainability in Mind


Part
When you write Visual Basic code, you should try to make it as clear as possible so that
yourself and others can maintain it in the future. This means it is important to have docu- I
mentation at all levels, from comments in the code itself to pictures, flowcharts, and user Ch
instructions.
1
Although coding style itself is not necessarily a design issue, making decisions during the
design process to use standard coding practices and naming conventions will have a direct
impact on a program’s maintainability. This is especially important if you work on a devel-
opment team where more than one person will be working on the same program.
Consider, for example, the following section of code:
Public Sub ChangeCount(ID as String, NewValue As Integer)
Dim OldValue As Integer
OldValue = GetCurrentCounter(ID)
If OldValue < NewValue then Call UpdateCurrentCounter(ID,NewValue)
End Sub

This code in the previous function is very simple; it accepts an ID and a new value, and calls
some other functions to update a counter if the new value is greater than the old value.
However, the purpose behind the function is not very clear. Now, read the following version
of the function, which has been rewritten in a clearer style:
Public Sub ChangeCount(astrUserID As String, aintNumLogins AS Integer)

’This function updates the login counter for the given user

Dim lintCurrentLogins AS Integer

lintCurrentLogins = GetCurrentCounter(astrUserID)

’Only update if passed value is greater than current value


If lintCurrentLogins < aintNumLogins Then
Call UpdateCurrentCounter(astrUserID, aintNumLogins)
End If
End Sub

➔ For more information on declaring variables, see “Declaring and Naming Variables,” p. 140
Notice that there are comments to explain both the general purpose of the function and any
sections that may not be already clear to the reader. The variables have been declared with a
prefix to indicate the scope (l for local, a for argument) and data type (int for Integer, str for
string). The use of indentation, blank lines, and a multi-line If statement makes the code
visually easier to comprehend. By deciding on techniques such as these you can create code
that people can understand quicker.

TIP 4: Design for Reuse


Another tip that applies to group application development is reuse. If you write a lot of the
same types of programs, you do not want to “reinvent the wheel” each time. For example, if
ISBN: 0-558-13856-X

you write a function that connects to a database and returns a recordset, you should be able

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 18

18 Chapter 1 Introduction to Visual Basic .NET

to use it on all your database projects. If you keep the modules of your program well sepa-
rated by functional requirements and avoid hard-coding values where appropriate, you can
create code that you can use again and again.

TIP 5: Test Your Design


After you write a program but before it is technically finished, there is usually a testing
process to find bugs in the program. The primary goal of testing is, of course, to validate
that the program successfully performs the desired tasks. Another goal of an application
tester is to attempt to break your application through the use of borderline and invalid
input.
The same types of “testing to break it” techniques can apply to the design of your program.
This is especially applicable in database design because you can often look at the table lay-
outs and joins to see limitations. If you can get the end users to agree to as many absolute
facts as possible (“No, there will never be two active management types at the same time”)
then you can verify that your database design will accommodate their needs. Discussing
your plan with other competent developers and trying to shoot as many holes in it as possi-
ble also can be a useful activity.

Big Changes in Visual Basic .NET


Visual Basic .NET includes a large number of major enhancements since the previous ver-
sion, Visual Basic 6.0. We will discuss these enhancements throughout this book. Some of
these enhancements, which are part of the complete overhaul of Visual Studio (Visual Basic’s
parent product), include the following:
■ Web Forms—allow you to build Web pages to run in client browsers
■ Web Services—run on Web servers to be invoked across the Internet or an intranet
■ Server Explorer—provides an easy-to-use interface to server-based components
■ Visual Component Designer—assists in creating server-side components

In addition, Visual Basic itself has received a number of much-needed improvements,


including
■ Inheritance—allows classes to be inherited and reused
■ Multithreading—allows you to build free-threaded applications
■ Initializers—allow you to set the initial values of variables as they are defined
■ Structured Exception Handling—centralizes the handling of program errors

In addition, Visual Studio .NET makes extensive use of eXtensible Markup Language
(XML), a markup language that provides a format for describing structured data. Visual
Studio .NET also utilizes a standardized Integrated Development Environment (IDE),
ISBN: 0-558-13856-X

which is the interface in which you build your applications. This will greatly simplify the
porting of your skills from one Visual Studio .NET language to another.

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 19

From Here . . . 19

From Here . . . Part


This chapter has given you an overview of the application development process, the history
of Visual Basic .NET, and some handy programming tips. The remainder of this book will
I
serve as a guide as you begin to develop your own applications. Ch

■ The next chapter, “Creating Your First Windows Application,” will walk you step-by- 1
step through the process of developing a fully functional application that will run on a
Windows-based PC.
■ Chapter 3, “Creating Your First Web Application,” shows you how to use Visual Basic
.NET to develop programs that can be accessed across the Internet or an intranet via a
Web browser.
■ You will receive a thorough introduction to Visual Basic .NET and the Visual Studio
.NET development environment in Chapter 5, “Visual Basic Building Blocks.”
ISBN: 0-558-13856-X

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.
03 572x CH01 11/29/01 2:13 PM Page 20

ISBN: 0-558-13856-X

Special Edition Using Visual Basic® .Net, by Brian Siler and Jeff Spotts. Copyright © 2002 by Que. Published by Que Publishing.

Вам также может понравиться