this print for content only—size & color not accurate spine = 0.8941" 472 page count Books for professionals By professionals ® Beginning VB 2008: From Novice to Professional Dear Reader, This book is about showing you how to write good code in Visual Basic 2008, even if you have never programmed before. Writing good code can be a challenge because there are so many options, especially in Visual Basic 2008. If you really want to get the most from a programming language, you need to know which features work best in which situations, and understand their strengths and weaknesses. It is this understanding that makes the difference between coding and coding well. I have written Beginning VB 2008 to teach you how to use the Visual Basic programming language to solve problems. From the earliest chapters, you will be looking at real programming challenges and learning how Visual Basic can be used to solve them. As you progress through the book, these problems become increasingly more involved and interesting, as I show you how Visual Basic features can interact to achieve the programming results you want. By the time you’ve finished reading Beginning VB 2008 and worked through the sample exercises, you will be a confident and very competent Visual Basic programmer. You will still have many new explorations of the huge .NET Framework API to look forward to in your programming ventures, but you will have a firm foundation for your future career in Visual Basic, and you will know exactly where to find all the information you need to progress confidently with your Visual Basic projects. Christian Gross Author of Beginning C# 2008: From Novice to Professional US $39.99 Shelve in Programming Languages User level: Beginner–Intermediate Gross Beginning VB 2008 The eXperT’s Voice ® in .neT Beginning VB 2008 From Novice to Professional cyan MaGenTa yelloW Black panTone 123 c Christian Gross Companion eBook Available THE APRESS ROADMAP Visual Basic 2008 Recipes: A Problem-Solution Approach Pro VB 2008 and the .NET 3.5 Platform, Third Edition Beginning VB 2008: From Novice to Professional Pro WPF with VB 2008: Windows Presentation Foundation in .NET 3.5 Beginning VB 2008 Databases: From Novice to Professional Accelerated VB 2008 www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN-13: 978-1-59059-938-9 ISBN-10: 1-59059-938-1 9 781590 599389 5 3 9 9 9 www.it-ebooks.info www.it-ebooks.info Beginning VB 2008 From Novice to Professional ■■■ Christian Gross Gross_938-1FRONT.fm Page i Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Beginning VB 2008: From Novice to Professional Copyright © 2008 by Christian Gross All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-938-9 ISBN-10 (pbk): 1-59059-938-1 ISBN-13 (electronic): 978-1-4302-0542-5 ISBN-10 (electronic): 1-4302-0542-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matthew Moodie Technical Reviewer: Andy Olsen Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Sofia Marchant Copy Editor: Marilyn Smith Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Susan Glinert Stevens Proofreader: Nancy Riddiough Indexer: Broccoli Information Management Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http:// www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. Gross_938-1FRONT.fm Page ii Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Some food for thought when writing software: “A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.” “The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair.” —Douglas Adams, Mostly Harmless Gross_938-1FRONT.fm Page iii Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Gross_938-1FRONT.fm Page iv Thursday, January 24, 2008 5:21 PM www.it-ebooks.info v Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■CHAPTER 1 Ready, Steady, Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Learning About .NET Number and Value Types . . . . . . . . . . . . . . . . 25 ■CHAPTER 3 Learning About String Manipulations . . . . . . . . . . . . . . . . . . . . . . . . . 51 ■CHAPTER 4 Learning About Data Structures, Decisions, and Loops . . . . . . . . . 77 ■CHAPTER 5 Learning About Visual Basic Exception Handling . . . . . . . . . . . . . . 115 ■CHAPTER 6 Learning the Basics of Object-Oriented Programming . . . . . . . . . 135 ■CHAPTER 7 Learning About Components and Class Hierarchies . . . . . . . . . . . 163 ■CHAPTER 8 Learning About Component-Oriented Architecture . . . . . . . . . . . . 197 ■CHAPTER 9 Learning About Lists, Delegates, and Lambda Expressions . . . 229 ■CHAPTER 10 Learning About Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■CHAPTER 11 Learning About .NET Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 ■CHAPTER 12 Learning About Application Configuration and Dynamic Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 ■CHAPTER 13 Learning About Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 ■CHAPTER 14 Learning About Relational Database Data . . . . . . . . . . . . . . . . . . . . 371 ■CHAPTER 15 Learning About LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 ■CHAPTER 16 Learning About Other Visual Basic Techniques . . . . . . . . . . . . . . . 417 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Gross_938-1FRONT.fm Page v Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Gross_938-1FRONT.fm Page vi Thursday, January 24, 2008 5:21 PM www.it-ebooks.info vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■CHAPTER 1 Ready, Steady, Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Downloading and Installing the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Downloading Visual Basic Express . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Installing Visual Basic Express . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Choosing the Application Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Creating Projects and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Creating the Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Viewing the Source Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Saving the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Running the Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Making the Windows Application Say Hello. . . . . . . . . . . . . . . . . . . . . 8 Adding Comments to the Application . . . . . . . . . . . . . . . . . . . . . . . . . 11 Navigating the User Controls of the Solution . . . . . . . . . . . . . . . . . . . . . . . 12 Creating the Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Adding a Console Application Project to the Solution. . . . . . . . . . . . 12 Making the Console Application Say Hello. . . . . . . . . . . . . . . . . . . . . 13 Setting the Startup Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Running the Console Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Creating the Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Adding a Class Library Project to the Solution . . . . . . . . . . . . . . . . . 14 Moving Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Defining References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Calling Class Library Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Using Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Understanding How the .NET Framework Works . . . . . . . . . . . . . . . . . . . 19 The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Gross_938-1FRONT.fm Page vii Thursday, January 24, 2008 5:21 PM www.it-ebooks.info viii ■CONTENTS ■CHAPTER 2 Learning About .NET Number and Value Types . . . . . . . . . . . 25 Focusing and Organizing Your Development . . . . . . . . . . . . . . . . . . . . . . . 25 Organizing the Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Focusing the Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Implementing the Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Writing the Add() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Writing Code to Test the Add() Method . . . . . . . . . . . . . . . . . . . . . . . 35 Understanding Problems with Numeric Numbers. . . . . . . . . . . . . . . 40 Understanding Numeric and Value Data Types . . . . . . . . . . . . . . . . . . . . . 42 Understanding Value and Reference Types . . . . . . . . . . . . . . . . . . . . 42 Understanding the CLR Numeric Types . . . . . . . . . . . . . . . . . . . . . . . 43 Finishing the Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 ■CHAPTER 3 Learning About String Manipulations . . . . . . . . . . . . . . . . . . . . 51 Organizing the Translation Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Building the Translator Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Creating the Translator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Translating Hello. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Creating the Test Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Answering the Question of Responsibility . . . . . . . . . . . . . . . . . . . . . 55 Investigating the String Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Solving the Extra Whitespace Problem. . . . . . . . . . . . . . . . . . . . . . . . 60 Quoting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Character Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Dealing with Languages and Cultures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Setting Culture and Language in Windows . . . . . . . . . . . . . . . . . . . . 68 Parsing and Processing Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Working with Cultures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 ■CHAPTER 4 Learning About Data Structures, Decisions, and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Understanding the Depth-First Search Algorithm . . . . . . . . . . . . . . . . . . . 77 Implementing User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Declaring Structures and Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Value Type Constraints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Gross_938-1FRONT.fm Page viii Thursday, January 24, 2008 5:21 PM www.it-ebooks.info [...]... Basic 2008 programming language that is used to write applications for the NET Framework With Visual Basic 2008, the NET 3.0 and 3.5 Frameworks are used .NET 3.0 gives you all of the essentials, and NET 3.5 gives you the extras For the examples in this book, you’ll be using Visual Basic 2008 Express Edition, because it’s freely available and has everything you need to get started with Visual Basic 2008. .. Explorer and expanding the Form1 .vb node) Figure 1-3 All files that make up the WindowsApplication project In previous versions, the Form1.Designer .vb file used to be a binary file that you could not edit Now it is a text file that you can edit, but you should not make any changes to this file because it is maintained by the IDE Contained within the Form1.Designer .vb file are the details of how to... NET, there is no relative giving you socks or a sweater It’s excitement all the way! xv www.it-ebooks.info Gross_938-1FRONT.fm Page xvi Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Gross_938-1FRONT.fm Page xvii Thursday, January 24, 2008 5:21 PM About the Technical Reviewer ■ ANDY OLSEN is a freelance developer and consultant based in the UK Andy has been working with NET since beta 1 days,... Microsoft Visual Studio web site (http://msdn2.microsoft.com/en-us/vstudio/default.aspx) for more information If you already have Visual Studio 2008 Professional, you can use that for the examples in this book That edition can do everything that Visual Basic 2008 Express can do, and in fact, has many more options ■Note I personally use Visual Studio Standard or Professional in combination with other... ■ READY, STEADY, G O! Figure 1-1 Selecting Visual Basic 2008 Express Edition Installing Visual Basic Express After you’ve downloaded the setup file, you can start the Visual Basic Express installation During this process, all the pieces of the IDE—about 300MB—are downloaded and installed Follow these steps: 1 On your desktop, double-click the vbsetup.exe file Wait while the setup program loads all... Basic Express automatically generates some source code for it Right-click the Form1 .vb item in the Solution Explorer and select View Code from the context menu The following source code will appear in the area to the left of the Solution Explorer ■Note To shift between the user interface and generated code, right-click Form1 .vb in the Solution Explorer A submenu appears with the options View Code (to see... have just discovered the thrills of surfing and look much cooler than he ever will! xvii www.it-ebooks.info Gross_938-1FRONT.fm Page xviii Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Gross_938-1FRONT.fm Page xix Thursday, January 24, 2008 5:21 PM Introduction T he first computer programming book I read was titled Programming Windows 3.0 by Charles Petzold This was around the time when Microsoft... area will display only a single piece of information, which could be some code, a user interface, or a project As you saw earlier, when you double-click Form1 .vb in the Solution Explorer, the work area displays the form related to the Form1 .vb file Now that you have an idea of how the IDE works, let’s continue with our examples Next up is the console application Creating the Console Application A console...Gross_938-1FRONT.fm Page ix Thursday, January 24, 2008 5:21 PM ■C O N T E N T S Organizing the Search Algorithm 87 Writing the Depth-First Search Code 89 Defining and Implementing the Data Structure... 152 Implementing ActiveCurrencyTrader 152 Implementing HotelCurrencyTrader 155 www.it-ebooks.info ix Gross_938-1FRONT.fm Page x Thursday, January 24, 2008 5:21 PM x ■C O N T E N T S Learning More About Preprocessor Directives, Properties, and the MustOverride Keyword 157 More Preprocessor Directive Details . Approach Pro VB 2008 and the .NET 3.5 Platform, Third Edition Beginning VB 2008: From Novice to Professional Pro WPF with VB 2008: Windows Presentation Foundation in .NET 3.5 Beginning VB 2008 Databases: From. 9 www.it-ebooks.info www.it-ebooks.info Beginning VB 2008 From Novice to Professional ■■■ Christian Gross Gross_938-1FRONT.fm Page i Thursday, January 24, 2008 5:21 PM www.it-ebooks.info Beginning VB 2008: From Novice. Gross Author of Beginning C# 2008: From Novice to Professional US $39.99 Shelve in Programming Languages User level: Beginner–Intermediate Gross Beginning VB 2008 The eXperT’s Voice ® in .neT Beginning VB