Visual Basic .NET and the .NET Platform: an Advanced Guide
Total Page:16
File Type:pdf, Size:1020Kb
Visual Basic .NET and the .NET Platform: An Advanced Guide ANDREWTROELSEN APress Media, LLC Visual Basic .NET and the .NET Platform: An Advanced Guide Copyright ©2002 by Andrew'Ii'oelsen Originally published by Apress in 2002 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 978-1-893115-26-2 ISBN 978-1-4302-0849-5 (eBook) DOI 10.1007/978-1-4302-0849-5 Trademarked names may appear in this book. Rather than use a trademark sym bol 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: Pamela Fanstill, Rob Macdonald, Dan Appleman, William Oellermann Managing Editor: Grace Wong Copy Editors: Anne Friedman, Nicole LeClerc Production Editors: Janet Vail with Kari Brooks and Anne Friedman Composition and Art Services: Impressions Book and Journal Services, Inc. Indexer: Carol Burbo Cover Designer: Tom Debolski Marketing Manager: Stephanie Rodriguez Distributed to the book trade in the United States by Springer-Verlag New York, lnc.,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: I lwww. springer-ny. com. Outside the United States, fax +49 6221 345229, email orders@springer. de, or visit http: I lwww. 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 [email protected], or visit http:llwww.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, nei ther the author nor Apress shall have any liability to any person or entity with re spect 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: I lwww. a press. com in the Downloads section. You will need to answer questions pertaining to this book in order to successfully download the code. This book is dedicated to the memory ofthe thousands ofpeople who lost their lives during the tragic events that took place on September 11, 2001. Of exactly equal importance, this book is dedicated to each and every individual who worked around the clock, without limits, to bring some degree ofnormalcy back to New York, Pennsylvania, and Washington D. C. Thank you for your strength, inspiration, and relentless commitment. Thank you for engaging the unimaginable. Contents at a Glance What You Need to Use This Book ................................ .xv Acknowledgments ................................................ .xxii Introduction ................................................... .xviii Chapter 1 The Philosophy of • NET .............................. .. 1 Chapter 2 Building Visual Basic • NET Applications ......... .41 Chapter 3 VB .NET Language Fundamentals ...................... 77 Chapter 4 Object-Oriented Programming with VB .NET ...... .. 159 Chapter 5 Interfaces and Collections ........................ .227 Chapter 6 Delegates, Events, and Callback Interfaces .....263 Chapter 1 Assemblies, Threads, and AppDomains ............ .297 Chapter 8 Type Reflection and Attribute-Based Programming .................. .385 Chapter 9 Building a Better Window (Introducing Windows Forms) ....................... .421 Chapter 10 A Better Painting Framework (GDI+) ............ .507 Chapter 11 Programming with Windows Form Controls ........ .589 Chapter 12 Input, Output, and Object Serialization ........ 677 Chapter 13 Interacting with Unmanaged Code ................ 733 Chapter 14 Data Access with ADO.NET ........................ .. 791 Chapter 15 Web Development and ASP. NET .................... .. 877 Chapter 16 Building (and Understanding) Web Services .... .971 Index ............................................................... .1019 iv Contents What You Need to Use This Book .......................... .xv Acknowledgments ............................................. .xxii Introduction ................................................ .xviii Chapter 1 The Philosophy of • NET ......................... .1 Understanding the Current State of Affairs ...................... .1 The •NET Solution ................................................... .5 Building Blocks of .NET (CLR, CTS, and CLS) ......................6 .NET -Aware Programming Languages ................................... 7 What VB • NET Brings to the Table ...................................9 An Overview of .NET Binaries (aka Assemblies) ................. .10 The Role of Microsoft Intermediate Language .................... .12 The Role of Metadata ............................................... .15 Compiling IL to Platform-Specific Instructions ................ .16 • NET Types and • NET Names paces ................................... .17 Understanding the Common Language Runtime ...................... .17 Understanding the Common Type System ............................ .20 Understanding the Common Language Specification ............... .24 Working with Namespaces ........................................... .26 A Tour of the • NET Name spaces ..................................... .27 Referencing External Assemblies .................................. .30 Increasing Your Namespace Nomenclature .......................... .31 Summary .............................................................. .39 Chapter 2 Building Visual Basic .NET Applications ..................................... .41 Building VB .NET Applications Using the Command Line Compiler .............................. .41 Building Your First VB .NET Application ........................ .43 v Contents Building VB .NET Applications Using the Visual Studio • NET IDE ............................. .49 Building Your VS .NET Test Application ........................ ..53 Configuring a VB .NET Project .................................... .55 The Properties Window .............................................. .59 Adding Some Code ................................................... .60 What Happened to the World of VB 6.0? .......................... .. 68 Upgrading VB 6.0 Projects into VB .NET ........................ .. 72 The Microsoft.VisualBasic.Compatability.VB6 Namespace ......... 75 Should You Upgrade? ................................................. 75 Summary .............................................................. 76 Chapter 3 VB • NET Language Fundamentals ............ .77 The Role of the Module Type ....................................... 77 The Main Method ..................................................... .80 Data Types and Variable Declarations ............................ .81 Experimenting with the System Data Types ....................... .85 Data Type Conversions .............................................. .88 Concatenation Operators ........................................... .89 Basic Input and Output with the Console Class ................. .90 Introducing VB • NET String Formatting .......................... .. 91 Anatomy of a Simple VB . NET Class .............................. .. 93 Creating Objects: Constructor Basics ............................ .94 Default Values .......................................................99 Defining Program Constants ....................................... .100 Understanding Value Types and Reference Types ................ .102 The Master Node: System.Object .................................. .107 VB • NET Iteration Constructs .................................... .113 VB .NET Control Flow Constructs ................................ .. 117 Additional VB .NET Operators .................................... .119 Defining Custom Class Methods .................................. .. 121 Method Parameter Modifiers ·(ByRef and ByVal) ................ ..127 Shared Methods ..................................................... .132 Static (Not Shared) Data ........................................ .. 137 Array Manipulation in VB • NET .................................. ..139 String Manipulation in VB .NET .................................. .143 VB .NET Enumerations .............................................. .146 Defining Structures in VB • NET .................................. .151 Defining Custom Namespaces ....................................... .153 Summary ............................................................. .158 vi Contents Chapter 4 Object-Oriented Programming with VB . NET .................................... .159 A Catalog of VB • NET 00-Centric Keywords ...................... .159 Formal Definition of the VB .NET Class ........................ .161 Member Overloading ................................................ .165 Defining the Default Public Interface .......................... .166 Pillars of OOP ..................................................... .168 The First Pillar: VB .NET's Encapsulation Services .......... .173 Pseudo-Encapsulation: Creating Read-Only Fields .............. .181