Visual basic .net power tools

Embed Size (px)

DESCRIPTION

Visual basic .net power tools

Citation preview

  • 1.Add print and preview capabilities to basic Window controlsPRNListView: Add preview and print capabilities to the ListView control (Chapter 7)PRNTextBox Control, an enhanced TextBox control that provides its own Print and Preview methods. The control prints its text using the page settings provided by the user, using the controls Font and WordWrap properties (Chapter 7).

2. Visual Basic .Net Power Tools Evangelos Petroutsos and Richard MansfieldSan Francisco London 3. Associate Publisher: Joel Fugazzotto Acquisitions and Developmental Editor: Tom Cirtin Production Editor: Leslie E.H. Light Technical Editor: Greg Guntle Copyeditor: Suzanne Goraj Compositor: Maureen Forys, Happenstance Type-O-Rama Graphic Illustrator: Jeffery Wilson, Happenstance Type-O-Rama Proofreaders: Nancy Riddiough, Amey Garber, Emily Hsuan, Sarah Tannehill, and Laurie OConnell Indexer: Nancy Guenther Book Designer: Maureen Forys, Happenstance Type-O-Rama Cover Designer: Richard Miller, Calyx Designs Cover Illustrator: Richard Miller, Calyx Designs Copyright 2004 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. The author(s) created reusable code in this publication expressly for reuse by readers. Sybex grants readers limited permission to reuse the code found in this publication or its accompanying CD-ROM so long as the author(s) are attributed in any application containing the reusable code and the code itself is never distributed, posted online by electronic transmission, sold, or commercially exploited as a stand-alone product. Aside from this specific exception concerning reusable code, no part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher. Library of Congress Card Number: 2003109130 ISBN: 0-7821-4242-7 SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries. Screen reproductions produced with FullShot 99. FullShot 99 19911999 Inbit Incorporated. All rights reserved. FullShot is a trademark of Inbit Incorporated. Internet screen shots using Microsoft Internet Explorer reprinted by permission from Microsoft Corporation. TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible. Portions of the manuscript may be based upon pre-release versions supplied by software manufacturer(s). The author and the publisher make no representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kind including but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly from this book. Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 4. Software License Agreement: Terms and Conditions The media and/or any online materials accompanying this book that are available now or in the future contain programs and/or text files (the Software) to be used in connection with the book. SYBEX hereby grants to you a license to use the Software, subject to the terms that follow. Your purchase, acceptance, or use of the Software will constitute your acceptance of such terms. The Software compilation is the property of SYBEX unless otherwise indicated and is protected by copyright to SYBEX or other copyright owner(s) as indicated in the media files (the Owner(s)). You are hereby granted a single-user license to use the Software for your personal, noncommercial use only. You may not reproduce, sell, distribute, publish, circulate, or commercially exploit the Software, or any portion thereof, without the written consent of SYBEX and the specific copyright owner(s) of any component software included on this media. In the event that the Software or components include specific license requirements or end-user agreements, statements of condition, disclaimers, limitations or warranties (End-User License), those EndUser Licenses supersede the terms and conditions herein as to that particular Software component. Your purchase, acceptance, or use of the Software will constitute your acceptance of such End-User Licenses. By purchase, use or acceptance of the Software you further agree to comply with all export laws and regulations of the United States as such laws and regulations may exist from time to time.REUSABLE CODE IN THIS BOOK The author(s) created reusable code in this publication expressly for reuse by readers. Sybex grants readers limited permission to reuse the code found in this publication, its accompanying CD-ROM or available for download from our website so long as the author(s) are attributed in any application containing the reusable code and the code itself is never distributed, posted online by electronic transmission, sold, or commercially exploited as a stand-alone product.SOFTWARE SUPPORT Components of the supplemental Software and any offers associated with them may be supported by the specific Owner(s) of that material, but they are not supported by SYBEX. Information regarding any available support may be obtained from the Owner(s) using the information provided in the appropriate read.me files or listed elsewhere on the media. Should the manufacturer(s) or other Owner(s) cease to offer support or decline to honor any offer, SYBEX bears no responsibility. This notice concerning support for the Software is provided for your information only. SYBEX is not the agent or principal of the Owner(s), and SYBEX is in no way responsible for providing any support for the Software, nor is it liable or responsible for any support provided, or not provided, by the Owner(s).WARRANTY SYBEX warrants the enclosed media to be free of physical defects for a period of ninety (90) days after purchase. The Software is not availablefrom SYBEX in any other form or media than that enclosed herein or posted to www.sybex.com. If you discover a defect in the media during this warranty period, you may obtain a replacement of identical format at no charge by sending the defective media, postage prepaid, with proof of purchase to: SYBEX Inc. Product Support Department 1151 Marina Village Parkway Alameda, CA 94501 Web: http://www.sybex.com After the 90-day period, you can obtain replacement media of identical format by sending us the defective disk, proof of purchase, and a check or money order for $10, payable to SYBEX.DISCLAIMER SYBEX makes no warranty or representation, either expressed or implied, with respect to the Software or its contents, quality, performance, merchantability, or fitness for a particular purpose. In no event will SYBEX, its distributors, or dealers be liable to you or any other party for direct, indirect, special, incidental, consequential, or other damages arising out of the use of or inability to use the Software or its contents even if advised of the possibility of such damage. In the event that the Software includes an online update feature, SYBEX further disclaims any obligation to provide this feature for any specific duration other than the initial posting. The exclusion of implied warranties is not permitted by some states. Therefore, the above exclusion may not apply to you. This warranty provides you with specific legal rights; there may be other rights that you may have that vary from state to state. The pricing of the book with the Software by SYBEX reflects the allocation of risk and limitations on liability contained in this agreement of Terms and Conditions.SHAREWARE DISTRIBUTION This Software may contain various programs that are distributed as shareware. Copyright laws apply to both shareware and ordinary commercial software, and the copyright Owner(s) retains all rights. If you try a shareware program and continue using it, you are expected to register it. Individual programs differ on details of trial periods, registration, and payment. Please observe the requirements stated in appropriate files.COPY PROTECTION The Software in whole or in part may or may not be copy-protected or encrypted. However, in all cases, reselling or redistributing these files without authorization is expressly forbidden except as specifically provided for by the Owner(s) therein. 5. To Nefeli Evangelos Petroutsos To David Lee and Cliff Richard Mansfield 6. Acknowledgments We were fortunate to have several smart, thoughtful editors assist us in polishing this manuscript. First, wed like to thank Development Editor Tom Cirtin. He deserves credit for his discernment, and the high quality of his editing. Hes very good at dealing with his authors, and equally skilled at raising important questions and improving their chapters. Technical Editor Greg Guntle carefully reviewed the manuscript and made many useful suggestions, caught a number of inconsistencies, and helped improve several code examples. Production Editor Leslie Light ensured that this book moved smoothly through production and was most helpful with suggestions about the graphics, drawings, and screen shots. Suzanne Goraj, copy editor, combed through every line of our text, making improvements throughout. To all these, and the other good people at Sybex who contributed to this book, our thanks for the intelligence and care that they brought to this book. In addition, the authors would like to give special thanks to their agents, Matt Wagner and David Fugate, of Waterside Productions, whose contributions to the authors careers goes above and beyond the call of duty. 7. Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter 1 Understanding the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2 New Ways of Doing Traditional Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 3 Serialization Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Chapter 4 Leveraging Microsoft Office in Your Applications . . . . . . . . . . . . . . . . . 93 Chapter 5 Understanding .NET Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Chapter 6 Encryption, Hashing, and Creating Keys . . . . . . . . . . . . . . . . . . . . . . . 139 Chapter 7 Advanced Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Chapter 8 Upon Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Chapter 9 Building Bug-Free and Robust Applications . . . . . . . . . . . . . . . . . . . . . 215 Chapter 10 Deploying Windows Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Chapter 11 Building Data-Driven Web Applications . . . . . . . . . . . . . . . . . . . . . . 271 Chapter 12 Peer-to-Peer Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Chapter 13 Advanced Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Chapter 14 Building Asynchronous Applications with Message Queues . . . . . . . . 341 Chapter 15 Practical ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Chapter 16 Building Middle-Tier Components . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Chapter 17 Exploring XML Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Chapter 18 Designing Data-Driven Windows Applications . . . . . . . . . . . . . . . . . 505 Chapter 19 Working with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 543 Chapter 20 Advanced Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 Chapter 21 Designing the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 Chapter 22 Using the .NET Compact Framework and Its Emerging Technologies . . 643 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 8. Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter 1 Understanding the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . 1 Why Read This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Help! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Grappling with Framework Class Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Hunt for a Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Why Two Ways? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 About Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Assemblies Three Ways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Understanding Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 About System.Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 MemberWiseClone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Equals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 ReferenceEquals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Main Point about Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 GetHashCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 GetType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 ToString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Strong Typing Weakens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Is Color a Data Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Exploiting the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 A Useful Class View Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 A Brief Lexicon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Chapter 2 New Ways of Doing Traditional Jobs Clipboard Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Control Arrays . . . . . . . . . . . . . . . . . . Multiple Handles . . . . . . . . . . . . . . . . . . . . . . . . . Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Zero-Based Collections (Sometimes) . . . . . . . . . . . . Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . Array Search and Sort Methods . . . . . . . . . . . . . . . Customized Sorting . . . . . . . . . . . . . . . . . . . . . . . . Many Properties and Methods . . . . . . . . . . . . . . . . The Flexible ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . Mass Manipulation . . . . . . . . . . . . . . . . . . . . . . . . Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enumerators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using HashTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . New Date/Time Techniques . . . . . . . . . . . . . . . . . . . . Adding Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 23 . . . . . . . . . . . . . . . . . . . . . . . . 23 . . . . . . . . . . . . . . . . . . . . . . . . 24 . . . . . . . . . . . . . . . . . . . . . . . . 27 . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . . . . 31 . . . . . . . . . . . . . . . . . . . . . . . . 33 . . . . . . . . . . . . . . . . . . . . . . . . 34 . . . . . . . . . . . . . . . . . . . . . . . . 35 . . . . . . . . . . . . . . . . . . . . . . . . 36 . . . . . . . . . . . . . . . . . . . . . . . . 36 . . . . . . . . . . . . . . . . . . . . . . . . 37 . . . . . . . . . . . . . . . . . . . . . . . . 37 . . . . . . . . . . . . . . . . . . . . . . . . 38 . . . . . . . . . . . . . . . . . . . . . . . . 39 9. xCONTENTSUsing the Old-Style Double DateTime Data Type Finding Days in a Month . . . . . . . . . . . . . . . . . . File I/O (Streaming) . . . . . . . . . . . . . . . . . . . . . . . . Reading a File . . . . . . . . . . . . . . . . . . . . . . . . . . How Do You Know Youre at the End? . . . . . . . . Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . Form References: Communication Between Forms . . . Using Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . Runtime Handles . . . . . . . . . . . . . . . . . . . . . . . . Detecting Key Presses . . . . . . . . . . . . . . . . . . . . Loading Graphics with LoadPicture . . . . . . . . . . . . . Managing the Registry . . . . . . . . . . . . . . . . . . . . . . . Writing to the Registry . . . . . . . . . . . . . . . . . . . Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . Filling an Array . . . . . . . . . . . . . . . . . . . . . . . . . SendKeys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Serializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mixing Types into the Same Stream . . . . . . . . . . Reading Back Mixed Data . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .39 39 39 40 41 42 45 46 47 47 48 48 49 49 52 53 53 55 56 57Chapter 3 Serialization Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 How Serialization Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Serialization Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Basic Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Creating Serializable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The ClassSerializer Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 XML Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The NETConfigFiles Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Custom Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The SerializationContext Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Serializing SQL Server Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The NWOrders Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Chapter 4 Leveraging Microsoft Office in Your Applications . . . . . . . . . . . . . 93 Using Words Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Spell-Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Sending a Fax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Loading Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Finding Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Feeding Individual Strings and Specialized Formatting . . . . . . . . . . . . . . . . . . . . . . . 102 Text Manipulation and Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Replacing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Borrowing Words Printing Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Using Outlook Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 10. CONTENTSAccessing Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Evaluating Math Expressions . . . . . . . . . . . . . . . . . . . . . . Pretty Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sending Data to Excel, Formatting, Calculating, and Saving Retrieving Data from Excel . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .111 111 113 113 115 117Chapter 5 Understanding .NET Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Security: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 .NETs Strong Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Users and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The Principal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Code-Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Everyone Must Agree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Understanding Code-Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 CAS Config Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Software Restriction Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Managing .NET Security Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Using the Framework Configuration Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Programming for Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Chapter 6 Encryption, Hashing, and Creating Keys The Main Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hashing a Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hashing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Encrypting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding Initialization Vectors . . . . . . . . . . . . . . . . Discovering Key Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . Hashing while Encrypting . . . . . . . . . . . . . . . . . . . . . . . Asymmetrical Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . How RSA Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Encrypting and Decrypting Using RSA . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 139 . . . . . . . . . . . . . . . . . . . . 139 . . . . . . . . . . . . . . . . . . . . 140 . . . . . . . . . . . . . . . . . . . . 142 . . . . . . . . . . . . . . . . . . . . 143 . . . . . . . . . . . . . . . . . . . . 146 . . . . . . . . . . . . . . . . . . . . 147 . . . . . . . . . . . . . . . . . . . . 147 . . . . . . . . . . . . . . . . . . . . 151 . . . . . . . . . . . . . . . . . . . . 153 . . . . . . . . . . . . . . . . . . . . 156 . . . . . . . . . . . . . . . . . . . . 158Chapter 7 Advanced Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Printing in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Printer and Page Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 The PageSettings Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 The PrinterSettings Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 The Printing Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Page Layout and Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 The DrawString and MeasureString Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Printing Plain Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Printing Tabular Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 A PrintScreen Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189xi 11. xiiCONTENTSChapter 8 Upon Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 What Use Is It? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Understanding Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Getting a Grip on Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Containers within Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Security Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Seeing Reflections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Accessing a Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Accessing the Current Projects Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Accessing a Loaded Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Loading a File from an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Loading an Assembly from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Getting the Methods in a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 More about Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Accessing Specific Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Searching for Members or Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Executing Discovered Code with CreateInstance and Invoke . . . . . . . . . . . . . . . . . . . . . . 208 Emission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Chapter 9 Building Bug-Free and Robust Applications Structured Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . The Finally Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ReadWriteFile Project . . . . . . . . . . . . . . . . . . . . . . . . . Resuming Statements That Failed . . . . . . . . . . . . . . . . . . . . The Exception Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Throwing Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . Bypassing Error Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Types of Programming Errors . . . . . . . . . . . . . . . . . . . . . . Dealing with Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 215 . . . . . . . . . . . . . . . . . . 216 . . . . . . . . . . . . . . . . . . 219 . . . . . . . . . . . . . . . . . . 219 . . . . . . . . . . . . . . . . . . 224 . . . . . . . . . . . . . . . . . . 225 . . . . . . . . . . . . . . . . . . 228 . . . . . . . . . . . . . . . . . . 230 . . . . . . . . . . . . . . . . . . 231 . . . . . . . . . . . . . . . . . . 232 . . . . . . . . . . . . . . . . . . 237 . . . . . . . . . . . . . . . . . . 241Chapter 10 Deploying Windows Applications . . . . . . . . . . . . . . . . . . . . . . . 243 Installing the .NET Framework Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 XCopy Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Internet Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Preparing for Internet-Based Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Deploying a Windows Application on a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . 249 Code Access Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Running the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Downloading Assemblies on Demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Deploying with Windows Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Creating a Windows Installer Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Using the File System Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Creating Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 12. CONTENTSThe Registry Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Using the User Interface Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Chapter 11 Building Data-Driven Web Applications New Features in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . Sending Entire Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying Data on a WebForm . . . . . . . . . . . . . . . . . . . . . . The DataList, Repeater, and Templates . . . . . . . . . . . . . . Using the DataGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . Detecting Postback . . . . . . . . . . . . . . . . . . . . . . . . . . . . Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programmatic Validation . . . . . . . . . . . . . . . . . . . . . . . . Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sending Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using HTML Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 271 . . . . . . . . . . . . . . . . . . . . 271 . . . . . . . . . . . . . . . . . . . . 272 . . . . . . . . . . . . . . . . . . . . 272 . . . . . . . . . . . . . . . . . . . . 273 . . . . . . . . . . . . . . . . . . . . 275 . . . . . . . . . . . . . . . . . . . . 276 . . . . . . . . . . . . . . . . . . . . 281 . . . . . . . . . . . . . . . . . . . . 282 . . . . . . . . . . . . . . . . . . . . 282 . . . . . . . . . . . . . . . . . . . . 283 . . . . . . . . . . . . . . . . . . . . 286 . . . . . . . . . . . . . . . . . . . . 287 . . . . . . . . . . . . . . . . . . . . 288Chapter 12 Peer-to-Peer Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Internet Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Using Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Using UDP Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Using TCP Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 The TCPChat Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 The TCPChatServer Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 The TCPChatClient Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Interacting with Web Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Downloading Documents with WebClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Uploading Documents with WebClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 The WebRequest and WebResponse Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Chapter 13 Advanced Web Services What Are Web Services? . . . . . . . . . . . . . Creating a Web Service . . . . . . . . . . . . . . Caching Web Service Data . . . . . . . . . Consuming a Web Service . . . . . . . . . . . . Preserving State . . . . . . . . . . . . . . . . . . . . Using Session State . . . . . . . . . . . . . . Making a Database Connection . . . . . . . . Using the Pubs Sample Database . . . . Getting an XML Dataset . . . . . . . . . . Potential Problems with MSDE . . . . . Looking at the Results . . . . . . . . . . . . Implementing WSDL . . . . . . . . . . . . . . . . Viewing WSDL . . . . . . . . . . . . . . . . .. . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331xiii 13. xivCONTENTSSOAP Too . . . . . . . . . . . . . . . . . . . . . . . . . . Complex Types . . . . . . . . . . . . . . . . . . . . . . PortType . . . . . . . . . . . . . . . . . . . . . . . . . . . Seeing SOAP, WSDL, and the Reference Map UDDI: The Registry . . . . . . . . . . . . . . . . . . . . . Testing a Published Web Service . . . . . . . . . . . . . Security Considerations . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .333 333 335 336 336 337 339 339Chapter 14 Building Asynchronous Applications with Message Queues . . . 341 Queues and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Types of Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Creating New Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Administering Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 The MessageQueue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Exploring a Computers Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 The Message Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Message Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Creating and Sending Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Acknowledgments and Time-Outs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Requesting Message Acknowledgment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Processing Acknowledgment Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Fault Tolerance and Load Balancing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 Transactional Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Processing Orders with Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Preparing Orders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Processing Orders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Message Queuing Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Defining Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Defining Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 The ProcessOrders Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Chapter 15 Practical ADO.NET Accessing Databases . . . . . . . . . . . . . The Visual Database Tools . . . . . The Connection Class . . . . . . . . The DataAdapter Class . . . . . . . The Command Class . . . . . . . . . Working with DataSets . . . . . . . . . . Accessing the DataSets Tables . . Working with Rows . . . . . . . . . . Handling Null Values . . . . . . . . Adding and Deleting Rows . . . . . Locating Rows . . . . . . . . . . . . . . Navigating through a DataSet . . . Using DataViews . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 14. CONTENTSInsert and Update Operations . . . . . . . . . . . . . . . Updating the Database with the DataAdapter . Handling Identity Columns . . . . . . . . . . . . . . Performing Transactions with the DataAdapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .428 428 430 436 440Chapter 16 Building Middle-Tier Components . . . . . . . . . . . . . . . . . . . . . . 441 From Client/Server to Multiple Tiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 What Exactly Is a Business Rule? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Designing with Middle-Tier Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Remoting the Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Converting the BusinessLayer Class to a Web Service . . . . . . . . . . . . . . . . . . . . . . . . 450 Converting the BusinessLayer to a Remote Service . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Using COM Components with .NET Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Using ActiveX Controls in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 Using COM+ Applications in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 The COMPlus Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 Exporting a Proxy and Testing It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 Building Serviced Components with .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Chapter 17 Exploring XML Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Choosing SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Copying the Sample File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Using SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Deeper into DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 Using Namespaces in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 Explicit Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 Implicit Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 The Explosion of Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Understanding XSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Using XML Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Programmatic XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Edit and Save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 A Recursive Walk through the Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 XML and DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Persisting with SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 Mixing and Matching Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Deserialization Trapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 More Interchangeability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 Chapter 18 Designing Data-Driven Windows Applications . . . . . . . . . . . . . 505 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 The NWProducts Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 The Applications Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 The Applications Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 The Applications Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510xv 15. xviCONTENTSAn Invoicing Application . . . . . . . The Applications Interface . . . The Applications Architecture The Applications Code . . . . . Adding a Business Rule . . . . . The Relations Application . . . . . . The Applications Architecture The Applications Code . . . . . The Relations1 Project . . . . . . . . Summary . . . . . . . . . . . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .. . . . . . . . . .516 516 518 525 532 535 535 536 539 542Chapter 19 Working with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . 543 Writing Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 The RegularExpressions Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Using the Matches Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550 Using the Match and NextMatch Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 The Split Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552 The Replace Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552 The Elements of a Regular Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 Characters and Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 Single Character Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 Ranges of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 White Space and Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562 Escaping Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 Alternation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 The RegExEditor Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564 Advanced Topics in Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 Grouping and Back-References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 Regular Expressions with Multiple Captures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 Lookahead and Lookbehind Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 Advanced Replacement Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 The RegularExpressions Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 The Visual grep Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588 Chapter 20 Advanced Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 The PlotControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590 The GraphicsPath Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591 The Controls Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591 Drawing the Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598 A Fractal Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 What Is a Fractal? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 The Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607 The Julia Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612 The Real Magic of Fractals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616 16. CONTENTSComplex Number Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620 The Transformation z = z 2 + c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622 Chapter 21 Designing the User Interface Making Applications Look Reliable . . . . . . . . . . Windows Conventions . . . . . . . . . . . . . . . . . . . . The Metallic Look . . . . . . . . . . . . . . . . . . . FontBold Off . . . . . . . . . . . . . . . . . . . . . . . Using a Sans-serif Typeface for Headlines . . . Choosing a Type Size . . . . . . . . . . . . . . . . . . Layering . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Depth . . . . . . . . . . . . . . . . . . . . . . . Light from the Upper Left . . . . . . . . . . . . . . Creating Zones . . . . . . . . . . . . . . . . . . . . . . Framing . . . . . . . . . . . . . . . . . . . . . . . . . . . Metallic Shading . . . . . . . . . . . . . . . . . . . . . . . . Sliding and Fading Transitions . . . . . . . . . . . . . . Fade In, Fade Out . . . . . . . . . . . . . . . . . . . . Sliding . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 623 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641Chapter 22 Using the .NET Compact Framework and Its Emerging Technologies . . . . . . . . . . . . . . . . . . . . . . . . . 643 Whats Eliminated? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644 Output Lite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644 Solving the Connectivity Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644 Using the Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 Understanding the Mobile Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646 Navigating to a Second Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646 More New Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 New Technology, New Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648 The List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 Mobile Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 Debugging via Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654 Custom Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 Trace Information Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656 Providing Friendly Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656 Device Specificity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658 Using Emulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 Custom Device Emulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 The Visual Studio 2003 Pocket PC Emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 New Technology, New Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665xvii 17. Introduction This book actually began in Athens, Greece in 1993. Evangelos Petroutsos wrote a very interesting outline, and several sample chapters, for a book about fascinating and sophisticated things you could do with Visual Basic. I agreed with my publisher that his ideas had potential, but Evangelos was a first-time author. I had a track record, though, so the publisher said theyd invest in this Power Toolkit book if I agreed to co-author it. Even a small book represents a $50,000 gamble for a publishing house, and this was a very large book. I merrily agreed because I thought the topics were compellingfractals, encryption, processing graphics, animated transitions, multimedia, manipulating color palettes, recursion, and other topics that were largely ignored by other VB books. To our delight, the book became a runaway bestseller in 1995. Evidently many Visual Basic programmers were ready for a book about advanced, cutting-edge programming techniques. In 2002, we decided to revisit this concept. Nearly a decade has passed, and we now have what amounts to a brand new Visual Basic language: VB.NET. We decided to follow the same path that we went down a decade ago: to explore aspects of VB.NET that have been largely ignored in other books, but are useful or interesting, or both. Most of the topics covered ten years ago in the previous book are not repeated heretimes have changed. But we feel that the subjects explored in this new book are compelling in their own right.Aesthetics Why would captivating topics be largely ignored in computer books? We think there are two primary reasons. The first category of ignored topics is seen as trivial or marginal. Put another way, these subjects involve aesthetics. Programmers by and large prefer to consider themselves part of the scientific community, so examining such unscientific concepts as beauty or appearance seems to many programmers to be a step down. Two of the chapters in this book, nonetheless, boldly explore aesthetic subjects. Truth be told, programming is an art, not a science. Some professors conjure up theoretical constructs and special terminology, but airy obfuscation and lofty-sounding jargon do not, by themselves, create a scienceand all too often actually inhibit rational discourse. Studies have shown that the best programmers are frequently English or music majors. Some of the best developers around today got into programming when they purchased their first Amiga computeran early machine devoted to the creative side of computing. And although academic programming is generally allied with mathematics departments, there is very little real relationship between 18. xxINTRODUCTIONmath (or science) and programmingjust as there is often very little relationship in general between many other academic studies and the real world. Consider the primary current computer applications: word processing, database management, Internet communications, and spreadsheets. Only spreadsheets have much at all to do with math. Programming can, of course, involve math, but its rarely central to the programmers task. You could write an entire word processing program without even knowing long division, much less algebra or anything beyond. And programming obviously isnt a science. Science involves theorizing and controlled experimentation, behaviors rarely associated with programming. Sure, theres a kind of experimental hacking that goes on while trying to fix bugsbut thats not scientific experimentation by any stretch of the imagination. Debugging is much closer to searching for a lost set of keys than sending a kite up into a thunderstorm. Programming is basically communicationalbeit between humans and machines. But it is a linguistic and expressive act. Its not exactly rhetorical (we dont need to persuade the machines, at least not yet). But its certainly descriptive, grammatical, and fundamentally communicative. The two chapters in this book that some will consider unscientific are Chapter 21, Designing the User Interface, and Chapter 20, Fractals: Infinity Made Visible. We agree. But then we think the entire subject of programming is unscientific, and were not bothered by that fact.Complexity and the Avant-Garde Most of the remaining topics in this book fall into the second category: topics that are either too cutting-edge or too complex for inclusion in many books. For example, not much is written about VB.NETs splendid and extensive security featureseven though security is a primary ongoing challenge for the computing community. Security-related VB.NET programming is avoided not because the programming involved is inherently difficult or novel, but rather because the concepts underlying cryptology and other aspects of security are fundamentally complex. Many computer book authors simply dont know enough about encryption, for example, to explain its implementation in computer programming. Fortunately, cryptology has long been a hobby of one of the authors of this book. Other topics are perhaps too new to be widely understood or implemented. Asynchronous programming, Web services, employing Office objects, using reflection, and the new .NET Compact Framework (how to squeeze programming and I/O into the highly restrictive platform of small, portable devices such as PDAs and cell phones) all fall into this category. Several of the chapters in this book, we admit, have been covered fairly extensively in other books (database programming, debugging, printing), but we included them because we feel that we have something new to say. For example, weve yet to find any book that correctly describes how to print hard copy in VB.NET. All the programming examples weve seen either cut letters in half at the end of lines, or cut lines in half at the end of pages. This doesnt happen on every line or at the end of every page, but youll agree that its pretty bad when it happens even intermittently. If youve been looking for the solution to this problem, see Chapter 7. Chapter 1 is unusual because it tackles an essential, yet widely avoided, question: Why was Visual Basic .NET designed by C programmers, and what are the implications? Its as if the Romans had been given the job of rebuilding Thebesthe result might be impressive, but it certainly wouldnt remain Egyptian. 19. INTRODUCTIONChapter 1 begins like this: Visual Basic .NET was not written by Visual Basic programmers. The entire .NET family of languages was created by C programmers. Cand its cohort OOPis an academic language. Visual Basic is a popular language. These facts have consequences. The authors of this book are not beholden to any organization. Were not writing for Microsoft Press, nor are we affiliated with any corporation or school. Indeed, we like to think that were not dependent on anyone for our paycheckother than you, dear readerand can therefore be more objective than many of our colleagues. We can ask heretical questions such as why OOP should be used in all programming situations as many of its proponents insist. We can question the wisdom of allowing C programmers to write the narratives and code examples for the Help system in VB.NET. We can wonder why structures are included in VB.NET if OOP experts insist that you should never use them. We can freely applaud VB.NET when it improves on traditional VB programming features (streaming and serialization, for instance), and point out when VB.NET creates needless confusion. (Some collections in VB.NET are zero-based; some are one-based. And theres no rhyme or reason involved, no pattern you can discover, no rule you can learn, to deal with this problem.) Another benefit of being outside programming and academic officialdom is that we can be clear. There is a lingo developing around programming, and too much of it appears to serve no real purpose other than job protection. If others cannot read your source code, or even understand your comments, then its likely theyll respect you and youll keep your job. Likewise, if you follow the party line and keep your geek-speak up-to-date, youll be on the team. So the usual little closed society of a priest class is being built. Remember that only a short time ago mass was said in Latin, a language that the churchgoers couldnt understand. And if you visit a college class in music theory or film theory today, you wont comprehend most of whats being said. When we do now and then indulge in techie jargon in this book, its usually to let you know whats meant by the latest catchphrases. True, the term overloaded signature is used in this book, but right next to it is the parenthetical explanation (more than one argument list), just so youll know what the heck is being discussed. And when terminology, such as strongly typed, has several different meanings, we point that out to you. Theres one final benefit derived from the authors status as independent writers, free of any obligation to particular corporations or institutions: we can be entertaining, or at least less boring than the average computer book. Academic articles and books, including many programming books, deliberately avoid amusing or interesting writing. Its thought in some circles that if your writing isnt obscure or tedious, then you must not be discussing anything sufficiently serious. We take the position that honest, understandable, direct, and interesting writing is preferable to the alternative.Who Should Read This Book, and Why? This book is intended to provide solutions for programmers who are ready to take the next step up to more complex, cutting-edge, or sophisticated topics. Chapters 1 and 2 are useful if youre making the transition to VB.NET from another programming language (such as classic Visual Basic, versions 6 and earlier). This book is, we believe, accessible to any intelligent person with programming experience. We have tried to be clear throughout the book, explaining everything as directly as possible, regardless of degree of difficulty of the various topics.xxi 20. xxiiINTRODUCTIONYoull find a variety of useful utilities throughout the book, including DES and RSA encryption systems in Chapter 6 that you can plug into your projects to protect the privacy of any kind of data. The chapter on Office automation, Chapter 4, demonstrates how to add a variety of utilities to your VB.NET programs: extracting statistical information such as word or paragraph counts; evaluating math expressions; spell checking; automatically retrieving e-mail; filtering and displaying e-mail; searching directories and subdirectories for specific files; importing data from or exporting data to Word, Excel or Outlook; and sending faxes. Chapter 5 takes you on a tour of the Windows and .NET multi-layered security mazeall the various secret keyholes and locks that must be correctly set before even a single function can execute. Youll see how to manage code-based (.NET), role-based (Windows), and miscellaneous (IIS, database-specific, and so on) security systems. Did you know that you can specify security with such great specificity that you can grant or deny permission for each individual procedureor even for each individual line of codewithin your VB.NET projects? Chapter 8 explores the new technology called reflection with which you can extract information from self-describing assemblies (libraries of code that contain descriptions of their contents). Youre shown how you can use this information in specialized, though practical, applications, and how you can even go so far as to emit (write code-generating code). Forms design is the topic of Chapter 21: What you can do to make the appearance of your VB.NET programs more professional, polished, and ergonomic. This chapter covers one of the most overlooked aspects of program designthe design itself properly so called, the actual look of the finished application on its surfacewhere your programming logic rises and becomes visible to your users. Among the techniques explained in this chapter are light sources, metallic surfaces, fonts, layering, depth, framing, shading, gradients, and transitions. In Chapters 11, 13, and 18 youll find largely hypefree coverage of programming data-driven Web applications, Web services, and XML, respectively. Although you will find a book on every aspect of ADO.NET in the market, weve included two chapters on the topic of database programming: an overview of the ADO.NET object model with simple examples and a chapter with practical data-driven applications. One of the examples is an invoicing application, which is a fundamental component of every business. Besides sending XML data to Japan, or exchanging data with a database residing on a satellite, a company may need to sell products and services, and for this youll need a functional application for preparing orders and invoices. Its a humble task, but too important to be skipped in a practical chapter on data-driven applications. You have certainly read a lot about the middle tier of a data-driven application. If you understand what a middle tier is, you can skip our discussion on developing middle-tier components. If you need a simple example of a business rule, how to implement business rules as middle-tier components, and how to deploy middle-tier components so that you can change the business rule without touching the code of the application that has already been deployed to the users workstations, then explore our examples in Chapters 16 and 18. Weve also included a few useful tools, which you can use in your projects with little or no customization. The PRNTextBox and PRNListView controls are enhanced versions of the TextBox and ListView controls that provide methods to print their contents. Regular expressions are not the bread and butter of the typical VB developer, and this is the reason most books totally ignore this topic. To demonstrate regular expressions in Chapter 19, weve included the RegExEditor: a simple text editor that allows you to search text files using general search patterns, such as e-mail addresses, dollar amounts, and so on. In Chapter 20 weve included two graphics applications, one for plotting functionsa practical control you can use in any number-processing applicationand a fractal applicationa program that generates fascinating patterns for your amusement. All of the utilities are available at www.sybex.com. 21. Chapter 1Understanding the .NET Framework Visual Basic .NET was not written by Visual Basic programmers. The entire .NET family of languages was created by C programmers. Cand its cohort OOPis an academic language. Visual Basic is a popular language. These facts have consequences. Visual Basic was conceived in 1990 specifically as an alternative to C. VB was designed as a rapid application-development languageblessedly free of cant and obscurantism. VB was created especially for the small businessman who wanted to quickly put together a little tax calculation utility, or the mother who wanted to write a little geography quiz to help Billy with his homework. VB was programming for the people. Several hundred thousand people use C; millions use Visual Basic. As with many culturesRome versus Egypt, USA versus France, town versus gownprogramming languages quickly divided into two camps. C and its offspring (C++, Java, C#, and others) represent one great camp of programmers. Visual Basic is the other camp. However, .NET is an attempt to merge Visual Basic with the C languageswhile still retaining as much as possible of the famous populist VB punctuation (direct, clear, straightforward, English-like), syntax, and diction. Many professors, bless them, thrive on abstraction, classification, and fine distinctions. Thats one reason why VB.NET is in some ways more confusing than necessary. It has many layers of accessibility (scoping) and many varieties of ways to organize data, some more useful than others. It has multiple qualification schemes; considerable redundancy; single terms with multiple meanings (strong typing, for example); multiple terms for a single behavior (Imports versus Import); and all kinds of exceptions to its own rules. VB.NET, however, is clearly an improvement over earlier versions of VB in many respects. We must all find ways of moving from local to distributed programming techniques. And VB.NET is also quite a bit more powerful than previous versions. For example, streaming replaces traditional file I/O, but streaming can also handle data flowing from several sourcesnot just the hard drive. Streaming considerably expands your data management tools. You can replace a FileStream with a WebResponse object, and send your data to a Web client. Nonetheless, in the effort to merge all computer languages under the .NET umbrella, VB had to give up some of its clarity and simplicity. In fact, VB now produces the same compiled code 22. 2Chapter 1 UNDERSTANDING THE .NET FRAMEWORKthat all the other .NET languages doso under the hood, there is no longer any distinction to be made between the two linguistic cultures. Its just on the surface, where we programmers work, that the differences reside. OOP itself, like biology, involves a complex system of classification. This means that people using OOP must spend a good amount of their time performing clerical duties (Where does this go? How do I describe this? What category is this in? Are the text-manipulation functions located in the Text namespace, or the String namespace? Does this object have to be instantiated, or can I just use its methods directly without creating the object first?)Why Read This Chapter If youre one of the millions of VB programmers who, like me, came upon VB.NET with the highly intelligent reaction Whaaaaa??!!, this chapter might be of use to you. Im not unintelligent, and Im assuming youre not either. But slogging through the VB.NET world makes one seem rather slow, especially at first. This chapter gives you some hard-won advice that can save you considerable confusion. VB.NET is, of course, far easier to navigate if you have a background in C programming languages (and its lovely wife, object-oriented programming). Millions of VB programmers deliberately decided not to use C. Thats why we became VB programmers in the first place. We preferred the power of VBs rapid application development tools. We didnt care for the reverse-Polish backward syntax, the redundant punctuation (all those semicolons) and other aspects of C and its daughter languages. The Internet changed all thatwe must develop new skills and adapt to new programming styles. Leaving the cozy and predictable world of local programming (applications for Windows, running on a single computer) requires new techniques. You dont have to switch to C or its ilk, but you do have to expand your VB vocabulary and skills. Todays programs are sometimes fractured into multiple programlets (distributed applications) residing in different locations on different hard drives and sometimes even using different platforms or languages. Web Services are the wave of the future, and this kind of computing greatly increases the impact of communication and security issues. Not to mention the necessity of encapsulating code into objects. So gird your loins or whatever else you gird when threatened, and get ready for some new ideas. Youve got to deal with some different notions. Each of us (the authors) has written several books on VB.NET in the past few years working within the .NET world daily for three years nowand were still discovering new tools, concepts, and features. Part of this is simply getting to know the huge .NET Framework, and part of it is adjusting to OOP and other C-like elements that are now part of VB. What they say of quantum mechanics applies to OOP: only ten people in the world understand it well, and nobody understands it completely. So be brave. You can learn some patterns and rules to help you get results in .NET, and the benefit is that VB.NET is quite a bit more powerful and flexible than traditional VB. There are considerable rewards for your patience and efforts. Youll find ideas in this chapter that will deepen your understanding of the great, vast .NET environment and framework. Youll find useful information here that will improve your VB.NET programmingguaranteed. For example: What are structures, and when should you use them? (Theyre 23. HELP!a replacement for classic VBs user-defined types, and you should never use them. OOP experts say that whenever youre tempted to use a structure, create a class insteadits more flexible. Of course other OOP experts disagree, and the squabbling begins.)Help! A significant effect of the merging in of VB with C-style languages is that the VB.NET Help system and documentation were mostly written by C programmers. These people are not generally writers nor are they very familiar with Visual Basic. Thats why you find techno-speak error messages, convoluted descriptions in the Help system, and other foggy patches. So, instead of VBs justly acclaimed clarity, we get Help descriptions that sound like they were written by a barmy IRS bureaucrat. Heres an example: Changing the value of a field or property associated with any one instance does not affect the value of fields or properties of other instances of the class. On the other hand, when you change the value of a shared field and property associated with an instance of a class, you change the value associated with all instances of the class. Got it? Not only is the VB.NET documentation all-too-often puzzling, the fact that C programmers wrote it means that the descriptions and even the source code examples are often some half-English, half-C beast. Many Help source code examples listed as VB.NET versions are, in fact, written by C programmers. VB programmers must spend the time to translate this faux VB code. Its great that there is now so much tested, bug-free example code in Help. However, perhaps Microsoft would be wise to ask experienced VB programmers to go over the pseudo Visual Basic code examples, and translate them into actual VB-style programming. For example, take a look at the entry in Help for String.IndexOf. If you scroll down the right pane, you can see all the ways that the sample code is not typical VB code. Many VB programmers will have to figure out how to actually make this code work. It cant just be copied and pasted. VB programmers can be confused by some of the strange punctuation and other odd qualities of the following, and many other examples you find in Help. Although nominally Visual Basic source code, too many Help examples are alien in many particulars, as you can see in this sample code illustrating the IndexOf method: Imports System Class Sample Public Shared Sub Main() Dim br1 As String = _ 0----+----1----+----2----+----3----+----4----+----5----+----6----+- Dim br2 As String = _ 0123456789012345678901234567890123456789012345678901234567890123456 Dim str As String = _ Now is the time for all good men to come to the aid of their party. Dim start As Integer3 24. 4Chapter 1 UNDERSTANDING THE .NET FRAMEWORKDim at As Integer Dim [end] As Integer Dim count As Integer [end] = str.Length start = [end] / 2 Console.WriteLine() Console.WriteLine(All occurences (sic) of he from position {0} to {1}., _ start, [end] - 1) Console.WriteLine({1}{0}{2}{0}{3}{0}, Environment.NewLine, br1, br2, str) Console.Write(The string he occurs at position(s): ) count = 0 at = 0 While start - 1 start+count must be a position within -str-. count = [end] - start at = str.IndexOf(he, start, count) If at = - 1 Then Exit While End If Console.Write({0} , at) start = at + 1 End While Console.WriteLine() End Sub Main End Class SampleThis example was obviously written by a C programmer who had little experience with Visual Basic programming. There are several giveaways. First is the use of a class to illustrate a simple, small programming technique. Creating a class for this purpose is overkill, though some strict OOP enthusiasts insist that everything in programming must be an object. Also, Visual Basic programmers almost never use Sub Main. Instead, we use the Form1_Load event as the startup object. For one thing, this event is the default startup object in Visual Basic. Programmers using languages that are not form-based, such as C, employ Sub Main as their entry point when a program begins execution. There is no default Form1 in C, so C programmers are accustomed to starting their programs in Sub Main (which is where they put initialization code, and also where they test short code samples). To make the many VB code samples in Help work, a VB.NET programmer must right-click the project name in Solution Explorer, choose Properties, then manually adjust the startup object in a Property Pages dialog box. Another giveaway that this isnt true VB is the line Imports System. VB.NET programmers know that by default the System namespace is always available and need not be imported. Also, VB.NET programmers do not use the brace {} symbols to fill in fields when displaying results, nor do we use the word End as a variable name, requiring that it be enclosed in brackets [ ]. 25. GRAPPLING WITH FRAMEWORK CLASS DESCRIPTIONSIn fact, until VB.NET, the Visual Basic language didnt even permit the use of braces, semicolons, or brackets. Blessed simplicity, including the avoidance of extraneous junk punctuation, has always been a hallmark of Visual Basic. To make this usable, to-the-point, Visual Basic-style sample code, you have to eliminate the C-flavored elements. What follows is a simplified, and pure-VB.NET, translation of this same sample. In addition to being written in recognizable VB programming style, it also has the advantage of focusing on IndexOf, the method being illustrated. The example displayed above from Help is overly complex: involving a loop, word counting, and one of the less frequently used of the IndexOf methods overloaded variations. The idea that the example is supposed to be demonstrating gets lost in a mess of irrelevancies. To be really helpful to VB programmers, Help code and Help narrative explanations should be written by a professional writer/programmer, not simply someone technically competent, with a strong C bias. And the example code should simply illustrate the method being explained, like this: Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.LoadDim s As String = _ Now is the time for all good men to come to the aid of their party. Dim found As Integer = s.IndexOf(men) Console.Write(found) End SubGrappling with Framework Class Descriptions You have to learn how to translate the class descriptions in the Object Browser, online documentation, or Help into useable VB code. Sure, theres example code in many Help entries, but that code all too often doesnt precisely demonstrate the syntax you are looking for (it was written by C programmers, after all). Other entries offer no example code at all. There are tens of thousands of members in VB.NET, each with its own signature (parameter list) or set of signatures (thanks to overloading). And as youll see in this chapter, even seemingly similar classes can require quite different instantiation and different syntactic interactions with other classes to accomplish a particular job. You therefore frequently have to read a description in Help, the Object Browser, or other documentation and then translate that description into executable source code. Press Ctrl+Alt+J to open the VB.NET Object Browser. Locate System.IO.File, then in the right pane locate the first version of the Create method, as shown in Figure 1.1. In the lower pane, you see this information: Public Shared Function Create(ByVal path As String) As System.IO.FileStream Member of: System.IO.File5 26. 6Chapter 1 UNDERSTANDING THE .NET FRAMEWORKSummary: Creates a file in the specified path. Parameters: path: The path and name of the file to create. Return Values: A System.IO.FileStream that provides read/write access to the specified file.Many VB programmers arent used to having to interpret this kind of informationVB used to be a simpler language. Now, with VB.NET, its a new ball game. You are certain to find yourself often looking at something like the description in Figure 1.1, and wondering how to change this into source code.The Hunt for a Grammar We want to think that there is an underlying set of rules, a grammar, that organizes .NET source code. We want to learn the rules so we can instantiate objects, and invoke their methods, without having to continually make educated guesses, then see error messages, then try again by adjusting the syntax, punctuation, or phrasing. We want to assume that the grammar of .NET is consistentso we dont have to struggle time and again with constructions that follow no particular pattern. Figure 1.1 Often this is all the information you get about how to use a .NET class. Translating this into useable source code is up to you. 27. THE HUNT FOR A GRAMMARToo bad. What we want isnt always what we get. For all its power and virtues, .NET is riddled with idiosyncratic, patternless constructions. Solecisms abound. The situation is similar to the way that prepositions are used in English. There are no fixed rules that govern prepositions, yet they serve to shade the meaning of many sentences. Probably the single most obvious clue that a speaker is relatively illiterate (once they get verb forms figured out) is the misuse of prepositions. Because there are no rules, each person refines his use of prepositions by listening to educated speakers (not local TV reporters) and reading well-written text (not local newspaper stories). For example, many people use the preposition for inappropriatelyusing it as an all-purpose substitute when they dont know the correct preposition. An example: Theres a good chance for rain. (Should be of.) Having worked with VB.NET on a daily basis for years now, I can tell you that your educated guesses about writing instantiation code and how to employ other syntaxes will improve over time. But youll never discover any fixed, reliable rules. There are too many exceptions, too many peculiarities. Heres an illustration. The first confusion when trying to grammatically diagram the items shown in Figure 1.2 comes from the fact that two classes appear to be working together to accomplish the job of creating a file in the specified path: System.IO.FileStream and System.IO.File. Both a filestream and a file object must be used in your code. However, since they both belong to the System.IO namespace, you need only import that single namespace: Imports System.IOThe namespaces that are automatically added as defaults to each VB.NET Windows-style project are: System, System.Data, System.Drawing, System.Windows.Forms, and System.XML. These defaults, however