Visual Basic .NET The Complete Reference

664

description

Visual Basic .NET The Complete Reference

Transcript of Visual Basic .NET The Complete Reference

  • 1. Table of Contents Visual Basic .NET The Complete Reference...................................................................................................1 Foreword..............................................................................................................................................................3 Acknowledgments ...................................................................................................................................6 Introduction.........................................................................................................................................................8 Chapter 1...............................................................................................................................................10 Chapter 2...............................................................................................................................................10 Chapter 3...............................................................................................................................................10 Chapter 4...............................................................................................................................................11 Chapter 5...............................................................................................................................................11 Chapter 6...............................................................................................................................................12 Chapter 7...............................................................................................................................................12 Chapter 8...............................................................................................................................................12 Chapter 9...............................................................................................................................................12 Chapter 10.............................................................................................................................................13 Chapter 11.............................................................................................................................................13 Chapter 12.............................................................................................................................................13 Chapter 13.............................................................................................................................................13 Chapter 14.............................................................................................................................................14 Chapter 15.............................................................................................................................................14 Chapter 16.............................................................................................................................................14 Chapter 17.............................................................................................................................................15 Conventions..........................................................................................................................................15 Part I: Introduction to Visual Basic .NET.....................................................................................................16 Chapter List...........................................................................................................................................16 . Chapter 1: Software Development and Visual Basic .NET ..........................................................................17 Overview...............................................................................................................................................17 Visual Basic and the Difficulty of Developing Software.....................................................................17 Reducing Complexity and TimetoMarket with Reuse..............................................................18 Software Development and Software Engineering...............................................................................18 The Classic Programming Models........................................................................................................19 Structured Programming.......................................................................................................................20 BugReduced Code ........................................................................................................................21 Divide and Conquer.......................................................................................................................21 . Reuse..............................................................................................................................................21 Teaming..........................................................................................................................................22 Structural Nada?.............................................................................................................................22 ObjectBased Programming..........................................................................................................22 ObjectOriented Software Development.............................................................................................22 . RealWorld Reflections.......................................................................................................................24 What Makes a Pure ObjectOriented Language..................................................................................25 Just Classes.....................................................................................................................................25 Classes for Modularity, Cohesion, and Coupling ...........................................................................26 Classes for Abstraction ...................................................................................................................27 Classes for Encapsulation ...............................................................................................................27 Classes for Hiding Information......................................................................................................27 Classes for Classification...............................................................................................................28 i

2. Table of Contents Chapter 1: Software Development and Visual Basic .NET Class that Beget Classes: Inheritance.............................................................................................29 Classes for Objects: Instantiation...................................................................................................29 Classes for Association, Aggregation, and Composition...............................................................29 Classes for Events..........................................................................................................................30 Classes for Message Passing..........................................................................................................30 Classes for Polymorphism..............................................................................................................30 Classes for Interfaces ......................................................................................................................31 Frameworks ...........................................................................................................................................31 Patterns ..................................................................................................................................................32 Observations.........................................................................................................................................33 Chapter 2: Visual Basic .NET and the .NET Framework ............................................................................34 Overview...............................................................................................................................................34 Getting to Know the Framework's Runtime.........................................................................................35 The Common Type System............................................................................................................35 The Common Language Specification...........................................................................................36 The Common Language Runtime.........................................................................................................38 Microsoft Intermediate Language..................................................................................................40 Metadata.........................................................................................................................................41 Executable Code.............................................................................................................................42 Managed Execution........................................................................................................................42 SidebySide Execution................................................................................................................44 Application Domains ......................................................................................................................44 Automatic Memory Management..................................................................................................45 JustinTime Deployment.............................................................................................................46 Understanding Assemblies ....................................................................................................................47 Locating Assemblies, Anytime......................................................................................................48 What's in an Assembly...................................................................................................................49 The Roles of the Assembly............................................................................................................53 Attributes, Reflection, and Assemblies..........................................................................................55 Strong Names.................................................................................................................................55 The .NET Security Model.....................................................................................................................56 Observations.........................................................................................................................................58 Part II: Visual Basic .NET Fundamentals.....................................................................................................60 Chapter List...........................................................................................................................................60 . Chapter 3: The Visual Basic .NET Development Environment..................................................................61 Overview...............................................................................................................................................61 Working with the Visual Studio IDE....................................................................................................61 Navigating the IDE .........................................................................................................................62 Starting from the Start Page...........................................................................................................69 Creating a Visual Basic .NET Solution................................................................................................72 Loading the Vb7cr Solution...........................................................................................................73 Creating a New Project..................................................................................................................73 . Solution Directory Structure..........................................................................................................76 File Extensions...............................................................................................................................76 Working with the BaseClass Library...........................................................................................77 A Minimal Visual Basic .NET Application...................................................................................80 ii 3. Table of Contents Chapter 3: The Visual Basic .NET Development Environment Observations.........................................................................................................................................82 Chapter 4: The Elements of Visual Basic .NET............................................................................................83 Overview...............................................................................................................................................83 Visual Basic .NET: The Foundation.....................................................................................................83 Lexical Elements............................................................................................................................83 Preprocessing Directives................................................................................................................84 General Concepts...........................................................................................................................84 Option, Imports, and Namespaces Directives................................................................................84 Types..............................................................................................................................................85 Type Members ................................................................................................................................85 Statements and Statement Blocks ...................................................................................................86 Expressions .....................................................................................................................................86 Operators........................................................................................................................................87 Visual Basic .NET Mini Style Guide ....................................................................................................88 Naming and Notation.....................................................................................................................88 Hungarian Notation........................................................................................................................89 Word Choice ...................................................................................................................................90 Getting Started......................................................................................................................................90 Character and Lines........................................................................................................................91 Statements and Blocks ....................................................................................................................95 Nothing for Nothing or Something for Nothing .............................................................................96 Classes, Types, and Objects: What's the Difference?...........................................................................97 Classes: The View from Above............................................................................................................98 The Class Declaration Space..........................................................................................................99 The Directive Space.....................................................................................................................102 The Implementation Space...........................................................................................................109 Elemental Value Types................................................................................................................110 Working with Numbers .......................................................................................................................112 Integer Types................................................................................................................................112 Visual Basic 6 to Visual Basic .NET...........................................................................................113 Point Types.........................................................................................................................................113 Characters...........................................................................................................................................114 Booleans ..............................................................................................................................................114 Literal Notation...................................................................................................................................115 Type Conversion.................................................................................................................................118 Working with Variables and Constants..............................................................................................120 Variable and Constant Declaration Shorthand.............................................................................124 Default Initialization .....................................................................................................................125 Keeping Data Private ....................................................................................................................126 Scope............................................................................................................................................128 Variable and Constant Lifetimes..................................................................................................129 Observations.......................................................................................................................................131 Chapter 5: Visual Basic .NET Operators....................................................................................................133 Overview.............................................................................................................................................133 What an Operator Does .......................................................................................................................133 Numbering Systems Reviewed...........................................................................................................134 Positional Notation.......................................................................................................................135 iii 4. Table of Contents Chapter 5: Visual Basic .NET Operators Converting from One System to Another....................................................................................136 . Operator Precedence...........................................................................................................................138 Changing Precedence Order with Parenthesis ..............................................................................139 Unary Operators..................................................................................................................................140 Arithmetic Operators..........................................................................................................................141 Assignment Operators .........................................................................................................................141 Comparison Operators........................................................................................................................143 Concatenation Operator......................................................................................................................144 Logical Operators ................................................................................................................................144 Logical And, Or, and Xor .............................................................................................................145 ShortCircuit Logical Operators..................................................................................................146 Bitwise Operators ................................................................................................................................147 Flag Sets........................................................................................................................................148 Shifting Bits ...................................................................................................................................150 Specialized Operators.........................................................................................................................154 Is...................................................................................................................................................155 Like ...............................................................................................................................................155 Operator Overloading.........................................................................................................................156 Exceptions Referenced in this Chapter...............................................................................................158 Observations.......................................................................................................................................158 Chapter 6: Software Design, Conditional Structures, and Control Flow.................................................159 Overview.............................................................................................................................................159 Control Structures...............................................................................................................................160 Control Flow.......................................................................................................................................160 Fully Sketched Code...........................................................................................................................162 StepForm Notation.....................................................................................................................162 Pseudocode...................................................................................................................................162 NassiSchneiderman Charts........................................................................................................162 Flowcharts....................................................................................................................................163 Design Pitfalls..............................................................................................................................164 Conditional Statements.......................................................................................................................165 If...................................................................................................................................................165 Else...............................................................................................................................................168 Else If...........................................................................................................................................168 Tips for Else and Else If...............................................................................................................169 Select Case...................................................................................................................................171 GoTo .............................................................................................................................................173 OnError .........................................................................................................................................174 Loops..................................................................................................................................................174 DoLoop .........................................................................................................................................174 For. . . Next ..................................................................................................................................175 . For Each . . . Next ........................................................................................................................176 . While............................................................................................................................................177 One or the Other Conditional Functions.............................................................................................177 Choose..........................................................................................................................................177 IIF.................................................................................................................................................178 Switch...........................................................................................................................................178 Pausing, Resuming, and Exiting Iteration..........................................................................................179 iv 5. Table of Contents Chapter 6: Software Design, Conditional Structures, and Control Flow Exit Idiosyncrasies.......................................................................................................................180 Observations.......................................................................................................................................181 Chapter 7: Methods.......................................................................................................................................182 Overview.............................................................................................................................................182 What Is a Method................................................................................................................................182 Types of Methods.........................................................................................................................184 Synchronous vs. Asynchronous Method Calls.............................................................................184 Method Data ........................................................................................................................................185 Method Data: Global vs. Local....................................................................................................187 Local Declarations ........................................................................................................................188 Passing Arguments to Parameters................................................................................................190 Calling Methods...........................................................................................................................194 Function or Sub Methods.............................................................................................................196 Method Access Characteristics...........................................................................................................198 Public Methods..............................................................................................................................199 Protected Methods.........................................................................................................................200 Friend .............................................................................................................................................200 Protected Friend............................................................................................................................200 Private Methods .............................................................................................................................201 Controlling Polymorphism Characteristics of Methods...............................................................201 Mining the Framework's Methods......................................................................................................205 The Methods of System.Math.............................................................................................................206 Programming with the Math Class...............................................................................................208 MathRelated Exceptions............................................................................................................210 Properties............................................................................................................................................211 Properties vs. Fields.....................................................................................................................213 Properties vs. Methods.................................................................................................................214 Introduction to Exception Handling ....................................................................................................214 The Exception Handler .................................................................................................................216 Try Catch Blocks.........................................................................................................................217 Design and Construction of Methods.................................................................................................218 Class and Method Cohesion.........................................................................................................220 Method Coupling ..........................................................................................................................222 The Length of a Method...............................................................................................................222 Recursive Design of Methods.............................................................................................................223 The Base Case..............................................................................................................................224 The Stopping Condition...............................................................................................................225 The Impact of Recursion..............................................................................................................226 Understanding Method Performance..................................................................................................227 Observations.......................................................................................................................................231 Part III: Classes and Objects........................................................................................................................232 Chapter List.........................................................................................................................................232 . Chapter 8: Types, Structures, and Enumerations......................................................................................233 Overview.............................................................................................................................................233 The ValueType Model......................................................................................................................233 How Value Types Work...............................................................................................................235 v 6. Table of Contents Chapter 8: Types, Structures, and Enumerations Boxing..........................................................................................................................................236 Why are Value Types Objects?....................................................................................................238 Structs and Enums Ahoy: Creating New Value Types................................................................239 Structures............................................................................................................................................240 Structure Behavior ........................................................................................................................246 Enumerations......................................................................................................................................248 Working with System.Enum........................................................................................................250 Flags.............................................................................................................................................253 Final Words on Enums.................................................................................................................255 The ObjectReference Model.............................................................................................................256 Null Reference ...............................................................................................................................261 What the Reference Refers To......................................................................................................262 The ObjectReference Model and Equality, Comparison, and Assignment...............................262 Observations.......................................................................................................................................263 Chapter 9: Classes ..........................................................................................................................................265 Overview.............................................................................................................................................265 Getting the Semantics Correct............................................................................................................266 Of Classes and Types...................................................................................................................267 Semantics and Notation................................................................................................................267 Modeling.............................................................................................................................................267 Software Modeling.......................................................................................................................268 Viewpoints ....................................................................................................................................269 Unified Modeling Language........................................................................................................271 UML Diagrams .............................................................................................................................272 UML Notation for Class Diagrams..............................................................................................273 UML Notation for Class Relationships........................................................................................274 Modularity..........................................................................................................................................274 Modularity Metrics: Coupling and Cohesion...............................................................................275 The Classes Are the System ................................................................................................................275 Class Characteristics...........................................................................................................................278 In the Beginning Abstract Classes.....................................................................................................279 Factoring Out Commonality .........................................................................................................280 The Members of Abstract Classes................................................................................................281 Inheritance ...........................................................................................................................................282 Inheritance and Polymorphism .....................................................................................................284 Inheritance and Coupling.............................................................................................................284 Multiple Inheritance.....................................................................................................................285 Order and Control with Inheritance ..............................................................................................286 Reduction of Complexity.............................................................................................................287 Maintenance.................................................................................................................................288 Code Reuse...................................................................................................................................288 Implementing a Space Ship's Fuel Injector Software.........................................................................289 Instance Fields..............................................................................................................................290 Instance Constructors...................................................................................................................292 Properties......................................................................................................................................293 Methods........................................................................................................................................295 Publishing the ShuttleInjector Class .............................................................................................297 Activating the ShuttleInjector Class.............................................................................................298 vi 7. Table of Contents Chapter 9: Classes The Inherited Members of Object.......................................................................................................300 Testing for Reference Equality with Equals .................................................................................300 ToString........................................................................................................................................301 Cloning.........................................................................................................................................302 GetHashCode ................................................................................................................................303 GetType........................................................................................................................................303 ReferenceEquals...........................................................................................................................304 Finalize.........................................................................................................................................304 Aggregation and Composition: Reuse by Containment.....................................................................304 More Aggregation at Work: A Form for Testing ................................................................................308 Ending Inheritance with Sealed Classes.............................................................................................310 Improved Performance with Shared Classes and Modules .................................................................311 Observations.......................................................................................................................................312 Chapter 10: Interfaces...................................................................................................................................313 Overview.............................................................................................................................................313 Abstraction and Interfaces in ObjectOriented Software Design......................................................313 Getting Passionate (or Radical) about Interfaces................................................................................315 Interfaces and Inheritance............................................................................................................316 Realizing the Benefits of Interfaces.............................................................................................318 Implicit Interfaces...............................................................................................................................318 Explicit Interfaces...............................................................................................................................320 Abstract Class or Explicit Interface ..............................................................................................321 An Introduction to Interface Design and Implementation..................................................................322 Accessing and Using the Implementation....................................................................................324 Compound Interfaces...................................................................................................................325 Designing and Defining Interfaces.....................................................................................................327 Interfaces, Once Published, Must Not Change .............................................................................327 Interface Invariance......................................................................................................................327 Constructing the Interface............................................................................................................328 Getting Started with the Interface Definition...............................................................................329 Implementing Interfaces.....................................................................................................................331 Interface Implementation Semantics............................................................................................331 Implementing ICloneable.............................................................................................................332 Implementing IComparable ..........................................................................................................333 Exceptions Covered in this Chapter ....................................................................................................334 Observations.......................................................................................................................................334 Chapter 11: Exceptions: Handling and Classes..........................................................................................336 Overview.............................................................................................................................................336 Why Do We Need Exception Handling?............................................................................................337 Structured Exception Handling 101....................................................................................................337 ExceptionHandling Models..............................................................................................................340 The Resumption Model................................................................................................................340 The Termination Model...............................................................................................................340 . Recovering from Exceptions ...............................................................................................................341 Exception Statements..........................................................................................................................343 Try................................................................................................................................................343 Catch .............................................................................................................................................344 vii 8. Table of Contents Chapter 11: Exceptions: Handling and Classes Finally ...........................................................................................................................................348 When Filters.................................................................................................................................349 Nesting Try Blocks .......................................................................................................................351 Throw...........................................................................................................................................352 ExceptionHandling Tips...................................................................................................................357 Creating Your Own Exception Classes ...............................................................................................358 The .NET Exception Hierarchy....................................................................................................358 Choosing a Base Class from which to Inherit..............................................................................359 Observations.......................................................................................................................................362 Chapter 12: Collections, Arrays, and Other Data Structures...................................................................363 Overview.............................................................................................................................................363 NET's Array and Collections Namespace ...........................................................................................364 Specialized Collections................................................................................................................366 ICollection....................................................................................................................................366 IEnumerator and IEnumerable.....................................................................................................367 IList..............................................................................................................................................367 Stacks..................................................................................................................................................367 How to Program Against a Stack.................................................................................................371 Queues .................................................................................................................................................372 How to Program Against a Queue ................................................................................................374 Arrays ..................................................................................................................................................376 The Array Class..................................................................................................................................377 Declaring and Initializing Arrays ........................................................................................................378 Declaring Multidimensional Arrays...................................................................................................381 Jagged Arrays ......................................................................................................................................381 Programming Against Arrays.............................................................................................................382 The UBound Statement................................................................................................................383 Redeclaring Arrays.......................................................................................................................384 The Erase Statement.....................................................................................................................386 The IsArray Function...................................................................................................................386 Array Exceptions................................................................................................................................386 Passing Arrays to Methods.................................................................................................................388 Receiving Arrays from Methods .........................................................................................................389 Searching and Sorting Arrays.............................................................................................................389 The BinarySearch Method ............................................................................................................390 The Basics of Sorting Arrays.......................................................................................................392 Bubble Sort.........................................................................................................................................393 Partition and Merge .............................................................................................................................397 Quicksort.............................................................................................................................................401 Sorting Strings and Other Objects......................................................................................................406 Populating Arrays...............................................................................................................................407 Arrays for Objects...............................................................................................................................407 Hash Tables .........................................................................................................................................413 Observations.......................................................................................................................................417 Chapter 13: Advanced Design Concepts: Patterns, Roles, and Relationships.........................................419 Overview.............................................................................................................................................419 Designs on Classes ..............................................................................................................................419 viii 9. Table of Contents Chapter 13: Advanced Design Concepts: Patterns, Roles, and Relationships Singleton .......................................................................................................................................420 Bridge...........................................................................................................................................423 Strategy .........................................................................................................................................427 State..............................................................................................................................................429 Linked Lists and Trees........................................................................................................................433 Understanding the Linked List.....................................................................................................434 Designing a BaseContainer Class for Lists (and Trees)............................................................436 Implementing the Node................................................................................................................437 Implementing the Container.........................................................................................................439 Implementing the Iterator.............................................................................................................456 Observations.......................................................................................................................................458 Chapter 14: Advanced Interface Patterns: Adapters, Delegates, and Events ..........................................459 Overview.............................................................................................................................................459 Adapters and Wrappers.......................................................................................................................461 Interface Adaptation in ActionCOM .NET Interop..........................................................................462 The Adapter Pattern in .NET..............................................................................................................465 The Adapter Pattern Event Model......................................................................................................470 Delegation: Please Help Me!..............................................................................................................471 Delegates.............................................................................................................................................475 Understanding Delegates ..............................................................................................................477 Declaring the Delegate.................................................................................................................477 Early Bound Delegate Declares...................................................................................................478 Late Bound Delegate Declares.....................................................................................................479 Sorting Data with Delegates...............................................................................................................481 Multicast Delegates .............................................................................................................................484 The .NET Framework Event Model: Delegates and Events...............................................................485 Getting Ready to Wireup: The Event Model in a Nutshell..............................................................489 Delegate Events vs. Adapter Events...................................................................................................490 Delegates vs. Function Pointers..........................................................................................................492 Observations.......................................................................................................................................493 Chapter 15: Data Processing and I/O..........................................................................................................494 Overview.............................................................................................................................................494 Data Processing ...................................................................................................................................494 Working with Strings..........................................................................................................................495 Members of the String Class...............................................................................................................496 Clone............................................................................................................................................496 Compare.......................................................................................................................................497 CompareTo...................................................................................................................................497 Concat ..........................................................................................................................................498 . Copy.............................................................................................................................................498 CopyTo.........................................................................................................................................499 EndsWith, StartsWith...................................................................................................................499 Equals...........................................................................................................................................500 Format..........................................................................................................................................500 IndexOf, LastIndexOf..................................................................................................................501 Insert.............................................................................................................................................501 Intern, IsInterned..........................................................................................................................501 ix 10. Table of Contents Chapter 15: Data Processing and I/O Join, Split......................................................................................................................................502 PadLeft, PadRight........................................................................................................................503 Remove .........................................................................................................................................503 Replace.........................................................................................................................................503 SubString......................................................................................................................................503 ToCharArray................................................................................................................................504 ToLower, ToUpper .......................................................................................................................504 Trim, TrimEnd, TrimStart............................................................................................................504 Classic Visual Basic String Functions................................................................................................504 String Formatting................................................................................................................................505 NumberFormatInfo .......................................................................................................................506 DateTimeFormatInfo....................................................................................................................508 Custom Formatters.......................................................................................................................510 Building Strings with StringBuilder...................................................................................................512 Capacity........................................................................................................................................513 Append.........................................................................................................................................513 AppendFormat..............................................................................................................................514 Insert.............................................................................................................................................514 Remove .........................................................................................................................................514 Replace.........................................................................................................................................515 Regular Expressions ............................................................................................................................515 The .NET Framework Regex Metalanguage ................................................................................517 File, Stream, and Text IO Operations.................................................................................................519 Files and Directories...........................................................................................................................520 The File Class...............................................................................................................................521 Path...............................................................................................................................................521 File Enumerations .........................................................................................................................524 Basic File Class Operations ..........................................................................................................527 Directory .......................................................................................................................................529 The FileInfo Class........................................................................................................................530 DirectoryInfo................................................................................................................................531 Using the Classic File System Object..........................................................................................532 FileSystemWatcher......................................................................................................................534 Streams ................................................................................................................................................537 FileStream....................................................................................................................................538 SeekOrigin Enumeration..............................................................................................................542 BufferedStream............................................................................................................................543 NetworkStream .............................................................................................................................544 CryptoStream ................................................................................................................................544 MemoryStream.............................................................................................................................544 Readers and Writers............................................................................................................................545 Text Encoding..............................................................................................................................545 StringReader/StringWriter...........................................................................................................546 . StreamReader/StreamWriter........................................................................................................548 BinaryReader/BinaryWriter.........................................................................................................550 XML I/O.............................................................................................................................................550 Reading XML Files......................................................................................................................550 Writing XML Files with XMLTextWriter...................................................................................554 Serialization with XML......................................................................................................................557 x 11. Table of Contents Chapter 15: Data Processing and I/O Activating Serialization at Run Time...........................................................................................560 Observations.......................................................................................................................................561 Part IV: Writing Software with VB .NET...................................................................................................562 Chapter List.........................................................................................................................................562 . Chapter 16: Interfacing with the End User.................................................................................................563 Overview.............................................................................................................................................563 Windows Forms..................................................................................................................................565 A Form Is an Object.....................................................................................................................566 The System.Windows.Forms Namespace....................................................................................566 Automatically Generated Code....................................................................................................568 Introduction to Threading...................................................................................................................569 The User Interface and Thread Design .........................................................................................570 The .NET Framework's Thread Model........................................................................................571 Getting Started with Basic Threading..........................................................................................571 MDI Applications...............................................................................................................................573 Creating the MDI Parent..............................................................................................................573 Creating the MDI Children ...........................................................................................................575 The Active Child..........................................................................................................................576 Arranging the Forms....................................................................................................................577 Delegating Application Startup and Shutdown............................................................................577 Keeping a Form on Top ................................................................................................................578 Form Transparency .......................................................................................................................578 Modality.......................................................................................................................................579 Changing Borders.........................................................................................................................580 Changing the Size of Forms.........................................................................................................581 Screen Location............................................................................................................................581 Components and Controls...................................................................................................................583 Adding Components and Controls to Forms................................................................................583 Layout and Grouping ....................................................................................................................584 Positioning Controls.....................................................................................................................588 Setting a Single Property for Multiple Controls ...........................................................................589 Complex Property Pages..............................................................................................................589 Using The Property Grid..............................................................................................................590 Menus and Toolbars ............................................................................................................................590 Adding Menus and Menu Items Programmatically.....................................................................590 ContextChanging Menus ............................................................................................................591 Enhancing Menus.........................................................................................................................593 The Menu Class............................................................................................................................594 Responding to User Input...................................................................................................................594 Binding Events Dynamically ........................................................................................................595 Hot Spots......................................................................................................................................595 Mouse and Keyboard Events ........................................................................................................596 Keyboard Events..........................................................................................................................597 Using a Timer to Fire Events.......................................................................................................597 Collecting User Input..........................................................................................................................597 Buttons ..........................................................................................................................................597 Edit Text Boxes............................................................................................................................598 xi 12. Table of Contents Chapter 16: Interfacing with the End User Check Boxes.................................................................................................................................600 Radio Buttons...............................................................................................................................601 Combo Boxes...............................................................................................................................601 DomainUpDown..........................................................................................................................601 NumericUpDown.........................................................................................................................601 Date and Time Picker...................................................................................................................601 Calendar.......................................................................................................................................602 . A Palette.......................................................................................................................................602 List Boxes.....................................................................................................................................602 CheckedListBox...........................................................................................................................602 ListView.......................................................................................................................................603 Trackbars (Sliders).......................................................................................................................603 Toolbars........................................................................................................................................603 TreeView......................................................................................................................................603 Presentation and Informational Controls............................................................................................603 Labeling........................................................................................................................................604 LinkLabel.....................................................................................................................................604 Status Bar.....................................................................................................................................605 Icons.............................................................................................................................................605 PictureBox....................................................................................................................................605 ImageList......................................................................................................................................605 Progress Bars................................................................................................................................605 Grids.............................................................................................................................................606 ToolTip.........................................................................................................................................606 The ErrorProvider Control...........................................................................................................606 Help Provider...............................................................................................................................607 Printing Support...........................................................................................................................607 Drag and Drop .....................................................................................................................................607 Dragging Data..............................................................................................................................607 Dropping Data..............................................................................................................................608 Using the Clipboard............................................................................................................................609 Observation.........................................................................................................................................609 Chapter 17: Getting Ready to Release.........................................................................................................610 Overview.............................................................................................................................................610 Thinking in Debug Terms...................................................................................................................611 The System.Diagnostics Namespace..................................................................................................611 Enabling Debugging...........................................................................................................................615 Runtime Configuration Files............................................................................................................616 Machine Configuration File.........................................................................................................617 Application Configuration File....................................................................................................617 Security Configuration File..........................................................................................................618 Working with Configuration Files...............................................................................................618 Working with the Debug Class...........................................................................................................618 The Debug Write Methods...........................................................................................................619 Assertion .......................................................................................................................................619 Fail................................................................................................................................................622 Tracing and the Trace Class ................................................................................................................623 Instrumentation .............................................................................................................................623 xii 13. Table of Contents Chapter 17: Getting Ready to Release Understanding the Phases of Code Tracing.................................................................................624 Listening to Your Code................................................................................................................624 DeveloperDefined Listeners......................................................................................................627 Trace Switches.............................................................................................................................627 Debugging with Visual Studio .NET..................................................................................................629 What Species Is Your Bug...........................................................................................................629 Debugging Aids ............................................................................................................................630 Breakpoints ...................................................................................................................................631 Getting Started ..............................................................................................................................632 The Visual Basic .NET Compiler.......................................................................................................635 Using a Response File for Compilation.......................................................................................637 Managing a Class Library from the Command Line ....................................................................638 Conditional Compilation Directives .............................................................................................639 Observations.......................................................................................................................................640 List of Figures..................................................................................................................................................641 Chapter 1: Software Development and Visual Basic .NET................................................................641 Chapter 2: Visual Basic .NET and the .NET Framework...................................................................641 Chapter 3: The Visual Basic .NET Development Environment.........................................................641 Chapter 4: The Elements of Visual Basic .NET.................................................................................641 Chapter 6: Software Design, Conditional Structures, and Control Flow ............................................641 Chapter 7: Methods.............................................................................................................................642 Chapter 8: Types, Structures, and Enumerations................................................................................642 Chapter 9: Classes...............................................................................................................................642 Chapter 10: Interfaces.........................................................................................................................642 Chapter 12: Collections, Arrays, and Other Data Structures..............................................................642 Chapter 13: Advanced Design Concepts: Patterns, Roles, and Relationships ....................................643 Chapter 14: Advanced Interface Patterns: Adapters, Delegates, and Events.....................................643 Chapter 16: Interfacing with the End User.........................................................................................644 Chapter 17: Getting Ready to Release................................................................................................644 List of Tables...................................................................................................................................................645 Chapter 2: Visual Basic .NET and the .NET Framework...................................................................645 Chapter 3: The Visual Basic .NET Development Environment.........................................................645 Chapter 4: The Elements of Visual Basic .NET.................................................................................645 Chapter 5: Visual Basic .NET Operators............................................................................................645 Chapter 6: Software Design, Conditional Structures, and Control Flow ............................................646 Chapter 7: Methods.............................................................................................................................646 Chapter 8: Types, Structures, and Enumerations................................................................................646 Chapter 9: Classes...............................................................................................................................646 Chapter 10: Interfaces.........................................................................................................................646 Chapter 11: Exceptions: Handling and Classes..................................................................................646 Chapter 12: Collections, Arrays, and Other Data Structures..............................................................647 Chapter 15: Data Processing and I/O..................................................................................................647 Chapter 16: Interfacing with the End User.........................................................................................648 Chapter 17: Getting Ready to Release................................................................................................648xiii 14. Table of Contents List of Sidebars ................................................................................................................................................649 Chapter 5: Visual Basic .NET Operators............................................................................................649 Chapter 7: Methods.............................................................................................................................649xiv 15. Visual Basic .NET The Complete Reference Jeffrey R. Shapiro About the Author Jeffrey R. Shapiro is a software architect and IT specialist who has written several books on software development and technology, including SQL Server 2000: The Complete Reference. McGrawHill/Osborne 2600 Tenth Street Berkeley, California 94710 U.S.A. To arrange bulk purchase discounts for sales promotions, premiums, or fundraisers, please contact McGrawHill/Osborne at the above address. For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book. Copyright 2002 by The McGrawHill Companies. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. 1234567890 DOC DOC 0198765432 ISBN: 0072133813 Publisher Brandon A. Nordin Vice President & Associate Publisher Scott Rogers Acquisitions Editor Ann Sellers Project Editor Elizabeth Seymour Acquisitions Coordinator Timothy Madrid Technical Editors Lou Boni, Amir Liberman Copy Editors Mandy Erickson, Bill McManus1 16. Visual Basic .NET The Complete Reference Proofreader Pat Mannion Indexer Valerie Perry Computer Designers George Toma Charbak, Melinda Moore Lytle Illustrators Michael Mueller, Lyssa Wald Series Design Peter Hancik This book was composed with Corel VENTURA Publisher. Information has been obtained by McGrawHill/Osborne from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGrawHill/Osborne, or others, McGrawHill/Osborne does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. This book is dedicated to the late Sabas (Saby) Blanco November 13, 1975 to October 7, 20012 17. Foreword This book started over a burger in a downtown Seattle caf in late 2001. I was reflecting with my editor, Ann Sellers, about the discussions we had just had with the Microsoft .NET protagonists. We came to the conclusion that if Microsoft could pul