ebook img

Starting out with Visual C# PDF

795 Pages·14.52 MB·English
by  GaddisTony
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Starting out with Visual C#

GLOBAL EDITION Starting Out With Visual C# FOURTH EDITION Tony Gaddis Digital Resources for Students Your new textbook provides 12-month access to digital resources that may include VideoNotes (step-by-step video tutorials on programming concepts), source code, web chapters, quizzes, and more. Refer to the preface in the textbook for a detailed list of resources. Follow the instructions below to register for the Companion Website for Tony Gaddis’ Starting Out with Visual C#, Fourth Edition, Global Edition. 1. Go to www.pearsonglobaleditions.com/Gaddis 2. Enter the title of your textbook or browse by author name 3. Click Companion Website 4. C lick Register and follow the on-screen instructions to create a login name and password Use a coin to scratch off the coating and reveal your access code. Do not use a sharp knife or other sharp object as it may damage the code. Use the login name and password you created during registration to start using the digital resources that accompany your textbook. IMPORTANT: This prepaid subscription does not include access to MyProgrammingLab, which is available at www.myprogramminglab.com for purchase. This access code can only be used once. This subscription is valid for 12 months upon activation and is not transferrable. If the access code has already been revealed it may no longer be valid. For technical support go to https://support.pearson.com/getsupport This page intentionally left blank Starting Out With Visual C# ® Fourth Edition Global Edition tony gaddis haywood Community College Harlow, England • London • New York • Boston • San Francisco • Toronto • Sydney • Dubai • Singapore • Hong Kong Tokyo • Seoul • Taipei • New Delhi • Cape Town • São Paulo • Mexico City • Madrid • Amsterdam • Munich • Paris • Milan Editorial Director: Marcia Horton Manager, Media Production, Global Edition: Acquisitions Editor: Matt Goldstein Vikram Kumar Editorial Assistant: Kristy Alaura Senior Manufacturing Controller, Production, VP of Marketing: Christy Lesko Global Edition: Kay Holman Director of Field Marketing: Tim Galligan Program Manager: Carole Snyder Product Marketing Manager: Bram van Kempen Production Project Manager: Camille Trentacoste Field Marketing Manager: Demetrius Hall Procurement Manager: Mary Fischer Marketing Assistant: Jon Bryant Senior Specialist, Program Planning and Director of Product Management: Erin Gregg Support: Maura Zaldivar-Garcia Team Lead Product Management: Manager, Rights Management: Rachel Youdelman Scott Disanno Senior Project Manager, Rights Assistant Acquisitions Editor, Global Edition: Management: William Opaluch Aditee Agarwal Cover Art: Patila/Shutterstock Assistant Project Editor, Global Edition: Cover Design: Lumina Datamatics Shaoni Mukherjee Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsonglobaleditions.com © Pearson Education Limited 2017 The right of Tony Gaddis to be identified as the author of this work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. Authorized adaptation from the United States edition, entitled Starting Out With Visual C#, 4th Edition, ISBN 978-0-13-438260-9, by Tony Gaddis published by Pearson Education © 2017. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 ISBN 10: 1-292-16321-6 ISBN 13: 978-1-292-16321-5 Typeset by iEnergizer Aptara®, Ltd. Printed and bound by Vivar in Malaysia Locations of VideoNotes www.pearsonglobaleditions.com/Gaddis Chapter 1 tutorial 1-1: Starting Visual Studio and Setting up the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 VideoNote tutorial 1-2: Starting a new Visual C# Project . . . . . . . . . . . . . . 48 tutorial 1-3: Saving and Closing a Project . . . . . . . . . . . . . . . . . 50 tutorial 1-4: Opening an Existing Project . . . . . . . . . . . . . . . . . . 58 tutorial 1-5: getting Familiar with the Visual Studio Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Chapter 2 tutorial 2-1: Creating the gui for the Hello World application . . . 79 tutorial 2-2: Writing Code for the Hello World application . . . . 93 tutorial 2-3: Creating the Language Translator application . . . 104 tutorial 2-4: Creating the Flags application . . . . . . . . . . . . . . . 112 tutorial 2-5: Creating the Card Flip application . . . . . . . . . . . . 116 Solving the Clickable number images Problem . . . . . . . . . . . . 131 Chapter 3 tutorial 3-1: the Birth Date String application . . . . . . . . . . . . . 144 tutorial 3-2: Calculating Fuel Economy . . . . . . . . . . . . . . . . . . 164 tutorial 3-3: Creating the Sale Price Calculator application with Currency Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 tutorial 3-4: Creating the Test Average application with Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 tutorial 3-5: Creating the Change Counter application . . . . . . 187 tutorial 3-6: Single-stepping through an application’s Code at runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Solving the tip, tax, and total Problem . . . . . . . . . . . . . . . . . . 213 Chapter 4 tutorial 4-1: Completing the Test Score Average application . . 222 tutorial 4-2: Completing the Payroll with Overtime application . 228 tutorial 4-3: Completing the Loan Qualifier application . . . . . 234 tutorial 4-4: Calculating Fuel Economy . . . . . . . . . . . . . . . . . . 257 tutorial 4-5: Creating the Color Theme application . . . . . . . . . 266 tutorial 4-6: Creating the Time Zone application . . . . . . . . . . . 273 Solving the Mass and Weight Problem . . . . . . . . . . . . . . . . . . . 282 Chapter 5 tutorial 5-1: using a Loop to Calculate an account Balance . . . 293 tutorial 5-2: Enhancing the Ending Balance application . . . . . 296 tutorial 5-3: using the for Loop . . . . . . . . . . . . . . . . . . . . . . . . 306 tutorial 5-4: Writing Data to a text File . . . . . . . . . . . . . . . . . . 317 tutorial 5-5: appending Data to the Friend .txt File . . . . . . . . . 322 tutorial 5-6: using a Loop to read to the End of a File . . . . . . . 330 tutorial 5-7: Calculating a running total . . . . . . . . . . . . . . . . . 334 tutorial 5-8: Simulating Coin tosses . . . . . . . . . . . . . . . . . . . . 344 tutorial 5-9: Creating a Load Event handler . . . . . . . . . . . . . . . 349 Solving the Celsius to Fahrenheit table Problem . . . . . . . . . . . 356 3 4 Locations of Videonotes Chapter 6 tutorial 6-1: Creating and Calling Methods . . . . . . . . . . . . . . . 365 tutorial 6-2: Passing an argument to a Method . . . . . . . . . . . . 372 tutorial 6-3: using an Output Parameter . . . . . . . . . . . . . . . . . 383 tutorial 6-4: Writing a Value-returning Method . . . . . . . . . . . 392 tutorial 6-5: Modularizing input Validation with a Boolean Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 tutorial 6-6: Practicing the Step Into Command . . . . . . . . . . . . 401 tutorial 6-7: Practicing the Step Over Command . . . . . . . . . . . 402 tutorial 6-8: Practicing the Step Out Command . . . . . . . . . . . . 403 Solving the Kinetic Energy Problem . . . . . . . . . . . . . . . . . . . . . 408 Chapter 7 tutorial 7-1: using an array to hold a List of random Lottery numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 tutorial 7-2: Processing an array . . . . . . . . . . . . . . . . . . . . . . . 448 tutorial 7-3: Completing the Seating Chart application . . . . . . 463 tutorial 7-4: Completing the Test Score List application . . . . . . 476 Solving the total Sales Problem . . . . . . . . . . . . . . . . . . . . . . . . 486 Chapter 8 tutorial 8-1: Completing the Password Validation application . 495 tutorial 8-2: Completing the Telephone Format application . . 508 tutorial 8-3: Completing the Telephone Unformat application . 512 tutorial 8-4: Completing the CSV Reader application . . . . . . . 518 tutorial 8-5: Completing the Phonebook application . . . . . . . . 533 tutorial 8-6: Completing the Color Spectrum application . . . . 541 tutorial 8-7: Completing the Random Card application . . . . . 548 Solving the Sum of numbers in a String Problem . . . . . . . . . . 555 Chapter 9 tutorial 9-1: Creating and using the Coin Class . . . . . . . . . . . . 566 tutorial 9-2: Creating and using the CellPhone Class . . . . . . . 574 tutorial 9-3: Creating and using the BankAccount Class . . . . . 581 tutorial 9-4: Completing the Cell Phone Inventory application . 589 tutorial 9-5: Creating an application with two Forms . . . . . . 606 tutorial 9-6: accessing a Control on a Different Form . . . . . . . 611 Solving the Pet Class Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 620 Chapter 10 tutorial 10-1: Creating and testing the SavingsAccount and CDAccount Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 tutorial 10-2: Completing the Polymorphism application . . . . 646 tutorial 10-3: Completing the Computer Science Student application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 Solving the Employee and ProductionWorker Classes Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 Chapter 11 tutorial 11-1: Starting the Phone Book application and Creating the Phonelist .mdf Database . . . . . . . . . . . . . . . . . . . . 669 tutorial 11-2: Completing the Phone Book application . . . . . . 677 tutorial 11-3: Creating the Products application and using a Details View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 tutorial 11-4: Creating the Product Lookup application . . . . . . 697 tutorial 11-5: Creating the Multiform Products application . . . 700 tutorial 11-6: Creating the Product Queries application . . . . . . 712 tutorial 11-7: Creating the Product Queries application . . . . . . 721 tutorial 11-8: Creating the Product Search application . . . . . . 725 Solving the Personnel Database Problem . . . . . . . . . . . . . . . . . 735 Brief Contents Chapter 1 Introduction to Computers and Programming 19 Chapter 2 Introduction to Visual C# 69 Chapter 3 Processing Data 135 Chapter 4 Making Decisions 217 Chapter 5 Loops, Files, and Random Numbers 287 Chapter 6 Modularizing Your Code with Methods 359 Chapter 7 Arrays and Lists 413 Chapter 8 More About Processing Data 489 Chapter 9 Classes and Multiform Projects 559 Chapter 10 Inheritance and Polymorphism 623 Chapter 11 Databases 663 appendix a C# Primitive Data Types 737 appendix B Additional User Interface Controls 739 appendix C ASCII/Unicode Characters 759 appendix D Answers to Checkpoint Questions 761 Index 781 5 Contents Preface 11 Attention Students 17 Chapter 1 Introduction to Computers and Programming 19 1 .1 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1 .2 hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1 .3 how Computers Store Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1 .4 how a Program Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1 .5 graphical user interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1 .6 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1 .7 the Program Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 1 .8 getting Started with the Visual Studio Environment . . . . . . . . . . . . . . . . . . 45 TUToRIAL 1-1: Starting Visual Studio and Setting Up the Environment.......... 46 TUToRIAL 1-2: Starting a New Visual C# Project .......................... 48 TUToRIAL 1-3: Saving and Closing a Project.............................. 50 TUToRIAL 1-4: Opening an Existing Project .............................. 58 TUToRIAL 1-5: Getting Familiar with the Visual Studio Environment........... 59 Key Terms 61 • Review Questions 62 • Programming Problems 67 Chapter 2 Introduction to Visual C# 69 2 .1 getting Started with Forms and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 2 .2 Creating the gui for Your First Visual C# application: the Hello World application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 TUToRIAL 2-1: Creating the GUI for the Hello World Application ............. 79 2 .3 introduction to C# Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2 .4 Writing Code for the Hello World application . . . . . . . . . . . . . . . . . . . . . . . . 93 TUToRIAL 2-2: Writing Code for the Hello World Application................ 93 2 .5 Label Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 TUToRIAL 2-3: Creating the Language Translator Application ............... 104 2 .6 Making Sense of intelliSense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 2 .7 PictureBox Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 TUToRIAL 2-4: Creating the Flags Application ........................... 112 TUToRIAL 2-5: Creating the Card Flip Application........................ 116 2 .8 Comments, Blank Lines, and indentation . . . . . . . . . . . . . . . . . . . . . . . . . . 120 2 .9 Writing the Code to Close an application’s Form . . . . . . . . . . . . . . . . . . . . 122 2 .10 Dealing with Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Key Terms 125 • Review Questions 125 • Programming Problems 130 6 Contents 7 Chapter 3 Processing Data 135 3 .1 reading input with textBox Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 3 .2 a First Look at Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 TUToRIAL 3-1: The Birth Date String Application......................... 144 3 .3 numeric Data types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 3 .4 Performing Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 3 .5 inputting and Outputting numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . 159 TUToRIAL 3-2: Calculating Fuel Economy............................... 164 3 .6 Formatting numbers with the ToString Method . . . . . . . . . . . . . . . . . . . . 167 TUToRIAL 3-3: Creating the Sale Price Calculator Application with Currency Formatting.......................................... 170 3 .7 Simple Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 TUToRIAL 3-4: Creating the Test Average Application with Exception Handling........................................... 178 3 .8 using named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 3 .9 Declaring Variables as Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 TUToRIAL 3-5: Creating the Change Counter Application................... 187 3 .10 using the Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 3 .11 More gui Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 3 .12 using the Debugger to Locate Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . 202 TUToRIAL 3-6 Single-stepping through an Application’s Code at Runtime.................................................. 203 Key Terms 208 • Review Questions 208 • Programming Problems 213 Chapter 4 Making Decisions 217 4 .1 Decision Structures and the if Statement . . . . . . . . . . . . . . . . . . . . . . . . . 217 TUToRIAL 4-1: Completing the Test Score Average Application .............. 222 4 .2 the if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 TUToRIAL 4-2: Completing the Payroll with Overtime Application ........... 228 4 .3 nested Decision Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 TUToRIAL 4-3: Completing the Loan Qualifier Application ................. 234 4 .4 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 4 .5 bool Variables and Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 4 .6 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 4 .7 Preventing Data Conversion Exceptions with the TryParse Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 TUToRIAL 4-4: Calculating Fuel Economy............................... 257 4 .8 input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 4 .9 radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 TUToRIAL 4-5: Creating the Color Theme Application ..................... 266 4 .10 the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 4 .11 introduction to List Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 TUToRIAL 4-6: Creating the Time Zone Application....................... 273 Key Terms 277 • Review Questions 277 • Programming Problems 281

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.