Programming Microsoft Visual Basic 2005: The Language byFrancesco Balena Microsoft Press 2006 (1024 pages) ISBN:0735621837 Use this comprehensive guide to get the essential, straightforward information you need to master the core capabilities of Visual Basic 2005 Table of Contents Programming Microsoft Visual Basic 2005—The Language Introduction Part I - The Basics Chapter 1 - Introducing the Microsoft NET Framework Chapter 2 - Basic Language Concepts Chapter 3 - Control Flow and Error Handling Chapter 4 - Using Visual Studio 2005 Chapter 5 - Debugging Visual Basic Applications Part II - Object-Oriented Programming Chapter 6 - Class Fundamentals Chapter 7 - Delegates and Events Chapter 8 - Inheritance Chapter 9 - Object Lifetime Chapter 10 - Interfaces Chapter 11 - Generics Part III - Working with the NET Framework Chapter 12 - NET Basic Types Chapter 13 - Arrays and Collections Chapter 14 - Regular Expressions Chapter 15 - Files, Directories, and Streams Chapter 16 - The My Namespace Chapter 17 - Assemblies and Resources Part IV - Advanced Topics Chapter 18 - Reflection Chapter 19 - Custom Attributes Chapter 20 - Threads Chapter 21 - Serialization Chapter 22 - PInvoke and COM Interop Index List of Figures List of Tables Back Cover Get the essential, straightforward information you need to master the core capabilities of Visual Basic 2005 Focusing on the language and the Microsoft NET Framework 2.0 base class library, a well-known authority on Visual Basic provides both new and experienced developers the pragmatic guidance and examples they need to build innovative solutions Discover how to: Compress files, manipulate ACLs, use semaphores, and exploit other new capabilities in the NET Framework 2.0 Implement generics to define a type-safe data structure Use inheritance, polymorphism, interfaces, delegates, and attributes to write flexible applications Use the My namespace to perform common tasks more easily Work with the new editing and debugging features of Microsoft Visual Studio 2005 Master regular expressions and perform complex text searches and input validation Take advantage of streams, serialization, and threading techniques Implement advanced programming techniques based on custom attributes, reflection, and onthe-fly Interact with legacy code by using Pinvoke and COM Interop Understand key differences from Visual Basic 6.0 About the Author Francesco Balena has been programming with Visual Basic since version 1.0 He is the author of many Microsoft Press titles, including the widely acclaimed prior editions of this book Francesco is a cofounder of Code Architects srl, an Italian software company that specializes in using Microsoft technologies to create enterprise-level solutions and programming tools In addition, he is a regional director for MSDN Italy, and a popular speaker at developer conferences Programming Microsoft Visual Basic 2005—The Language Francesco Balena (Code Architects) Published By Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2006 by Francesco Balena ISBN:0735621837 All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher Library of Congress Control Number 2005936844 Printed and bound in the United States of America 1 2 3 4 5 6 7 8 9 QWT 9 8 7 6 5 Distributed in Canada by H.B Fenn and Company Ltd A CIP catalogue record for this book is available from the British Library Microsoft Press books are available through booksellers and distributors worldwide For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/learning/ Send comments to mspinput@microsoft.com Microsoft, Active Directory, ActiveX, Authenticode, Excel, IntelliSense, JScript, Microsoft Press, MSDN, Visual Basic, Visual C#, Visual C++, Visual Studio, the Visual Studio logo, Win32, Windows, Windows NT, and Windows Server are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred This book expresses the author's views and opinions The information contained in this book is provided without any express, statutory, or implied warranties Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book Acquisitions Editor: Ben Ryan Project Editor: Kathleen Atkins Technical Reviewer: Jack Beaudry Copy Editor: Christina Palaia Indexer: Lynn Armstrong Body Part No X11-53584 Acknowledgments First of all, I am glad to have the opportunity to thank my friend Giuseppe Dimauro, for helping me better understand the many secrets behind the Microsoft Windows and NET Framework platforms Even more important, in these six months Giuseppe had to run Code Architects, the software and consulting company that we founded a few years ago, virtually by himself I am happy I can finally go back to the fight! Enrico Sabbadin is a true expert in n-tier enterprise applications, COM+, and security, and I feel very lucky he could review all the chapters while I was writing them He provided many valuable suggestions I'd like to thank Marco Bellinaso and Alberto Falossi, two pillars of the Code Architects team While I was busy on this book, Marco did a marvelous job with our U.S Web site (http://www.dotnet2themax.com) and Alberto did the same with our Italian Web site (http://www.dotnet2themax.it) Next come all the wonderful people at Microsoft Press Kathleen Atkins has taken care of all my books, and she is so marvelous that I can't even think of writing a book with another editor Jack Beaudry helped in finding typos and mistakes in code, whereas Christina Palaia did the same with my prose English isn't my mother tongue, so you can imagine what kind of job she had to face This book would be very different—or might have never been printed at all—if it weren't for Ben Ryan, my acquisitions editor, who accepted my proposal and offered many suggestions on how to improve the original structure Finally, my family Living with a full-time coder and writer isn't easy Only a special woman like my wife, Adriana, can do it in such a delightful way And only a special boy like my son, Andrea, has permission to break into my room to talk about his favorite movie or to ask for help with his homework I can't thank both of you enough for always reminding me that the most important things in life can't be rendered as a sequence of bytes, regardless of the programming language you're using Francesco Balena Francesco Balena began his software studies in the late 1970s and had to fight for a while with huge IBM mainframes and tons of punched cards while he waited for the PC to be invented From those good old days— when the word megabyte made little sense and gigabyte was pure blasphemy—he retained his taste for writing the most efficient and resource-aware code possible In more recent years, Francesco has become a contributing editor and member of the Technical Advisory Board of Visual Studio Magazine (formerly Visual Basic Programmer's Journal), for which he has written more than 80 articles and columns He's the author of the best-sellers Programming Microsoft Visual Basic 6.0, Programming Microsoft Visual Basic NET, and Programming Microsoft Visual Basic NET Version 2003, as well as coauthor of Applied NET Framework Programming in Microsoft Visual Basic NET (with Jeffrey Richter) and Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C# Developers (with Giuseppe Dimauro), all from Microsoft Press Francesco teaches Microsoft Visual Basic and C# courses in the United States and Europe and speaks at developer conferences such as VSLive!, SQL2TheMax, WinDev, and WinSummit He is the founder of the popular NET-2-The-Max site (http://www.dotnet2themax.com), where you can find articles, tips, routines, and updates as they occur for this book Francesco is the lead author of VBMaximizer, a best-seller add-in for Microsoft Visual Basic 6 that has won an award from readers of Visual Studio Magazine, and he is coauthor of CodeBox for NET (a code repository tool) and Form Maximizer for NET (a set of Microsoft Windows Forms controls) Since 2002, he has been the Microsoft MSDN Regional Director for Italy and is cofounder of Code Architects, an Italian company that specializes exclusively in Microsoft NET Framework programming and training and that consults for many large software companies in Italy, including Microsoft Francesco lives in Bari, Italy, with his wife, Adriana and his son, Andrea, but spends a lot of his time abroad In a previous life, he had many good times playing his alto sax with big bands and jazz combos until he found that computer programming can be just as much fun and doesn't require that he be awake and blowing until 4 A.M each and every night Only later did he realize that—to write code and meet deadlines—he wouldn't be going to sleep before 4 A.M anyway, but it was too late to change his mind Introduction Finding a reference book on Microsoft Visual Basic 2005 is easy By the time this book reaches the bookstores, you'll find plenty of Visual Basic books on the shelves Why should you buy this one? What makes this book different? When I began to write this book, I asked myself similar questions How can a book compete with Microsoft Visual Studio manuals and all the samples and tips you can find on the Internet? To answer this question I need to take a short historical detour Where Visual Basic is Coming From and Heading To I have been teaching Visual Basic since the early 1990s, well before it became Visual Basic NET, and I taught (and wrote about) Microsoft QuickBasic before then I have seen this language evolve from the time that you were practically compelled to use GoTo statements to make things work up to today's phenomenal object-oriented features Everything has changed in these 20 years, except one thing: developers have always underutilized—or even misused—this language For instance, Microsoft Visual Basic 4 was the first version to offer the ability to define classes, yet very few developers actually used classes in their applications The few who did, however, were able to catch the Microsoft NET Framework wave easier than their colleagues were and could deliver more powerful Visual Basic NET applications in less time Another example: Microsoft Visual Basic 6 developers were able to access a database through ActiveX Data Objects (ADO) using client-side recordsets in disconnected mode, but many preferred to ignore this feature and continued to work with easier-to-use but less scalable serverside cursors (And they had serious problems when writing large client/server applications.) Versions 2002 and 2003 of Visual Basic NET are very powerful development platforms, yet I see that many developers are missing their full potential For example, features such as threading, reflection, and custom attributes can really revolutionize the way you write applications, but only a minority of programmers leverage them The gap between what the language offers and what most developers actually use has Chapter 19: Custom Attributes Figure 19-1: The application's main form Figure 19-2: The CalculatorForm, which lets you perform simple calculations Figure 19-3: The new SamplePlugin project, which extends the CalculatorForm to account for a discount percentage Figure 19-4: All the types in the DataObjectLibrary project, the core of the CAP framework Figure 19-5: Two steps in the Data Source Configuration Wizard Figure 19-6: For each DataTable in the DataSet, you can define one or more custom commands, such as FillByCustomerID Figure 19-7: Two steps of the TableAdapter Query Configuration Wizard Figure 19-8: The main form of the DemoClient project Figure 19-9: The demo application filtering customers by their country Chapter 20: Threads Figure 20-1: The Windows Task Manager utility Figure 20-2: The dialog box that NET applications display by default when an unhandled exception occurs Figure 20-3: The Debug Location toolbar, which displays the thread name and other information about the running process Figure 20-4: The Threads window, which lists all threads and lets you freeze and restart them Figure 20-5: The Breakpoint Filter dialog box Figure 20-6: The Performance utility, which lets you see several statistics regarding CLR threads Figure 20-7: The console window clearly showing that writing a space plus the thread number isn't an atomic operation Figure 20-8: A demo application using the BackgroundWorker component to search for files in the background Chapter 22: PInvoke and COM Interop Figure 22-1: The standard window that the SHFileOperation API function displays when copying files Figure 22-2: The demo program, which uses the Windows API to display the tree of all open windows Figure 22-3: The Runtime Callable Wrapper object Figure 22-4: The COM tab of the Add Reference dialog box Figure 22-5: The Isolated property of an interop assembly added to the references of a project Figure 22-6: The COM Callable Wrapper object List of Tables Chapter 2: Basic Language Concepts Table 2-1: Data Types Supported Under Visual Basic 2005 and Their Corresponding NET Framework Types Chapter 4: Using Visual Studio 2005 Table 4-1: Template Parameters Chapter 6: Class Fundamentals Table 6-1: Suggested Names for Methods Alternative to Operator Overloads Chapter 8: Inheritance Table 8-1: The Effect of Class Scope and Constructor Scope on a Class's Ability to Be Instantiated or Used as a Base Class Chapter 12: NET Basic Types Table 12-1: Special Formatting Characters in Custom Formatting Strings Table 12-2: Standard Formats for Date and Time Values Table 12-3: Character Sequences That Can Be Used in Custom Date and Time Formats Chapter 14: Regular Expressions Table 14-1: The Regular Expression Language Table 14-2: Regular Expression Options Table 14-3: Common Regular Expression Patterns Chapter 15: Files, Directories, and Streams Table 15-1: Values of the FileSystemRights Enumerated Type Chapter 16: The My Namespace Table 16-1: The My.Application Object Table 16-2: Child Objects of the My.Computer Object Table 16-3: The My.User Object Chapter 17: Assemblies and Resources Table 17-1: Assembly-Level Attributes Chapter 20: Threads Table 20-1: The Possible Values for the ThreadState Property Chapter 22: PInvoke and COM Interop Table 22-1: UnmanagedType Enumerated Values ... Programming Microsoft Visual Basic 2005: The Language is broadly organized in four parts The first three chapters of Part I cover the language basics If you aren't new to Visual Basic, you might skip them, even though you might find... visiting the preceding sites, please send them to Microsoft Press by email: mspinput @microsoft. com Or by postal mail: Microsoft Press Attn: Programming Microsoft Visual Basic 2005 Editor One Microsoft Way... I have been teaching Visual Basic since the early 1990s, well before it became Visual Basic NET, and I taught (and wrote about) Microsoft QuickBasic before then I have seen this language evolve from the time