Professional Documents
Culture Documents
Preface xvii
vii
viii Contents
8.3 Procedures and Functions That Work with Arrays ..................................... 524
TUTORIAL 8-5: Examining an application that passes an array
to procedures and functions ......................................................................... 526
8.4 Multidimensional Arrays ........................................................................... 531
TUTORIAL 8-6: Completing the Seating Chart application ....................................... 535
8.5 Focus on GUI Design: The Enabled Property and the
Timer Control .......................................................................................... 540
TUTORIAL 8-7: The Timer Demo ............................................................................ 541
TUTORIAL 8-8: Creating the Catch Me game ........................................................... 542
8.6 Focus on GUI Design: Anchoring and Docking Controls ........................... 545
8.7 Focus on Problem Solving: Building the Demetris Leadership Center
Application .............................................................................................. 548
TUTORIAL 8-9: Building the Demetris Leadership Center
Sales Reporting application .......................................................................... 551
8.8 Using Lists to Hold Information (Optional Topic) ...................................... 554
TUTORIAL 8-10: Building a List from user input ...................................................... 556
Summary 559 • Key Terms 560 • Review Questions and Exercises 561 •
Programming Challenges 566
W elcome to Starting Out with Visual Basic, Eighth Edition. This edition has been
revised and updated for Visual Studio 2017. It is intended for use in an intro-
ductory programming course. It is designed for students who have no prior programming
background, but even experienced students will benefit from its depth of detail and the
chapters covering databases, Web applications, and other advanced topics. The book is
written in clear, easy-to-understand language and covers all the necessary topics of an
introductory programming course. The text is rich in concise, practical, and real-world
example programs, so the student not only learns how to use the various controls, con-
structs, and features of Visual Basic, but also learns why and when to use them.
xvii
xviii Preface
Microsoft has continued to enhance and improve Visual Basic by regularly releasing new
versions. The versions, which are named after the year in which they were released, are
Visual Basic 2003, Visual Basic 2005, Visual Basic 2008 and so forth. At the time this
book was written, the current release was Visual Basic 2017. You can see a complete list of
the enhancements that have been made to this version of Visual Basic, as well as past ver-
sions, on this web page: msdn.microsoft.com/en-us/library/we86c8x2.aspx.
Chapters 1–7
Chapter 11
Chapters 1 through 7 cover the fundamentals of program design, flow control, modular
programming, and the most important Visual Basic controls. The instructor may then
continue in any order with Chapters 8, 9, 10, or 12. Part of Chapter 11 relies on database
concepts, so it should be covered after Chapter 10.
Chapter 4: Making Decisions. The student learns about relational operators and
how to control the flow of a program with the If... Then, If...Then... Else, and
If... Then... ElseIf statements. Logical operators are introduced, and the Select Case
statement is covered. Important applications of these constructs are discussed, such as
testing numeric values, strings, and determining if a value lies within a range, and validat-
ing user input. Several string-handling functions and string methods are discussed. Radio
buttons and check boxes are also introduced.
Chapter 5: Lists and Loops. This chapter begins by showing the student how to use
input boxes as a quick and simple way to gather input. Next, list boxes and combo boxes
are introduced. The chapter covers repetition control structures: the Do While, Do Until,
and For... Next loops. Counters, accumulators, running totals, and other loop-related
topics are discussed. The student also learns how to generate random numbers.
Chapter 6: Procedures and Functions. The student learns how and why to modu-
larize programs with general-purpose procedures and functions. Arguments, parameters,
and return values are discussed. Debugging techniques for stepping into and over proce-
dures are introduced.
Chapter 7: Multiple Forms, Modules, and Menus. This chapter shows how to
add multiple forms to a project and how to create a module to hold procedures and func-
tions that are not associated with a specific form. It covers creating a menu system, with
commands and submenus that the user may select from.
Chapter 8: Arrays and More. This chapter discusses both single dimension and mul-
tidimensional variable arrays. Many array programming techniques are presented, such
as summing all the elements in an array, summing all the rows or columns in a two-
dimensional array, searching an array for a specific value, sorting arrays, and using paral-
lel arrays. The Enabled property, timer controls, and control anchoring and docking are
also covered.
Chapter 9: Files, Printing, and Structures. This chapter begins by discussing how
to save data to sequential text files and then read the data back into an application. The
OpenFileDialog, SaveFileDialog, FontDialog, and ColorDialog controls are introduced.
The PrintDocument control is discussed, with a special focus on printing reports. The
chapter shows the student how to create user-defined data types with structures.
Chapter 10: Working with Databases. This chapter introduces basic database con-
cepts. The student learns how to display a database table in a DataGridView control and
write applications that display, sort, and update database data. The Structured Query
Language (SQL) is introduced. An application that shows how to display database data in
list boxes, text boxes, labels, and combo box is presented. The chapter concludes with an
overview of Language Integrated Query (LINQ).
Chapter 11: Developing Web Applications. This chapter shows the student how
to create ASP.NET applications that run on Web Browsers such as Internet Explorer,
Chrome, Firefox, and Safari. Using Microsoft Visual Studio, or Microsoft Visual Web
Developer, the student learns how to use Web server controls and Web forms to build
interactive, database-driven Web applications.
Chapter 12: Classes, Collections, and Inheritance. This chapter introduces
classes as a tool for creating abstract data types. The process of analyzing a problem and
determining its classes is discussed, and techniques for creating objects, properties, and
methods are introduced. Collections are presented as structures for holding groups of
objects. The Object Browser, which allows the student to see information about the
xx Preface
classes, properties, methods, and events available to a project, is also covered. The chap-
ter concludes by introducing inheritance, and shows how to create a class that is based on
an existing class.
Appendix A: Advanced User Interface Controls and Techniques. Discusses
many of the more advanced controls available in Visual Basic, as well as several helpful
programming techniques. This appendix also provides a summary of common user inter-
face design guidelines.
Appendix B: Windows Presentation Foundation (WPF). Introduces the student
to the Windows Presentation Framework (WPF), and includes a tutorial in which the stu-
dent creates a simple WPF application.
Appendix C: Converting Mathematical Expressions to Programming State-
ments. Shows the student how to convert a mathematical expression into a Visual Basic
programming statement.
Appendix D: Answers to Checkpoints. Students may test their progress by com-
paring their answers to Checkpoints with the answers provided. The answers to all Check-
points are included.
Appendix E: Glossary. Provides a glossary of the key terms presented in the text.
The following appendixes can be downloaded from the book’s companion Web site at
www.pearson.com/gaddis.
Appendix F: Visual Basic Function and Method Reference. Provides a reference
for the functions and methods that are covered in the text. The exceptions that may be
caused by these functions and methods are also listed.
Appendix G: Binary and Random-Access Files. Describes programming tech-
niques for creating and working with binary and random-access data files.
Review Questions and Exercises. In the tradition of all Gaddis texts, each chap-
ter presents a thorough and diverse set of review questions and exercises. These
include traditional fill-in-the-blank, true or false, multiple choice, and short answer
questions. There are also unique tools for assessing a student’s knowledge. For exam-
ple, Find the Error questions ask the student to identify syntax or logic errors in brief
code segments. Algorithm Workbench questions ask the student to design code seg-
ments to satisfy a given problem. There are also What Do You Think? questions that
require the student to think critically and contemplate the topics presented in the
chapter.
Programming Challenges. Each chapter offers a pool of programming exercises
designed to solidify the student’s knowledge of the topics at hand. In most cases, the
assignments present real-world problems to be solved. When applicable, these exercises
also include input validation rules.
Supplements
Student
The following supplementary material is bundled with the book:
• Source code and files required for the chapter tutorials are available at the book’s
companion website: www.pearson.com/gaddis.
• The website also contains Appendix F, Visual Basic Function and Method Refer-
ence, and Appendix G, Binary and Random-Access Files.
Instructor
The following supplements are available to qualified instructors:
• Answers to all Review Questions in the text
• Solutions for all Programming Challenges in the text
• PowerPoint presentation slides for every chapter
• Test bank
• Test generation software that allows instructors to create customized tests
For information on how to access these supplements, visit the Pearson Education Instruc-
tor Resource Center at http://www.pearson.com.
For a full demonstration, to see feedback from instructors and students, or to get started using
MyLab Programming in your course, visit www.pearson.com/mylab/programming.
Web Resources
Self-assessment quizzes, PowerPoint slides, source code files, and glossary flashcards
are available on the Companion Website for Starting Out with Visual Basic at www.
pearson.com/gaddis.
Acknowledgments
There were many helping hands in the development and publication of this text. The
authors would like to thank the following faculty reviewers for their helpful suggestions
and expertise:
Achla Agarwal, Bossier Parrish Community College Martha Gibson, Central Texas College
Ronald Bass, Austin Community College Iskandar Hack, Indiana University—Purdue
Ronald Beauchemin, Springfield Technical University at Fort Wayne
Community College Tom Higginbotham, Southeastern Louisiana
Zachory T. Beers, Microsoft Corporation University
Robert M. Benavides, Collin County Community Dennis Higgins, SUNY Oneonta
College District David M. Himes, Oklahoma State University,
Paul T. Bladek, Edmonds Community College Okmulgee
Douglas Bock, Southern Illinois University at Greg Hodge, Northwestern Michigan College
Edwardsville Corinne Hoisington, Central Virginia
Skip Bottom, J. Sargeant Reynolds Community Community College
College Jackie Horton, University of Vermont
Harold Broberg, Indiana Purdue University May-Chuen Hsieh, Southwest Tennessee
Nancy Burns, Professor of Computer Science, Community College
Chipola College Lee A. Hunt, Collin County Community College
Arthur E. Carter, Radford University Darrel Karbginsky, Chemeketa Community
Mara Casado, State College of Florida, College
Manatee-Sarasota Frank J. Kreimendahl, University of New
Joni Catanzaro, Louisiana State University Hampshire
Jesse Cecil, College of the Siskiyous Herb Kronholm, Mid-State Technical College
Dr. Robert Coil, Cincinnati State Community Phil Larschan, Tulsa Community College
and Technical College Linda Lau, Longwood University
Mohammad Dadashzadeh, Oakland University Art Lee, Lord Fairfax Community College
Travis Dalton, Columbia College (SC) Joo Eng Lee-Partridge, Central Connecticut State
Carol A. DesJardins, St. Clair County University
Community College Jing Liu, Southeastern Louisiana University
Sallie Dodson, Radford University Juan Marquez, Mesa Community College
William J. Dorin, Indiana University Gary Marrer, Glendale Community College
Robert Ekblaw, SUNY Albany Norman McNeal, Dakota County Technical
Rose M. Endres, City College of San Francisco College
Jean Evans, Brevard Community College George McOuat, Hawaii Pacific University
Mark Fienup, University of Northern Iowa Joseph Merrell
Pierre M. Fiorini, PhD, University of Southern Sylvia Miner, Florida International University
Maine Billy Morgan, Holmes Community College
Arlene Flerchinger, Chattanooga State Technical Joan P. Mosey, Point Park College
Community College Solomon Negash, Kennesaw State University
Lawrence Fudella, Erie Community College Robert Nields, Cincinnati State Community and
Gail M. Gehrig, Florida Community College at Technical College
Jacksonville Gregory M. Ogle
Jayanta Ghosh, Florida Community College Christopher J. Olson, Dakota State University
Preface xxiii
Merrill B. Parker, Chattanooga State Technical JoAnne Strickland, Solano Community College
Community College Angeline Surber, Mesa Community College
Rembert N. Parker, Anderson University Robert L. Terrell, Walters State Community
Alison Pechenick, University of Vermont College
Richard Pelletier, San Diego City College Joaquin Velez, San Diego Community College
Carol M. Peterson, South Plains Community District
College Margaret Warrick, Allan Hancock College
Anita Philipp, Oklahoma City Community Doug Waterman, Fox Valley Technical College
College Elaine Yale Weltz, Seattle Pacific University
T. N. Rajashekhara, Camden County College Floyd Jay Winters, Program Director, Computer
Gregory Ramsay, Morgan State University Science, College of Florida, Manatee-
Mark Reis, University of Virginia Sarasota
Malu Roldan, San Jose State Catherine Wyman, DeVry Institute, Phoenix
Pete Sanderson, Otterbein University Erik Wynters, Bloomsburg University of
Judy Scholl, Austin Community College Pennsylvania
Gurmukh Singh, SUNY at Fredonia Sheri L. York, Ball State University
Anne Spalding, Mesa State College Youlong Zhuang, Columbia College of Missouri
Judith A. Stafford, Tufts University
The authors would like to thank their families for their tremendous support throughout
this project. We would also like to thank everyone at Pearson who is part of the editorial,
production, and marketing team. We are extremely fortunate to have Matt Goldstein as
our editor. He has guided us through the delicate process of updating this book many
times. We are also fortunate to have Demetrius Hall and Yvonne Vannatta as our market-
ing managers. Their hard work is truly inspiring, and they do a great job of getting this
book out to the academic community. The production team, led by Amanda Brands,
worked tirelessly to make this book a reality. Thanks to you all!
When we look into the name “Dundee,” we find that some derive
it from the Latin “Donum Dei” (the “gift of God”). Evidently those who
designed the town arms accepted this etymology, for above the two
griffins holding a shield is the motto “Dei Donum.” Yet beneath their
intertwined and forked tails is the more cautious motto, perhaps
meant to be regulative,—for “sweet are the uses of adversity,” in
learning as in life,—“prudentia et candore.” While prudence bids us
look forward, candor requires honesty as to the past. So the diligent
scholarship and editorial energy of modern days delete the claims of
local pride, as belated, and declare them “extravagant,” while
asserting that so favorable a situation for defence as has Dundee
antedates even the Roman occupation.
Others, not willing to abandon the legend savoring of divinity, find
the name in the Celtic “Dun Dha,” the “Hill of God.” The probabilities
are, however, that Mars will carry off the honors, and that the modern
form is from the Gaelic name “Dun Tow,” that is, the “fort on the Tay”;
of which the Latin “Tao Dunum” is only a transliteration. All Britain
was spotted with forts or duns, and the same word is in the second
syllable of “London.”
The name “Dundee” first occurs in writing in a deed of gift, dated
about a.d. 1200, by David, the younger brother of William the Lion,
making the place a royal burg. Later it received charters from Robert
the Bruce and the Scottish kings. Charles I finally granted the city its
great charter.
In the war of independence, when the Scots took up arms
against England, Dundee was prominent. William Wallace, educated
here, slew the son of the English constable in 1291, for which deed
he was outlawed. The castle, which stood until some time after the
Commonwealth, but of which there is to-day hardly a trace, was
repeatedly besieged and captured. In Dundee’s coat of arms are two
“wyverns,” griffins, or “dragons,” with wings addorsed and with
barbed tails, the latter “nowed” or knotted together—which things
serve as an allegory. In the local conversation and allusions and in
the modern newspaper cartoons and caricatures, the “wyverns”
stand for municipal affairs and local politics.
Such a well-situated port, on Scotland’s largest river, Tay, must
needs be the perennial prize of contending factions and leaders. But
when, after having assimilated the culture of Rome, the new
struggle, which was inevitable to human progress, the Reformation,
began, and the Scots thought out their own philosophy of the
universe, Dundee was called “the Scottish Geneva,” because so
active in spreading the new doctrines. Here, especially, Scotland’s
champion, George Wishart, student and schoolmaster (1513–46),
one of the earliest reformers, introduced the study of Greek and
preached the Reformation doctrines. Compelled to flee to England,
he went also to Switzerland. In Cambridge, he was a student in
1538. It is not known that he ever “took orders,” any more than did
the apostle Paul. He travelled from town to town, making everywhere
a great impression by his stirring appeals.
Instead of bearing the fiery cross of the clans as of old, Wishart
held up the cross of his Master.
Patriotism and economics, as well as religion, were factors in the
clash of ideas. Cardinal Beaton stood for ecclesiastical dependence
on France, Wishart for independence. Beaton headed soldiers to
make Wishart prisoner. Young John Knox attached himself to the
person of the bold reformer and carried a two-handed sword before
Wishart for his defence. After preaching a powerful sermon at
Haddington, the evangelist was made a prisoner by the Earl of
Bothwell and carried to St. Andrew’s. There, at the age of thirty-
three, by the cardinal’s order, Wishart was burned at the stake in
front of the castle, then the residence of the bishop. While the fire
was kindling, Wishart uttered the prophecy that, within a few days his
judge and murderer would lose his life. After such proceedings in the
name of God, it seems hardly wonderful that the mob, which had
been stirred by Wishart’s preaching, should have destroyed both the
cathedral and the episcopal mansion.
Was Wishart in the plot to assassinate the cardinal, as hostile
critics suggest? Over his ashes a tremendous controversy has
arisen, and this is one of the unsettled questions in Scottish history.
There was another George Wishart, bailie of Dundee, who was in
the plot. Certainly the preacher’s name is great in Scotland’s history.
One of the relics of bygone days, which the Dundeeans keep in
repair, is a section of the old battlemented city wall crossing one of
the important streets. This for a time was the pulpit of the great
reformer. With mine host of the Temperance Hotel, Bailie Mather,
who took me, as other antiquarians, poets, and scholars did also,
through the old alleys and streets, where the vestiges of historic
architecture still remain from the past, I mounted this old citadel of
freedom.
On the whole, the Reformation in Dundee was peacefully carried
out, but in 1645, during the Civil War, the city was sacked and most
of its houses went down in war fires. In 1651, General Monk, sent by
Cromwell, captured Dundee, and probably one sixth of the garrison
were put to the sword. Sixty vessels were loaded with plunder to be
sent away, but “the sailors being apparently as drunk as the soldiery”
the vessels were lost within sight of the city. “Ill got, soon lost,” said
Monk’s chaplain. Governor Lumsden, in heroic defence, made his
last stand in the old tower, which still remains scarred and pitted with
bullet marks.
Dundee rose to wealth during our Civil War, when jute took the
place of cotton. Being a place of commerce rather than of art,
literature, or romance, and touching the national history only at long
intervals, few tourists see or stay long in “Jute-opolis.” Nevertheless,
from many visits and long dwelling in the city and suburbs, Dundee
is a place dearly loved by us three; for here, in health and in
sickness, in the homes of the hospitable people and as leader of the
worship of thousands, in the great Ward Chapel, where he often
faced over a thousand interested hearers, the writer learned to know
the mind of the Scottish people more intimately than in any other city.
Nor could he, in any other better way, know the heart of Scotland,
except possibly in some of those exalted moments, when surveying
unique scenery, it seemed as if he were in the very penetralia of the
land’s beauty; or, when delving in books, he saw unroll clearly the
long panorama of her inspiring history.
Among the treasures, visible in the muniment room of the Town
House, are original despatches from Edward I and Edward II; the
original charter, dated 1327, and given the city by Robert the Bruce;
a Papal order from Leo X, and a letter from Mary Queen of Scots,
concerning extramural burials. Then the “yardis, glk sumtyme was
occupyit by ye Gray Cordelier Freres” (Franciscan Friars, who wore
the gray habit and girdle of St. Francis of Assisi) as an orchard, were
granted to the town as a burying-place by Queen Mary in 1564. The
Nine Crafts having wisely decided not to meet in taverns and
alehouses, made this former place of fruit their meeting-place; hence
the name “Howff,” or haunt. My rhyming friend Lee, in his verses on
the “Waukrife Wyverns” (wakeful griffins), has written the feelings
and experiences of his American friend, who often wandered and
mused among the graven stones, as well as his own:—
* * * * *
Vast meeting-place whaur all are mute,
For dust hath ended the dispute;
These yards are fat wi’ ither fruit
Than when the friars
Grew apples red for their wine-presses—
And stole frae ruddier dames caresses,
Else men are liars.”