introduction to programming with microsoft visual...

18
Outsource Laboratories Outsource Laboratories Press Introduction to Programming with Microsoft ® Visual Basic ® .NET

Upload: others

Post on 23-Mar-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

OutsourceLaboratories

Outsource Laboratories Press™

Introduction to Programming with Microsoft® Visual Basic® .NET

Page 2: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Copyright © 2004 Outsource Laboratories Press.Printed in the United States of America

ISBN 0-9725199-7-11 2 3 4 5 6 7 8 9 10 09 08 07 06 05 04 OUTSOURCE LABORATORIES PRESS PO Box 187, Matawan, NJ 07747-0187 USAFor more information about Outsource Laboratories Press’ textbooks and services, contact us: Toll free: 888-GO-OLABS (888-466-5227) Email: [email protected] http://www.olabs.com

Technical Support: Outsource Laboratories will offer limited technical support to instructors who have purchased classroom sets and have registered with Outsource Laboratories. Contact your representative for more information. All rights reserved. This product and related documentation is protected by copyright and distributed under license restricting its use, copying, distribution, and decompilation. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without prior written authorization from the publisher. The authors and publisher have taken care in preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for any errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the information contained in this book. All other products referenced herein are trademarks of their respective holders.Visual Basic and .NET are trademarks or registered trademarks of Microsoft Corporation in the United States and other countries. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

OL300 11194

ii

Page 3: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Table of Contents

About Olabs’ Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-xv

1 Basics of Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1What is Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2A Brief History of Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3

Early History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3Stored Program Computers - The Birth of Modern Computers . . . . . . . . 1-3

Main Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5

Machine Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6Assembly Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6High Level Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7

Compilers and Interpreters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8Procedural Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9Object-Oriented Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9

Steps for Developing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11Step 1: Analyze the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11

Analyzing an Example Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13Exercise 1-1: Analyzing Simple Programming Problems . . . . . . . . . . . . . 1-14

Step 2: Design an Algorithm to Solve the Problem . . . . . . . . . . . . . . . . . 1-15Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15Flowcharts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18

Exercise 1-2: Designing Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20Step 3: Implement the Algorithm using a Programming Language. . . . . 1-21Step 4: Compile and Run the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21Step 5: Test and Debug the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21Step 6: Maintain the Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23

Steps in the Software Maintenance Process . . . . . . . . . . . . . . . . . . . . . . . . . 1-24Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25

2 Visual Basic .NET Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1Facts about Visual Basic .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2Facts about .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5

Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6.NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7

Types of Visual Basic .NET Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8

iii

Page 4: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NETTable of Contents

Console Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-8Windows Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-9Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-10Windows Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-11Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-12

Using Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-13Launching Visual Studio . NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-13Configuring Visual Studio .NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-15Opening a Visual Basic .NET Project. . . . . . . . . . . . . . . . . . . . . . . . . . . .2-16Main Components of the Visual Basic .NET IDE . . . . . . . . . . . . . . . . . .2-18

Compiling and Running a Visual Basic .NET Program. . . . . . . . . . . . . . . . . .2-23Steps to Compile and Run a Program for Debug Purposes . . . . . . . . . . .2-24Steps to Compile and Run a Program for Release Purposes . . . . . . . . . .2-25

Exercise 2-1: Compile and Run Visual Basic .NET Programs . . . . . . . . .2-26Running a Program from a DOS Command Prompt Window. . . . . . . . . . . . .2-27

Starting a Command Prompt Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27Changing into a Different Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28Listing All Files in a Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28A Few Words About Files and Filenames . . . . . . . . . . . . . . . . . . . . . . . .2-30Running an Executable Program from the Command Prompt . . . . . . . . .2-30

Creating a Simple Visual Basic .NET Program . . . . . . . . . . . . . . . . . . . . . . . .2-31Creating a New Visual Basic .NET Project . . . . . . . . . . . . . . . . . . . . . . .2-34Laying Out the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-34Setting Form/Control Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-39Writing Event Handling Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-43

Exercise 2-2: Creating a GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-47Exercise 2-3: Implementing Event Handlers . . . . . . . . . . . . . . . . . . . . . . .2-49Basic Structure of a Visual Basic .NET Program . . . . . . . . . . . . . . . . . . . . . .2-51Exercise 2-4: The Sub Main Procedure. . . . . . . . . . . . . . . . . . . . . . . . . . . .2-52Comments in Visual Basic .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-53Exercise 2-5: Using Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-54Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-55

3 Variables, Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-1The Add2Numbers Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-2Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-4Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-5

Declaring Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-6Assigning Values to Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-7Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-8

Exercise 3-1: Identify Valid Variable Names . . . . . . . . . . . . . . . . . . . . . . .3-9Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-9

Strongly Typed v. Weakly Typed Languages . . . . . . . . . . . . . . . . . . . . . .3-10Visual Basic .NET’s Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . .3-11

iv

Page 5: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Main Data Types Used in Programming . . . . . . . . . . . . . . . . . . . . . . . . . 3-12The Data Type ‘Integer’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12Exercise 3-2: Declare a Variable of Type Integer. . . . . . . . . . . . . . . . . . . . 3-13Exercise 3-3: Assign Values to Variables of Type Integer . . . . . . . . . . . . . 3-14The Data Type ‘Double’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16Exercise 3-4: Declare a Variable of Type Double. . . . . . . . . . . . . . . . . . . . 3-19Exercise 3-5: Assign Values to Variables of Type Double . . . . . . . . . . . . . 3-20Assigning a Value When Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . 3-21The Data Type ‘Char’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22Exercise 3-6: Declare a Variable of Type Char . . . . . . . . . . . . . . . . . . . . . 3-24Exercise 3-7: Assign Values to Variables of Type Char. . . . . . . . . . . . . . . 3-25Exercise 3-8: Identify Values of Type Char . . . . . . . . . . . . . . . . . . . . . . . . 3-26The Data Type ‘String’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27

Printing Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27Exercise 3-9: Working with the String Data Type . . . . . . . . . . . . . . . . . . . 3-29The Data Type ‘Boolean’. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30Exercise 3-10: Working with the Boolean Data Type . . . . . . . . . . . . . . . . 3-32Exercise 3-11: Map Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34

Literal Type Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34Type Conversion and Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-36Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37

Addition Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-38Subtraction Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-40Multiplication Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-42Division Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-44Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-46

Exercise 3-12: Using Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-48Exercise 3-13: Bills Calculator Program . . . . . . . . . . . . . . . . . . . . . . . . . . 3-50Exercise 3-14: Calculating the Average Mark . . . . . . . . . . . . . . . . . . . . . . 3-52Exercise 3-15: Calculate the Weekly Salary . . . . . . . . . . . . . . . . . . . . . . . . 3-54

Arithmetic/Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-56Exercise 3-16: Using Arithmetic/Assignment Operators . . . . . . . . . . . . . 3-57

Order of Operations - Precedence of Operators . . . . . . . . . . . . . . . . . . . . 3-58Exercise 3-17: Change the Order of Operations . . . . . . . . . . . . . . . . . . . . 3-59Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-60

4 Flow Control - Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1What is Flow Control?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2

Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4Exercise 4-1: Using Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . . 4-5

v

Page 6: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NETTable of Contents

Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-6AND Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6OR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7

Exercise 4-2: Using Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-8Types of If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-9

The If-Then-Else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-9The If Statement Without Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-14

Exercise 4-3: Using an If-Then-Else Statement . . . . . . . . . . . . . . . . . . . . .4-17Exercise 4-4: Reef Cruise Reservation System . . . . . . . . . . . . . . . . . . . . . .4-19

Multiple If Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-23Exercise 4-5: Understanding Multiple If-Statements . . . . . . . . . . . . . . . .4-26Exercise 4-6: Fastfood Ordering System. . . . . . . . . . . . . . . . . . . . . . . . . . .4-27The Select-Case Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-30Exercise 4-7: Using the Select-Case Statement . . . . . . . . . . . . . . . . . . . . . .4-33Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-34

5 Flow Control - Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-1Repetition Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-2

Counter v. Sentinel-Controlled Loops . . . . . . . . . . . . . . . . . . . . . . . . . . .5-3The While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-4Exercise 5-1: Keeping Track of Variables in a While Loop . . . . . . . . . . .5-7Exercise 5-2: Debt Calculator Program . . . . . . . . . . . . . . . . . . . . . . . . . . .5-8The Do-While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-10Exercise 5-3: Bonus Notifier Program . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-13The For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-14Exercise 5-4: Name Decorator Program . . . . . . . . . . . . . . . . . . . . . . . . . . .5-17Exercise 5-5: Selecting the Appropriate Loop . . . . . . . . . . . . . . . . . . . . . .5-19Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-20

6 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-1Modularity: The Reason Behind Procedures . . . . . . . . . . . . . . . . . . . . . . . . . .6-2The Sub Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-6Parameters of a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-7

What are Parameters? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-7Passing Parameters to a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-8

Function Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-9Exercise 6-1: Identifying Parts of a Function Procedure . . . . . . . . . . . . .6-11Exercise 6-2: Calling Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-12Exercise 6-3: Modularizing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-13

Using a Function Procedure Inside a Statement as an Operand . . . . . . . .6-14Examples of Program Modularization using Procedures. . . . . . . . . . . . . . . . .6-15

vi

Page 7: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Example 1 - CostCalculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15Example 2 - Convert Inches to Centimeters. . . . . . . . . . . . . . . . . . . . . . . 6-17

Exercise 6-4: Converting Currencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-21

7 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-1What is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-1Declaring an Array Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5

Array Values Known at Compile Time . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5Exercise 7-1: Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6

Array Values Known at Run Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7Assigning Values to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8

Assigning a Value to an Array Element . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8Variables/Expressions as Indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9

Exercise 7-2: Identifying Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . 7-10Retrieving Values from an Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11

Retrieving an Array Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11Retrieving All Array Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12The GetUpperBound Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14

Common Problem: Array Index Out of Range . . . . . . . . . . . . . . . . . . . . . . . . 7-15Example Array Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16

Scenario 1: Representing Rows of People in a Theater . . . . . . . . . . . . . . 7-16Scenario 2: Weekday Translation Program . . . . . . . . . . . . . . . . . . . . . . . 7-18

Exercise 7-3: The Weekdays Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21Scenario 3: Summing Up an Array of Prices . . . . . . . . . . . . . . . . . . . . . . 7-22Scenario 4: Finding Lowest Price in Array of Prices . . . . . . . . . . . . . . . . 7-24Scenario 5: Storing Student Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-26

Exercise 7-4: Storing Student Marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-29Application Command Line Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-30Exercise 7-5: Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-31Multi-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-32

Declaring a Multi-Dimensional Array Variable . . . . . . . . . . . . . . . . . . . . 7-33Creating a Multi-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33Assigning Values to a Multi-Dimensional Array . . . . . . . . . . . . . . . . . . . 7-36Retrieving Values from a Multi-Dimensional Array . . . . . . . . . . . . . . . . 7-36

Exercise 7-6: Storing Team Scores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-39Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-40

8 Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-1Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2Creating a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2

vii

Page 8: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NETTable of Contents

Exercise 8-1: Creating String Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-4Immutability of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-5String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6Exercise 8-2: Using String Methods I . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-8Common String Manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9

Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9Exercise 8-3: Using Concatenation Operators . . . . . . . . . . . . . . . . . . . . . .8-10Exercise 8-4: Concatenating Static and Dynamic Strings . . . . . . . . . . . .8-11

Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-12Exercise 8-5: Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-14

Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-15Tokenizing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-17

Exercise 8-6: Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-19Mutable Strings - StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-20

Declaring and Creating a StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . .8-20StringBuilder Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-22Common StringBuilder Manipulations. . . . . . . . . . . . . . . . . . . . . . . . . . .8-23

Exercise 8-7: Modifying a StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . .8-24Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-25

9 Introduction to Object Oriented Programming and UML . . . . . . . . . . . . . . . . . .9-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-1What is Object Oriented Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-2Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-3Defining a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-5Exercise 9-1: Interpreting UML Class Notation . . . . . . . . . . . . . . . . . . . .9-8Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-10Exercise 9-2: Using Encapsulation to Define a Class . . . . . . . . . . . . . . . . .9-11Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-12Exercise 9-3: Inheritance Concept. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-14Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-15Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-16Code for our Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-17Using a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-19Creating Objects from a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-22Exercise 9-4: Creating Objects from Classes . . . . . . . . . . . . . . . . . . . . . . .9-24More UML Diagram Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-25

Representing a Single Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-25Exercise 9-5: Draw a Class Diagram for a Single Class . . . . . . . . . . . . . .9-27

Representing Class-Object Relationship . . . . . . . . . . . . . . . . . . . . . . . . .9-29Exercise 9-6: Drawing a Class-Object Relationship Diagram . . . . . . . . .9-30

Representing Class Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-31Exercise 9-7: Representing Class Associations. . . . . . . . . . . . . . . . . . . . . .9-34Exercise 9-8: Representing Cardinality of Relationships . . . . . . . . . . . . .9-35

viii

Page 9: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Inheritance Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-35Exercise 9-9: Inheritance Relationship Diagrams . . . . . . . . . . . . . . . . . . . 9-36Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-37

10Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-1More About Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2

Why Are Objects Important? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2Where do Objects Come From? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-3A World of Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-5

Exercise 10-1: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6Declaring Object Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-8Accessing Object Attributes and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 10-9

Access Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10Exercise 10-2: Creating and Using Objects . . . . . . . . . . . . . . . . . . . . . . . . 10-11Invoking Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-12

Member Signatures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13The Me Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14Using the .NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-15Initializing Objects with Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-16

Person Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17Procedure and Constructor Overloading . . . . . . . . . . . . . . . . . . . . . . . . . 10-18

Exercise 10-3: Using the Person Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20Objects with Multiple Aliases (Shared Objects) . . . . . . . . . . . . . . . . . . . . . . . 10-21

Side Effects of Multiple Aliases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-22Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24The Null Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26

Dereferencing a Null Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26The CD Database System Exercise: The Database Class . . . . . . . . . . . . . . . . 10-28Exercise 10-4: The CD Database System . . . . . . . . . . . . . . . . . . . . . . . . . . 10-29Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-32

11Developing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-1Steps to Create a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2

Case Study: A Banking Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2Step 1: Name the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3

A Minimal Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-4Exercise 11-1: A Minimally-Defined Class . . . . . . . . . . . . . . . . . . . . . . . . . 11-6Step 2: Determine and Declare the Attributes and Properties . . . . . . . . . . . . . 11-6Exercise 11-2: Class Attributes and Properties . . . . . . . . . . . . . . . . . . . . . 11-9Step 3: Determine, Define, and Implement the Methods . . . . . . . . . . . . . . . . 11-10

Step 3a: Defining Methods - Determine Operations . . . . . . . . . . . . . . . . 11-10

ix

Page 10: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NETTable of Contents

Step 3b: Defining Methods - Define Signatures and Return Types . . . . .11-11Naming Conventions for Visual Basic .NET Methods. . . . . . . . . . . . . . . . . 11-12Case Study Example: Determine and Define the Methods. . . . . . . . . . . . . . 11-13

Step 3c: Implement the Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-14How Method Calls Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-15Implementing the Deposit and Withdraw Methods . . . . . . . . . . . . . . . . . . . 11-16Local Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-16

Methods Implemented in BankAccount Class . . . . . . . . . . . . . . . . . . . . .11-17Exercise 11-3: Implementing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-19Initializing Objects: Constructors Revisited . . . . . . . . . . . . . . . . . . . . . . . . . .11-20

Creating a No-Argument Constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . .11-21Overloaded Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-22

Exercise 11-4: Define a Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-26Inheritance Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-26

The Inherits Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-27Using Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-28The MyBase Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-31Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-32The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-33

Abstract Classes Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-34The MustInherit Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-34Using an Abstract Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-35

Interfaces Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-38Visual Basic .NET’s IComparable Interface. . . . . . . . . . . . . . . . . . . . . . .11-38The Implements Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-39Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-40

Type Casting of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-42Up-Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-42Down-Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-42

Access Modifiers: Private, Public and Protected . . . . . . . . . . . . . . . . . . . . . . .11-43The Shared Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-45

Visual Basic .NET’s Math Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-46Exercise 11-5: Using a Shared Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . .11-47Exercise 11-6: A Banking Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-48Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-50

12Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-1What are Data Structures? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-2Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-3

Stack Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-4Implementing a Stack Using an Array . . . . . . . . . . . . . . . . . . . . . . . . . . .12-4Stack Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-7

Exercise 12-1: Using the StackArray Class. . . . . . . . . . . . . . . . . . . . . . . . .12-8Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-8

Queue Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-9

x

Page 11: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Implementing a Queue Using an Array . . . . . . . . . . . . . . . . . . . . . . . . . . 12-10Queue Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-14

Exercise 12-2: Using the QueueArray Class . . . . . . . . . . . . . . . . . . . . . . . 12-15Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-16

Linked List vs. Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-17Linked List Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-18Implementation of a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-20Linked List Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-26

Exercise 12-3: Implementing a Stack using a Linked List . . . . . . . . . . . . 12-27Exercise 12-4: Implementing a Queue using a Linked List . . . . . . . . . . . 12-28Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-29

Example Problem that Cannot be Solved Using Recursion . . . . . . . . . . . 12-29Example Problem that Can be Solved Using Recursion . . . . . . . . . . . . . 12-29

Exercise 12-5: Find the Nth Number in a Fibonacci Sequence . . . . . . . . 12-32Binary Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-33

Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-34Binary Search Tree Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-35

Inserting an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-35Locating an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-37Deleting an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-37Tree Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-41

Implementation of a Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . . 12-43Binary Tree Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-51

Exercise 12-6: Using the BinaryTree Class. . . . . . . . . . . . . . . . . . . . . . . . . 12-54Exercise 12-7: Inserting Data Into a Binary Search Tree . . . . . . . . . . . . . 12-54Exercise 12-8: Traversing a Binary Search Tree . . . . . . . . . . . . . . . . . . . . 12-55The ArrayList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-56

Methods of the ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-57Example Using the ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-57Enumerating Over an ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-59

Exercise 12-9: Using the ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . 12-60The Hashtable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-60

Enumerating Over a Hashtable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-62Exercise 12-15: Using the Hashtable Class. . . . . . . . . . . . . . . . . . . . . . . . . 12-64Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-65

13 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-1What is an Exception? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2How Visual Basic .NET Handles Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 13-3Exercise 13-1: Visual Basic .NET Exceptions . . . . . . . . . . . . . . . . . . . . . . 13-4Exception Handling with Try-Catch-Finally . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5

The Try Block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-6The Catch Block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-6

Catching Multiple Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-8The Finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-9

xi

Page 12: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NETTable of Contents

Exercise 13-2: Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-10Exercise 13-3: More on Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . .13-11Exception Handling with Throw Statement. . . . . . . . . . . . . . . . . . . . . . . . . . .13-12

Propagating Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-13Exceptions Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-14Visual Basic .NET Exception Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-16User-Defined Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-17Exercise 13-4: User-Defined Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . .13-18Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-19

14Visual Basic .NET I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-1What is I/O? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-2

Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-2Readers and Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-3

Text File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-4Reading from a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-4Writing to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-5

Exercise 14-1: Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . .14-6Exercise 14-2: Reading and Writing Files (optional) . . . . . . . . . . . . . . . . .14-7Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-9

15 The First Bank of VB GUI: A Case Study15-1Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-1First Bank of VB GUI Requirements: General . . . . . . . . . . . . . . . . . . . . . . . .15-2First Bank of VB GUI Requirements: Use Cases . . . . . . . . . . . . . . . . . . . . . .15-2First Bank of VB GUI Requirements: The User Interface. . . . . . . . . . . . . . . .15-4

The Login Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-4The Main Application Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-5The Create Customer Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-5The Create Account Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-6The Customer Details Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-7The Account Details Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-8The Search Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-9

Persistent Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-10Implementing the First Bank of VB GUI. . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-10

Creating a GUI with Multiple Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-10Creating a Menubar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-12Using Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-13Using Message Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-14

Exercise 15-1: Creating the Login Screen . . . . . . . . . . . . . . . . . . . . . . . . . .15-15Exercise 15-2: Creating a Menubar for the Main Screen . . . . . . . . . . . . .15-17Exercise 15-3: Creating the Create Customer Screen . . . . . . . . . . . . . . . .15-18

xii

Page 13: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Exercise 15-4: Handling Login Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-21Exercise 15-5: Handling Menu Item Events. . . . . . . . . . . . . . . . . . . . . . . . 15-22Exercise 15-6: Handling Create Customer Events . . . . . . . . . . . . . . . . . . 15-23Exercise 15-7: Handling Deposit/Withdrawal Events . . . . . . . . . . . . . . . . 15-24Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-25

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I-1

xiii

Page 14: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic® .NET

Index

AAbs function 11-46abstract classes 9-15, 11-34abstract methods 9-15access modifiers 10-10, 11-43accessors 9-5, 11-7

Get and Set accessors 9-10Ada Lovelace 1-3addition operator 3-38algorithm 1-15Analytical Engine 1-3And operator 4-6AndOr operator 4-7Arithmetic Logic/Unit (ALU) 1-4array element 7-1array index 7-1ArrayList class 12-56arrays

assigning values 7-8creation 7-5declaration 7-4definition 7-1GetUpperBound property 7-14multi-dimensional arrays 7-32retrieving values 7-11variable/expressions as indices 7-9

assembly language 1-6assignment operator 3-7attributes 1-9, 11-6Bbinary number 1-4binary search tree 12-34

implementation 12-43operations 12-35

binary tree 12-33traversal 12-41

bit 1-4Boolean data type 3-30

Button controladding to a form 2-38handling button click events 2-44setting properties 2-42

byte-oriented streams 14-3Ccardinality 9-31Catch block 13-6Central Processing Unit (CPU) 1-4Char data type 3-22

literal type characters 3-34character-oriented streams 14-3Charles Babbage 1-3class 1-9, 9-3

abstract class 9-15attributes 9-5, 11-6concrete class 9-15creating objects 9-22creation 11-2defining a class 9-5implementing methods 11-14methods 9-7, 11-10naming conventions 11-3properties 9-6, 11-6UML notation 9-5

Class keyword 2-51class-object relationship 9-29Code Editor Window 2-20ComboBox control

adding to a form 2-37handling combo box keyboard input events 2-

45setting properties 2-42

command-line parameters 7-30comments 2-53Common Language Runtime 2-6comparison operators 4-4compiler 1-8computer

I-1

Page 15: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic .NET®

Index

architecture 1-3main components 1-4

Concatenation operators 8-9concrete class 9-15console applications 2-8Const keyword 3-36constants 3-36constructor 10-8, 11-20

object initialization 10-16overloading 10-18, 11-22

Control Unit (CU) 1-4counter-controlled loops 5-3CType function 3-35Ddata hiding 9-10data structures

ArrayList class 12-56binary search tree 12-34binary tree 12-33Hashtable class 12-60linked lists 12-16queues 12-8stacks 12-3

data types 3-9constants 3-36main programming data types 3-12primitive data types 3-11reference data types 3-11strongly typed languages 3-10value data types 3-11weakly typed languages 3-10

default constructor 11-20Dim keyword 3-6divide and conquer 6-2division operators 3-44DOS

cd command 2-28dir command 2-28

dot operator 10-9, 10-12Double data type 3-16Do-While loop 5-2, 5-10Eencapsulation 9-3, 9-10ENIAC 1-3event handlers 2-21event handling

in Visual Basic .NET 2-43event-driven programming 2-2exception

how Visual Basic .NET handles them 13-3try block 13-6

exception handlers 13-5generalized 13-8specialized 13-8Try-Catch-Finally blocks 13-5

exceptionscatching multiple exceptions 13-8class hierarchy 13-16code example 13-14exception handlers 13-5Throw statement 13-12user-defined 13-17

expression 3-4FFile I/O 14-4FileNotFoundException 13-5Finally block 13-9First Bank of VB GUI 15-1flow control 4-2

repetition 5-2selection 4-2

flowcharts 1-18For loop 5-2, 5-14Form Design Window 2-20forms 2-20

adding controls 2-35resizing 2-35setting as Startup object 15-11setting properties 2-40showing and hiding 15-11

Function procedures 6-9Ggarbage collection 2-6, 10-24Get accessor 9-10, 11-7GetUpperBound property 7-14graphical user interface 1-9

See also GUIGUI 1-9

creating a menubar 15-12First Bank of VB GUI 15-1using message boxes 15-14using panels 15-13with multiple forms 15-10

HHashtable class 12-60high level language 1-7II/O 14-2

readers 14-3reading from a text file 14-4streams 14-2writers 14-3

I-2

Page 16: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic .NET®

Index

writing to a text file 14-5IComparable interface 11-38If-statement without Else 4-14If-Then-Else statement 4-9immutability of strings 8-5Implements statement 11-39IndexOutOfRangeException 7-15inheritance 9-12, 11-26

UML notation 9-12Inherits statement 11-27inorder traversal 12-41Input/Output (I/O) 1-4, 14-2

See also I/Oinstance variable 9-17Integer data type 3-12interfaces 9-16, 11-38intermediate language code 1-8interpreter 1-8iteration 5-2JJacquard Loom 1-3Just-In-Time compiler 1-8LLabel control

adding to a form 2-36setting properties 2-41

linked lists 12-16implementation 12-20operations 12-18

literals 3-34logical operators 4-6loop 5-2Mmachine language 1-6Math class 11-46Max function 11-46member signatures 10-13Memory 1-4, 3-20menubar 2-19, 15-12message boxes 15-14methods 1-9, 11-10

definition 11-11how methods calls work 11-15naming conventions 11-12overloading 10-18See also procedures

Microsoft Intermediate Language 2-6Min function 11-46minimal class definition 11-4modularity 1-9, 6-2Module keyword 2-51

modulo operator 3-46multi-dimensional arrays 7-32

assigning values 7-36creation 7-33declaration 7-33ragged arrays 7-35retrieving values 7-36

multiple aliases 10-21multiple If-statements 4-23multiple inheritance 9-16multiplication operator 3-42MustInherit Keyword 11-34MyBase keyword 11-31N.NET 2-3.NET Framework 2-5.NET Framework Class Library 2-7, 10-15New operator 7-7, 10-8Not operator 4-7Nothing keyword 10-26null reference 10-26

dereferencing problem 10-26Nothing keyword 10-26

Oobject 1-9, 9-3

attributes 10-9creation 10-8initializing 10-16methods 10-12properties 10-9variable declaration 10-7

Object class 11-33object equality 11-33object identity 11-33object-oriented programming 9-2object-oriented programming languages 1-9operand 3-4operating system 2-3operators 3-4

arithmetic operators 3-37arithmetic/assignment operators 3-56comparison operators 4-4concatenation operators 8-9logical operators 4-6precedence 3-58

Or operator 4-7OrElse operator 4-7Ppanels 15-13parameters 11-15

definition 6-7

I-3

Page 17: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic .NET®

Index

passed by reference 6-7passed by value 6-7passing to a procedure 6-8

polymorphism 11-32postorder traversal 12-41Pow function 11-46precedence of operators 3-58preorder traversal 12-41primitive data types 3-11, 10-7Private modifier 10-10, 11-43procedural programming languages 1-9procedures

Function procedure 6-9See also methodsSub procedure 6-6

program 1-2programming

definition 1-2history 1-3steps for developing a program 1-11

programming languagesevent-driven programming languages 2-2object-oriented programming languages 1-9procedural programming languages 1-9

programming paradigm 1-9programs

Add2Numbers 3-2, 3-39AddPrices 5-6CompanyPayroll 6-3CostCalculator 6-16Divide2Numbers 3-45InstallSoftware 4-15LengthConverter 6-18LowestPrice 7-25Mod2Numbers 3-47Multiply2Numbers 3-43MultiWeatherData 4-24PrintNumbers 5-16PropagationDemo 13-14StudentInfo 7-27Subtract2Numbers 3-41TaxSalary 4-13TheaterRow 7-18TotalPrices 7-23ToyProduction 5-12WeatherData 4-11WeatherOrNot 3-31Weekdays 7-20

project 2-14properties 11-6Properties Window 2-22

Protected modifier 10-10, 11-43pseudocode 1-15

choosing between two alternative actions 1-17choosing from multiple alternative actions 1-17performing arithmetic 1-17providing output 1-16repeating actions 1-18storing a value 1-17taking input 1-16

Public modifier 10-10, 11-43Qqueues 12-8

implementing using an array 12-10operations 12-9

Rragged arrays 7-35Random Access Memory (RAM) 1-4readers 14-3recursion 12-29reference data types 3-11reference to objects 10-7repetition 4-2, 5-2

counter-controlled loops 5-3Do-While loop 5-10For loop 5-14sentinel-controlled loops 5-3While loop 5-4

reserved keywords 3-8Return statement 6-9SSelect-Case statement 4-30selection 4-2

If-statement without Else 4-14If-Then-Else statement 4-9multiple If-statements 4-23Select-Case statement 4-30

sentinel-controlled loops 5-3Set accessor 9-10, 11-7Shared modifier 11-45short circuit evaluation 4-7Sin function 11-46software maintenance 1-23solution 2-14Solution Explorer Window 2-22Sqrt function 11-46stacks 12-3

implementing using an array 12-4operations 12-4

Standard Toolbar 2-19Startup object 2-43, 15-11statement 3-4

I-4

Page 18: Introduction to Programming with Microsoft Visual Basicolabs.com/publishing/books/ol300/ol300SampleTOC_v121004.pdf · OLutsource aboratories Outsource Laboratories Press™ Introduction

Introduction to Programming with Microsoft® Visual Basic .NET®

Index

Status Bar 2-19stored program computers 1-3StreamReader class 14-4streams 14-2StreamWriter class 14-5String class

methods 8-6String data type 3-27StringBuilder class 8-20

methods 8-22Strings 3-27, 8-2

comparisons 8-15concatenation 8-9creation 8-2immutability 8-5printing 3-27substrings 8-12

strongly typed languages 3-10Sub procedure 2-51, 6-6subclass 9-12subtraction operator 3-40superclass 9-12syntax 3-6TThrow statement 13-12Toolbox Window 2-19try block 13-6type casting 3-35

down-casting 11-42up-casting 11-42

type conversion 3-35UUML notation

cardinality 9-31cardinality symbols 9-33class relationships 9-31class-object relationship 9-29inheritance relationships 9-35single class 9-25

Unified Modeling Language (UML) 9-1use case 15-2Using a Shared Attribute 11-47Vvalue data types 3-11variables 3-5

assignment 3-7declaration 3-6local variables 11-16naming 3-8

Visual Basic .NETdata types 3-11

development environment (IDE) 2-18overview 2-2types of programs 2-8

Visual Basic .NET Programmingbasic program structure 2-51comments 2-53compiling and running a program 2-23creating a new project 2-34event handling 2-43filenames 2-30laying out the GUI 2-34opening a project 2-16running a program from a DOS window 2-27,

2-30setting form/control properties 2-39steps for developing a program 2-31

Visual Studio .NET 2-4configuring 2-15launching 2-13

Wweakly typed languages 3-10Web applications 2-10Web services 2-12While loop 5-2, 5-4Windows applications 2-9Windows services 2-11writers 14-3ZZ3 1-3

I-5