ebook img

Pro VB 2005 and the .NET 2.0 Platform PDF

1083 Pages·2006·15.72 MB·English
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 Pro VB 2005 and the .NET 2.0 Platform

5785ch00FM.qxd 3/31/06 3:31 PM Page i Pro VB 2005 and the .NET 2.0 Platform Second Edition Andrew Troelsen 5785ch00FM.qxd 3/31/06 3:31 PM Page ii Pro VB 2005 and the .NET 2.0 Platform Copyright © 2006 by Andrew Troelsen 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-578-7 ISBN-10 (pbk): 1-59059-578-5 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 atrademark symbol with every occurrence of atrademarked 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: Ewan Buckingham Technical Reviewer: Don Reamey Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Kier Thomas, Matt Wade Production Director and Project Manager: Grace Wong Copy Edit Manager: Nicole LeClerc Senior Copy Editor: Ami Knox Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor and Artist: Kinetic Publishing Services, LLC Proofreaders: April Eddy, Lori Bring, Nancy Sixsmith Indexer: Broccoli Information Management 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 [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], 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(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.comin the Source Code section. You will need to answer questions pertaining to this book in order to successfully download the code. 5785ch00FM.qxd 3/31/06 3:31 PM Page iii To my wife Mandy. Thank you babes for supporting me in yet another book. I love you. 5785ch00FM.qxd 3/31/06 3:31 PM Page iv 5785ch00FM.qxd 3/31/06 3:31 PM Page v Contents at a Glance About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix PART 1 ■ ■ ■ Introducing Visual Basic 2005 and the .NET Platform ■ CHAPTER 1 The Philosophy of .NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■ CHAPTER 2 Building Visual Basic 2005 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 PART 2 ■ ■ ■ Visual Basic 2005 Language Fundamentals ■ CHAPTER 3 VB 2005 Programming Constructs,Part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ■ CHAPTER 4 VB 2005 Programming Constructs,Part II. . . . . . . . . . . . . . . . . . . . . . . . . . . 103 PART 3 ■ ■ ■ Core Object-Oriented Programming Techniques ■ CHAPTER 5 Defining Encapsulated Class Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 ■ CHAPTER 6 Understanding Inheritance and Polymorphism. . . . . . . . . . . . . . . . . . . . . . 167 ■ CHAPTER 7 Understanding Structured Exception Handling . . . . . . . . . . . . . . . . . . . . . . 201 ■ CHAPTER 8 Understanding Object Lifetime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 PART 4 ■ ■ ■ Advanced Object-Oriented Programming Techniques ■ CHAPTER 9 Working with Interfaces and Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 ■ CHAPTER 10 Callback Interfaces,Delegates,and Events . . . . . . . . . . . . . . . . . . . . . . . . . 281 ■ CHAPTER 11 Advanced VB 2005 Programming Constructs. . . . . . . . . . . . . . . . . . . . . . . . 309 ■ CHAPTER 12 Understanding Generics and Nullable Data Types . . . . . . . . . . . . . . . . . . . 337 v 5785ch00FM.qxd 3/31/06 3:31 PM Page vi PART 5 ■ ■ ■ Programming with .NET Assemblies ■ CHAPTER 13 Introducing .NET Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 ■ CHAPTER 14 Type Reflection,Late Binding,and Attribute-based Programming. . . . . 407 ■ CHAPTER 15 Processes,AppDomains,Contexts,and CLR Hosts. . . . . . . . . . . . . . . . . . . 441 ■ CHAPTER 16 Building Multithreaded Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 ■ CHAPTER 17 COM and .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 PART 6 ■ ■ ■ Exploring the .NET Base Class Libraries ■ CHAPTER 18 The System.IO Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 ■ CHAPTER 19 Understanding Object Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 ■ CHAPTER 20 The .NET Remoting Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 ■ CHAPTER 21 Building a Better Window with System.Windows.Forms . . . . . . . . . . . . . 611 ■ CHAPTER 22 Rendering Graphical Data with GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 ■ CHAPTER 23 Programming with Windows Forms Controls. . . . . . . . . . . . . . . . . . . . . . . . 707 ■ CHAPTER 24 Database Access with ADO.NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 PART 7 ■ ■ ■ Web Applications and XML Web Services ■ CHAPTER 25 Building ASP.NET 2.0 Web Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837 ■ CHAPTER 26 ASP.NET 2.0 Web Controls,Themes,and Master Pages . . . . . . . . . . . . . . 883 ■ CHAPTER 27 ASP.NET 2.0 State Management Techniques . . . . . . . . . . . . . . . . . . . . . . . . 919 ■ CHAPTER 28 Understanding XML Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991 vi 5785ch00FM.qxd 3/31/06 3:31 PM Page vii Contents About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix PART 1 ■ ■ ■ Introducing Visual Basic 2005 and the .NET Platform ■ CHAPTER 1 The Philosophy of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Understanding the Previous State of Affairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Life As a C/Win32 API Programmer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Life As a C++/MFC Programmer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Life As a Visual Basic 6.0 Programmer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Life As a Java/J2EE Programmer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Life As a COM Programmer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Life As a Windows DNA Programmer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The .NET Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Introducing the Building Blocks of the .NET Platform (the CLR,CTS, and CLS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The Role of the Base Class Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 What Visual Basic 2005 Brings to the Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Additional .NET-Aware Programming Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Life in a Multilanguage World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 An Overview of .NET Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Single-File and Multifile Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Role of the Common Intermediate Language . . . . . . . . . . . . . . . . . . . . . . . . . 12 Benefits of CIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Compiling CIL to Platform-Specific Instructions. . . . . . . . . . . . . . . . . . . . . . 14 The Role of .NET Type Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The Role of the Assembly Manifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Understanding the Common Type System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 CTS Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 CTS Structure Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 CTS Interface Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 CTS Enumeration Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 vii 5785ch00FM.qxd 3/31/06 3:31 PM Page viii viii ■ CONTENTS CTS Delegate Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 CTS Type Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Intrinsic CTS Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Understanding the Common Language Specification . . . . . . . . . . . . . . . . . . . . . . 19 Ensuring CLS Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Understanding the Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . 20 The Assembly/Namespace/Type Distinction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Accessing a Namespace Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . 24 Referencing External Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Using ildasm.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Viewing CIL Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Viewing Type Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Viewing Assembly Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Deploying the .NET Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The Platform-Independent Nature of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 ■ CHAPTER 2 Building Visual Basic 2005 Applications . . . . . . . . . . . . . . . . . . . . . 31 Installing the .NET Framework 2.0 SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The VB 2005 Command-Line Compiler (vbc.exe). . . . . . . . . . . . . . . . . . . . . . . . . . 32 Configuring the VB 2005 Command-Line Compiler. . . . . . . . . . . . . . . . . . . 33 Configuring Additional .NET Command-Line Tools. . . . . . . . . . . . . . . . . . . . 33 Building VB 2005 Applications Using vbc.exe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Referencing External Assemblies Using vbc.exe . . . . . . . . . . . . . . . . . . . . . 35 Compiling Multiple Source Files Using vbc.exe . . . . . . . . . . . . . . . . . . . . . . 36 Referencing Multiple External Assemblies Using vbc.exe. . . . . . . . . . . . . . 37 Working with vbc.exe Response Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The Default Response File (vbc.rsp). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Building .NET Applications Using TextPad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Enabling VB 2005 Keyword Coloring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Configuring the *.vb File Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Hooking Into vbc.exe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Associating Run Commands with Menu Items. . . . . . . . . . . . . . . . . . . . . . . 42 Building .NET Applications Using SharpDevelop. . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Learning the Lay of the Land:SharpDevelop . . . . . . . . . . . . . . . . . . . . . . . . 44 The Project and Classes Scouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 The Assembly Scout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Windows Forms Designers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Building .NET Applications Using Visual Basic 2005 Express. . . . . . . . . . . . . . . . 48 The Big Kahuna:Building .NET Applications Using Visual Studio 2005. . . . . . . . 49 Learning the Lay of the Land:Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . 50 The Solution Explorer Utility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 The My Project Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 5785ch00FM.qxd 3/31/06 3:31 PM Page ix ■ CONTENTS ix The Class View Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 The Object Browser Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Visual Studio 2005 Code Snippet Technology. . . . . . . . . . . . . . . . . . . . . . . . 52 The Visual Class Designer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Object Test Bench . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Integrated Help System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Role of the Visual Basic 6.0 Compatibility Assembly . . . . . . . . . . . . . . . . . . . 58 A Partial Catalogue of Additional .NET Development Tools . . . . . . . . . . . . . . . . . . 60 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 PART 2 ■ ■ ■ Visual Basic 2005 Language Fundamentals ■ CHAPTER 3 VB 2005 Programming Constructs,Part I . . . . . . . . . . . . . . . . . . . . 65 The Role of the Module Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Projects with Multiple Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Modules Are Not Creatable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Renaming Your Initial Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Members of Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 The Role of the Main Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Processing Command-line Arguments Using System.Environment. . . . . . 69 Processing Command-line Arguments with Main() . . . . . . . . . . . . . . . . . . . 70 Main() As a Function (not a Subroutine). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Simulating Command-line Arguments Using Visual Studio 2005. . . . . . . . 71 An Interesting Aside:Some Additional Members of the System.Environment Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The System.Console Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Basic Input and Output with the Console Class . . . . . . . . . . . . . . . . . . . . . . 73 Formatting Console Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 .NET String Formatting Flags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The System Data Types and VB 2005 Shorthand Notation . . . . . . . . . . . . . . . . . . 76 Variable Declaration and Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Default Values of Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The Data Type Class Hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 “New-ing”Intrinsic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Experimenting with Numerical Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . 81 Members of System.Boolean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Members of System.Char. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Parsing Values from String Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Understanding the System.String Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Basic String Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 String Concatenation (and the “Newline”Constant) . . . . . . . . . . . . . . . . . . 84

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.