Preface ix
Chapter 1 Introduction to Computers and Programming 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 How Computers Store Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 How a Program Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.5 Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.6 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.7 The Program Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.8 Getting Started with the Visual Studio Environment . . . . . . . . . . . . . . . . . . 27
TUTORIAL 1-1: Starting Visual Studio and Setting Up the Environment . . . . . . . . . . 28
TUTORIAL 1-2: Starting a New Visual C# Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
TUTORIAL 1-3: Saving and Closing a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
TUTORIAL 1-4: Opening an Existing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
TUTORIAL 1-5: Getting Familiar with the Visual Studio Environment . . . . . . . . . . . 44
Key Terms 45 • Review Questions 46 • Exercises 51
Chapter 2 Introduction to Visual C# 53
2.1 Getting Started with Forms and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.2 Creating the GUI for Your First Visual C# Application:
The Hello World Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
TUTORIAL 2-1: Creating the GUI for the Hello World Application . . . . . . . . . . . . . . 63
2.3 Introduction to C# Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.4 Writing Code for the Hello World Application . . . . . . . . . . . . . . . . . . . . . . . . 77
TUTORIAL 2-2: Writing Code for the Hello World Application . . . . . . . . . . . . . . . . 77
2.5 Label Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
TUTORIAL 2-3: Creating the Language Translator Application . . . . . . . . . . . . . . . . . 88
2.6 Making Sense of IntelliSense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
2.7 PictureBox Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
TUTORIAL 2-4: Creating the Flags Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
TUTORIAL 2-5: Creating the Card Flip Application . . . . . . . . . . . . . . . . . . . . . . . . 100
2.8 Comments, Blank Lines, and Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . 103
2.9 Writing the Code to Close an Application’s Form . . . . . . . . . . . . . . . . . . . . 106
2.10 Dealing with Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Key Terms 108 • Review Questions 108 • Programming Problems 113
Chapter 3 Processing Data 117
3.1 Reading Input with TextBox Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
3.2 A First Look at Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
TUTORIAL 3-1: The Birth Date String application . . . . . . . . . . . . . . . . . . . . . . . . . 126
3.3 Numeric Data Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
3.4 Performing Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
3.5 Inputting and Outputting Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . 141
TUTORIAL 3-2: Calculating Fuel Economy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
3.6 Formatting Numbers with the ToString Method . . . . . . . . . . . . . . . . . . . 149
TUTORIAL 3-3: Creating the Sale Price Calculator application
with currency formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
3.7 Simple Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
TUTORIAL 3-4: Creating the Test Average Application
with Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
3.8 Using Named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
3.9 Declaring Variables as Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
TUTORIAL 3-5: Creating the Change Counter Application . . . . . . . . . . . . . . . . . . . 169
3.10 Using the Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
3.11 More GUI Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Key Terms 185 • Review Questions 185 • Programming Problems 190
Chapter 4 Making Decisions 195
4.1 Decision Structures and the if Statement . . . . . . . . . . . . . . . . . . . . . . . . . 195
TUTORIAL 4-1: Completing the Test Score Average application . . . . . . . . . . . . . . . 200
4.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
TUTORIAL 4-2: Completing the Payroll with Overtime application . . . . . . . . . . . . 206
4.3 Nested Decision Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
TUTORIAL 4-3: Completing the Loan Qualifier application . . . . . . . . . . . . . . . . . . 212
4.4 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
4.5 bool Variables and Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
4.6 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
4.7 Preventing Data Conversion Exceptions with the TryParse Methods . . . . 230
TUTORIAL 4-4: Calculating Fuel Economy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
4.8 Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
4.9 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
TUTORIAL 4-5: Creating the Color Theme application . . . . . . . . . . . . . . . . . . . . . . 244
4.10 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
4.11 Introduction to List Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
TUTORIAL 4-6: Creating the Time Zone application . . . . . . . . . . . . . . . . . . . . . . . . 251
Key Terms 255 • Review Questions 255 • Programming Problems 259
Chapter 5 Loops, Files, and Random Numbers 265
5.1 More About ListBoxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
5.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
TUTORIAL 5-1: Using a Loop to Calculate an Account Balance . . . . . . . . . . . . . . . 271
TUTORIAL 5-2: Enhancing the Ending Balance Application . . . . . . . . . . . . . . . . . . 274
5.3 The ++ and -- operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
5.4 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
TUTORIAL 5-3: Using the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
5.5 The do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
5.6 Using Files for Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
TUTORIAL 5-4: Writing Data to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
TUTORIAL 5-5: Appending Data to the Friend.txt File . . . . . . . . . . . . . . . . . . . . . . 300
TUTORIAL 5-6: Using a Loop to Read to the End of a File . . . . . . . . . . . . . . . . . . . 308
TUTORIAL 5-7: Calculating a Running Total . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
5.7 The OpenFileDialog and SaveFileDialog Controls . . . . . . . . . . . . . . . . . . . . 315
5.8 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
TUTORIAL 5-8: Simulating Coin Tosses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
5.9 The Load Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
TUTORIAL 5-9: Creating a Load Event Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Key Terms 330 • Review Questions 330 • Programming Exercises 333
Chapter 6 Modularizing Your Code with Methods 337
6.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
6.2 void Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
TUTORIAL 6-1: Creating and Calling Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
6.3 Passing Arguments to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
TUTORIAL 6-2: Passing an Argument to a Method . . . . . . . . . . . . . . . . . . . . . . . . . 350
6.4 Passing Arguments by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
TUTORIAL 6-3: Using an Output Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
6.5 Value-Returning Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
TUTORIAL 6-4: Writing a Value-Returning Method . . . . . . . . . . . . . . . . . . . . . . . . 370
TUTORIAL 6-5: Modularizing Input Validation with a Boolean Method . . . . . . . . . 373
Key Terms 379 • Review Questions 379 • Programming Exercises 382
Chapter 7 Arrays and Lists 387
7.1 Value Types and Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
7.2 Array Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
TUTORIAL 7-1: Using an array to hold a list of random lottery numbers . . . . . . . . 397
7.3 Working with Files and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
7.4 Passing Arrays as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 405
7.5 Some Useful Array Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
TUTORIAL 7-2: Processing an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
7.6 Advanced Algorithms for Sorting and Searching Arrays . . . . . . . . . . . . . . . 427
7.7 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
TUTORIAL 7-3: Completing the Seating Chart application . . . . . . . . . . . . . . . . . . . 437
7.8 Jagged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
7.9 The List Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
TUTORIAL 7-4: Completing the Test Score List Application . . . . . . . . . . . . . . . . . . 450
Key Terms 456 • Review Questions and Exercises 456 • Programming
Exercises 459
Chapter 8 More About Processing Data 463
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
8.2 String and Character Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
TUTORIAL 8-1: Completing the Password Validation Application . . . . . . . . . . . . . 469
TUTORIAL 8-2: Completing the Telephone Format Application . . . . . . . . . . . . . . . 482
TUTORIAL 8-3: Completing the Telephone Unformat Application . . . . . . . . . . . . . 486
TUTORIAL 8-4: Completing the CSV Reader Application . . . . . . . . . . . . . . . . . . . . 493
8.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
TUTORIAL 8-5: Completing the Phonebook Application . . . . . . . . . . . . . . . . . . . . 507
8.4 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
TUTORIAL 8-6: Completing the Color Spectrum Application . . . . . . . . . . . . . . . . . 516
8.5 The ImageList Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
TUTORIAL 8-7: Completing the Random Card Application . . . . . . . . . . . . . . . . . . 523
Key Terms 526 • Review Questions 526 • Programming Problems 530
Chapter 9 Classes and Multiform Projects 535
9.1 Introduction to Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
TUTORIAL 9-1: Creating and Using the Coin class . . . . . . . . . . . . . . . . . . . . . . . . . 542
9.2 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
TUTORIAL 9-2: Creating and Using the CellPhone class . . . . . . . . . . . . . . . . . . . . 550
9.3 Parameterized Constructors and Overloading . . . . . . . . . . . . . . . . . . . . . . 555
TUTORIAL 9-3: Creating and Using the BankAccount class . . . . . . . . . . . . . . . . . . 556
9.4 Storing Class Type Objects in Arrays and Lists . . . . . . . . . . . . . . . . . . . . . 562
TUTORIAL 9-4: Completing the Cell Phone Inventory Application . . . . . . . . . . . . . 564
9.5 Finding the Classes and Their Responsibilities in a Problem . . . . . . . . . . . . 568
9.6 Creating Multiple Forms in a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
TUTORIAL 9-5: Creating an application with two forms . . . . . . . . . . . . . . . . . . . . . 581
TUTORIAL 9-6: Accessing a Control on a Different Form . . . . . . . . . . . . . . . . . . . . 585
Key Terms 591 • Review Questions 591 • Programming Problems 595
Chapter 10 Inheritance and Polymorphism 599
10.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
TUTORIAL 10-1: Creating and testing the SavingsAccount
and CDAccount classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
10.2 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
TUTORIAL 10-2: Completing the Polymorphism Application . . . . . . . . . . . . . . . . . 622
10.3 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
TUTORIAL 10-3: Completing the Computer Science Student Application . . . . . . . . 629
Key Terms 634 • Review Questions 634 • Programming Exercises 637
Chapter 11 Databases 639
11.1 Introduction to Database Management Systems . . . . . . . . . . . . . . . . . . . . 639
11.2 Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
11.3 Creating a Database in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
TUTORIAL 11-1: Starting the Phone Book Application and Creating the Phonelist.mdf Database . . . . . . . . . . . . . . . . . . . . . . . . . . 645
11.4 The DataGridView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
TUTORIAL 11-2: Completing the Phone Book Application . . . . . . . . . . . . . . . . . . . 653
11.5 Connecting to an Existing Database and Using Details View Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
TUTORIAL 11-3: Creating the Products Application and Using a Details View . . . . 661
11.6 More About Data-Bound Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
TUTORIAL 11-4: Creating the Product Lookup Application . . . . . . . . . . . . . . . . . . 674
TUTORIAL 11-5: Creating the Multiform Products Application . . . . . . . . . . . . . . . 676
11.7 Selecting Data with the SQL Select Statement . . . . . . . . . . . . . . . . . . . . . 682
TUTORIAL 11-6: Creating the Product Queries Application . . . . . . . . . . . . . . . . . . 688
TUTORIAL 11-7: Creating the Product Queries Application . . . . . . . . . . . . . . . . . . 697
TUTORIAL 11-8: Creating the Product Search Application . . . . . . . . . . . . . . . . . . . 702
Key Terms 707 • Review Questions 707 • Programming Exercises 711
Appendix A C# Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
Appendix C ASCII/Unicode Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
Appendix D Answers to Checkpoint Questions . . . . . . . . . . . . . . . . . . . . . 737
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757