Visual Basic .NET The Complete Reference

3,309 views

Published on

Visual Basic .NET The Complete Reference

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,309
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
278
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Visual Basic .NET The Complete Reference

  1. 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 Time−to−Market with Reuse..............................................................18 Software Development and Software Engineering...............................................................................18 The Classic Programming Models........................................................................................................19 Structured Programming.......................................................................................................................20 Bug−Reduced Code ........................................................................................................................21 Divide and Conquer.......................................................................................................................21 . Reuse..............................................................................................................................................21 Teaming..........................................................................................................................................22 Structural Nada?.............................................................................................................................22 Object−Based Programming..........................................................................................................22 Object−Oriented Software Development.............................................................................................22 . Real−World Reflections.......................................................................................................................24 What Makes a Pure Object−Oriented 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. 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 Side−by−Side Execution................................................................................................................44 Application Domains ......................................................................................................................44 Automatic Memory Management..................................................................................................45 Just−in−Time 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 Base−Class Library...........................................................................................77 A Minimal Visual Basic .NET Application...................................................................................80 ii
  3. 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. 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 Short−Circuit 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 Step−Form Notation.....................................................................................................................162 Pseudocode...................................................................................................................................162 Nassi−Schneiderman 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. 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 Math−Related 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 Value−Type Model......................................................................................................................233 How Value Types Work...............................................................................................................235 v
  6. 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 Object−Reference Model.............................................................................................................256 Null Reference ...............................................................................................................................261 What the Reference Refers To......................................................................................................262 The Object−Reference 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. 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 Object−Oriented 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 Exception−Handling Models..............................................................................................................340 The Resumption Model................................................................................................................340 The Termination Model...............................................................................................................340 . Recovering from Exceptions ...............................................................................................................341 Exception Statements..........................................................................................................................343 Try................................................................................................................................................343 Catch .............................................................................................................................................344 vii
  8. 8. Table of Contents Chapter 11: Exceptions: Handling and Classes Finally ...........................................................................................................................................348 When Filters.................................................................................................................................349 Nesting Try Blocks .......................................................................................................................351 Throw...........................................................................................................................................352 Exception−Handling 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. 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 Base−Container 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 Wire−up: 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. 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. 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 Context−Changing 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. 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 Run−time 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. 13. Table of Contents Chapter 17: Getting Ready to Release Understanding the Phases of Code Tracing.................................................................................624 Listening to Your Code................................................................................................................624 Developer−Defined 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................................................................................................648 xiii
  14. 14. Table of Contents List of Sidebars ................................................................................................................................................649 Chapter 5: Visual Basic .NET Operators............................................................................................649 Chapter 7: Methods.............................................................................................................................649 xiv
  15. 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. McGraw−Hill/Osborne 2600 Tenth Street Berkeley, California 94710 U.S.A. To arrange bulk purchase discounts for sales promotions, premiums, or fund−raisers, please contact McGraw−Hill/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 McGraw−Hill 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: 0−07−213381−3 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 McManus 1
  16. 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 McGraw−Hill/Osborne from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw−Hill/Osborne, or others, McGraw−Hill/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, 2001 2
  17. 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 pull off the .NET Framework, it would change software developmentat least for the Windows operating systemsforever. This book's publication is testimony to the achievement that is the .NET Framework and, in particular, Visual Basic .NET. Visual Basic .NET and the .NET Framework and I go back almost five years when I was an aggressive Java programmer (of course, the framework was just a nameless, mysterious OO project at MS then). While I worked with several flavors of Java, all my customers needed stuff for the Windows platform. Visual J++ was my tool of choice (especially after programming Delphi for a few years). Then, delegates (those so−called object−oriented function pointers) and other things that Microsoft was doing with Java hit a nerve center at the house of Sun. The result was the Intifada between the two software makers that stopped VJ++ in its tracks. For the next couple of years the Visual J++ Web site at Microsoft.com and "VJ" remained unchanged. The Java world moved on with newer stuff from Sun, and VJ and Windows Foundation Classes (WFC) languished in the lawsuits. I and tens of thousands of Windows VJ programmers lost a lot of faith in the software development business. Many of us felt passionate enough, and were hurt enough, to get some answers directly from Microsoft on where they were heading. At the end of 1998 I was told "if you can wait for our new object−oriented software to arrive you will really be pleased." My reply to that was "What should I do in the meantime?" "Go and develop with Visual Basic and we'll call you when it's ready." Not knowing what they were baking, I took their advice, and that's exactly what I did for the next few years. I went on to use VB for a number of major projects. While I developed some sophisticated stuff in VB, I also kept current with Java (not VJ). So it did not take me long to finally "get" what had "set" so many VB programmers to refusing to use anything else to develop their software (no matter how much certain technology lacking affected them). When Visual Basic .NET emerged I realized I had the best of both worlds everything I loved about VJ and VB in one package. Sure, I still have an affinity for curly braces, but no other language in the world is as productive as Visual Basic .NET, not even C#. And I tell you that as a programmer who has coded in more than just Java, Delphi, and Visual Basic . . . and C#. Well, it is finally here, and it's the best thing since strawberry−flavored cranberries. While chewing on my burger, I also reflected on what Microsoft had said to us earlier that day about how they would like us to write about the .NET Framework. The one statement that stood out was that they were keen to see books that did not simply rehash the tons of information that were going to be put out, or compete with the online material (which is excellent). They were looking for books that would help people understand not only the fundamentals of the framework and its many languages, but also how to use the extremely powerful and complex constructs and technologies they were going to unleash. It became apparent to me how different .NET in general and Visual Basic .NET in particular was going to be from the current version of the language (VB 6) and the current Windows software development paradigms and object models, such as COM and ActiveX. I was both surprised and amused at how familiar Visual Basic .NET and the .NET Framework now looked to Java, Visual J++ and Delphi. 3
  18. 18. Foreword I found myself in a unique position, having arrived at Visual Basic (I skipped the earlier versions and cut my teeth on VB 5 and 6) via C++, Delphi, and Java. I at once felt that Visual Basic .NET would appeal not only to existing VB developers (or scare some of them half to death) but to the many Java and Delphi programmers. The latter are of special interest to me because, like me, they are not only dedicated to the Windows platform, but they have been programming in true object−oriented languages for almost a decade now. After all, I learned Delphi back in the early 1990s, Java in 1995. Much of what's new to Visual Basic, like true OO and free threading, has been part of the Delphi and Java arsenal for many years. You will see that I talk not only to VB programmers (for the most part) but also to VJ and Delphi programmers. What you now have in Visual Basic .NET is the marvelous utility and simplicity of Visual Basic coupled with the incredible power of OO and Visual J++ (ten−fold). To put it in a nutshell: You can now do with Visual Basic .NET what you once only dreamed was possible, and do it faster than with any other language. In many cases, once you get up to speed with the syntax and semantics, you can just about write the software in your sleep. I have written a number of computer books, but it was a challenge to write a really good software development book. The proverbial hat goes off to all those guys and gals who have penned something for software developers. Having my own library of software development books, I know that you need to strike a balance between talking and showing. If you talk too much and don't provide enough examples, you lose your audience. If you show too much you end up alienating everyone and might as well be writing in hieroglyphics. I especially dislike books that provide a page of narrative and then throw 15 pages of unexplained code at you. I must admit I have never learned much about writing software from any of those tomes. It's also difficult to provide meaningful examples and enough of them. I believe the examples should be plucked from live code in functioning products. They should not be examples knocked up in Notepad that were obviously never compiled. While I have taken some shortcuts here and there to provide a quick and dirty example, every line of code written in this book has been written and compiled in Visual Studio .NET and then tested. You will also find that there is more narrative than code in many places. I have gone to great lengths to explore all the various facets of the constructs and technologies you will encounter as a .NET programmer. The reason is simple. Computer books don't seem to get thinner, and this is compounded by there being so much to talk about or explain. I decided that instead of publishing 1500 pages with hundreds of examples that cannot be compiled from wood pulp, or compiled on the train to work, that it would be better to put as much extra code as possible into the various projects that were created with this book. A giant Visual Studio .NET solution, Vb7cr, is included with this book that by publication will have been culled from more than 50 projects, some of them implemented for commercial applications. You will find a lot in the projects, from the linked list classes to a full−blown ASP.NET application that implements a sophisticated search facility, and more. They have all been compiled and tested with the released version of Visual Studio and the .NET Framework. You can download the software from www.osborne.com and www.sdamag.com. It's just code and downloads easily on a 56K connection, so Osborne and I deemed it unnecessary to come out with a CD. Of particular interest in the solution is a complete binary tree example, which extends the linked list project covered in the book. Rather than publishing the 50 pages this particular project took, the code has been fully tested and is available in the first version of the solution. Not only will you find the classes useful for building sophisticated data structures, but the project provides an excellent learning tool for object−oriented enthusiasts. It at once provides grounding in OO, inheritance, aggregation, composition, and polymorphism, 4

×