Programming VB.NET: A Guide for Experienced Programmers GARY CORNELL AND JONATHAN MORRISON Programming VB.NET: A Guide for Experienced Programmers Copyright ©2002 by Gary Cornell 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 (pbk): 1-893115-99-2 Printed and bound in the United States of America 12345678910 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. Editorial Directors: Dan Appleman, Gary Cornell, Jason Gilmore, Karen Watterson Technical Reviewers: Ken Getz, Tim Walton Managing Editor and Production Editor: Grace Wong Copy Editors: Steve Wilent, Tracy Brown Collins Compositor: Susan Glinert Stevens Artist: Allan Rasmussen Indexer: Valerie Haynes Perry Cover Designer: Karl Miyajima Marketing Manager: Stephanie Rodriguez Distributed to the book trade in the United States by Springer-Verlag New York, Inc.,175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany In the United States, phone 1-800-SPRINGER, email orders@springer-ny.com, or visit http://www.springer-ny.com . Outside the United States, fax +49 6221 345229, email orders@springer.de , or visit http://www.springer.de. For information on translations, please contact Apress directly at 901 Grayson Street, Suite 204, Berkeley, CA 94710. Phone 510-549-5930, fax: 510-549-5939, email info@apress.com, or visit http://www.apress.com. 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 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 in the Down- loads section. iii Dedication To the people at Apress: the best group of people I can ever imagine working with. iv Contents at a Glance Dedication iii Contents v Acknowledgments viii About This Book ix Chapter 1 Introduction 1 Chapter 2 The VB .NET IDE: Visual Studio .NET 11 Chapter 3 Expressions, Operators, and Control Flow 47 Chapter 4 Classes and Objects (with a Short Introduction to Object-Oriented Programming) 97 Chapter 5 Inheritance and Interfaces 177 Chapter 6 Event Handling and Delegates 237 Chapter 7 Error Handling the VB .NET Way: Living with Exceptions 265 Chapter 8 Windows Forms, Drawing, and Printing 279 Chapter 9 Input/Output 333 Chapter 10 Multithreading 379 Chapter 11 A Brief Introduction to Database Access with VB .NET 423 Chapter 12 A Brief Overview of ASP .NET 443 Chapter 13 .NET Assemblies, Deployment, and COM Interop 463 Index 479 [...]... Microsoft takes the word platform seriously It even calls Windows a platform 2 Introduction What we mean is that instead of having to write code that worked more or less like this: Select Case kindOfEmployee Case Secretary RaiseSalary 5% Case Manager RaiseSalary 10% Case Programmer RaiseSalary 15% Case Architect RaiseSalary 20% 'etc End Select which was a pain to maintain because whenever you added a new... orientation We feel that the hardest part of dealing with the various changes in VB over the years is not so much in that the IDE changed a little or a lot, or that there were a few new keywords to learn, the pain was in having to change the way that you thought about your VB programs In particular, to take full advantage of VB5 and VB6 , you had to begin to move from an object-based language with an extremely... The languages themselves had relatively small feature sets When VB 3 was released with a way to access databases that required learning a new programming model, the first reaction of many people was, “Oh great, they’ve messed up VB! ” With the benefit of hindsight, the database features added to VB3 were necessary for it to grow beyond the toy stage into a serious tool With VB4 came a limited ability... NET and Java is that with NET you can use any language, as long as you write it for the CLR; with Java, you can write for any platform (theoretically at least—in practice there are some problems) as long as you write in Java We think NET will be successful precisely because it leverages existing language skills 7 Actually, this was not the bottleneck in a lot of cases People can only click so fast and... Collection and you get a For Each for free (see Chapter 5) Automatic Garbage Collection: Fewer Memory Leaks Programmers who used Visual Basic always had a problem with memory leaks from what are called circular references (A circular reference is when you have object A referring to object B and object B referring to object A. ) Assuming this kind of code was not there for a reason, there was no way for the VB. .. advantages and disadvantages of automatic garbage collection Structured Exception Handling All versions of Visual Basic use a form of error handling that dates back to the first Basic written almost 40 years ago To be charitable, it had problems To be uncharitable (but we feel realistic), it is absurd to use On Error GoTo with all the spaghetti code problems that ensue in a modern programming language... With that in mind, let us look at some of the things to watch out for or take advantage of—when switching from VB6 to VB NET The Common Language Runtime Visual Basic has always used a runtime, so it may seem strange to say that the biggest change to VB that comes with NET is the change to a Common Language Runtime (CLR) shared by all NET languages The reason is that while on the surface the CLR is a runtime... line) to a Smart setting (where the body of a loop is automatically indented) as good programming style would indicate (You can select tabs and apply smart formatting after the fact using Ctrl+K, Ctrl +F or via the Edit|Advanced|Format Selection option Note that when you are using Smart Tabs, selecting a region and pressing Shift+Tab (to manage indents) also reformats.) Figure 2-10 The Options dialog box... part of a specific solution Think of a solution as the container that holds all information needed to compile your code into a usable form This means a solution will contain one or more projects; various associated files such as images, resource files, metadata (data 12 The VB NET IDE: Visual Studio NET that describes the data in your program), XML documentation; and just about anything else you can... QuickBasic While we certainly feel there is a lot of knowledge you can carry over from your Visual Basic for Windows programming experience, there are as many changes in programming for the 1 Read BASIC as meaning “very readable-with no ugly braces.…” 2 Its code name, “Thunder,” appeared on one of the rarest T-shirts around—it says “Thunder unlocks Windows” with a lightning bolt image You may also see a . Programming VB. NET: A Guide for Experienced Programmers GARY CORNELL AND JONATHAN MORRISON Programming VB. NET: A Guide for Experienced Programmers Copyright ©2002 by Gary Cornell All rights. relatively easy languages to learn. The languages themselves had rela- tively small feature sets. When VB 3 was released with a way to access databases that required learning a new programming model,. RaiseSalary 5% Case Manager RaiseSalary 10% Case Programmer RaiseSalary 15% Case Architect RaiseSalary 20% 'etc End Select which was a pain to maintain because whenever you added a new type of