Visual Basic 2008 offers powerful new features, and Accelerated VB 2008 is the fastest path to mastering them, and the rest of Visual Basic, for both experienced Visual Basic programmers moving to VB 2008 and programmers moving to Visual Basic from another objectoriented language. Many books introduce VB, but very few also explain how to use it optimally with the .NET common language runtime (CLR). This book teaches both core Visual Basic language concepts and how to wisely employ VB idioms and objectoriented design patterns to exploit the power of VB and the CLR. Accelerated VB 2008 is both a rapid tutorial and a permanent reference. You’ll quickly master VB syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code VB when this book will show you how to do things the right way, right from the start? Comprehensively and concisely explains both Visual Basic 2005 and Visual Basic 2008 features Focuses on the language itself and on how to use Visual Basic 2008 proficiently for all .NET application development Concentrates on how VB features work and how to best use them for robust, highperformance code What you’ll learn How VB works with and exploits the CLR How to use arrays and collections How to handle events with delegates How to design and use generic types and methods How to thread efficiently and robustly How the new VB 2008 anonymous types, lamba expressions, and extension methods work and how to use them Who this book is for If you’re an experienced VB programmer, you need to understand how VB has changed with VB 2008. If youre an experienced objectoriented programmer moving to VB, you want to ramp up quickly in the language while learning the latest features and techniques. In either case, this book is for you. The first three chapters succinctly present VB fundamentals for those new to or reviewing VB. The rest of the book covers all the major VB features in great detail, explaining how they work and how best to use them. Whatever your background or need, you'll treasure this book for as long as you code in VB 2008. Table of Contents VB 2008 Overview VB 2008 Syntax Classes and Structures Methods, Properties, and Fields VB 2008 and the CLR Interfaces Operator Overloading Exception Handling Working with Strings Arrays and Collections Delegates and Events Generics Threading VB 2008 Best Practices LINQ with VB 2008 Accelerated VB 2008......Page 1 Contents at a Glance......Page 7 Contents......Page 9 About the Authors......Page 13 About the Technical Reviewers......Page 15 Acknowledgments......Page 17 About This Book......Page 19 .NET Runtime......Page 23 VB 2008 and C# 3.0......Page 24 Common Type System......Page 25 What’s New in VB 2008......Page 27 Object Initializers......Page 28 LINQ to XML......Page 29 Lambda Expressions (Inline Functions)......Page 30 Nullable Types, Enhanced in VB 2008......Page 31 Summary......Page 32 Strong Typing......Page 33 Type Categories......Page 35 Value Types......Page 36 Enumerations......Page 38 Reference Types......Page 39 Type Conversion......Page 41 CType......Page 42 Boxing......Page 44 Reference Type Operators......Page 45 Namespaces......Page 47 Defining Namespaces......Page 48 Using Namespaces......Page 49 Object Browser......Page 50 If . . . Then . . . Else......Page 51 Select . . . Case......Page 52 For Each . . . Next......Page 53 Do While and Do Until......Page 54 Summary......Page 55 Class Definitions......Page 57 Constructors......Page 58 Accessibility......Page 59 Interfaces......Page 60 MyBase and MyClass Keywords......Page 61 MustInherit Classes......Page 64 Nested Classes......Page 65 Item Property Indexers......Page 68 Value Type Definitions......Page 70 Constructors......Page 71 The Meaning of Me......Page 72 Interfaces......Page 73 Boxing and Unboxing......Page 74 When Boxing Occurs......Page 77 Efficiency and Confusion......Page 78 System.Object......Page 79 Using New with Value Types......Page 80 Using New with Reference Types......Page 81 Shared Constructor......Page 82 Instance Constructor and Creation Ordering......Page 83 Finalizers......Page 84 The IDisposable Interface......Page 85 The Using Keyword......Page 87 Summary......Page 89 Shared Methods......Page 91 Instance Methods......Page 92 ByRef Arguments......Page 93 ParamArray......Page 95 Overridable and MustOverride Methods......Page 96 Overrides and Shadows......Page 97 Properties......Page 99 Declaring Properties......Page 100 Read-Only, Write-Only, and Read-Write Properties......Page 101 Fields......Page 102 Field Initialization......Page 104 Summary......Page 106 From VB to IL......Page 107 From IL to Platform......Page 109 Understanding Assemblies......Page 110 Assembly Management......Page 113 Shared Assemblies and the Global Assembly Cache......Page 114 Loading Assemblies......Page 116 Reflection......Page 117 Summary......Page 118 Interfaces Are Reference Types......Page 119 Defining Interfaces......Page 120 What Can Be in an Interface?......Page 122 Implementing Multiple Interfaces......Page 123 Hiding Interface Members......Page 124 Implementing Interfaces in Structures......Page 125 Using a Generic Interface......Page 127 Using a Generic Method in an Interface......Page 128 Implementing Contracts with Classes......Page 129 Implementing Contracts with Interfaces......Page 131 Choosing Between Interfaces and Classes......Page 132 Polymorphism with Interfaces......Page 136 Summary......Page 137 Operators That Can Be Overloaded......Page 139 Types and Formats of Overloaded Operators......Page 140 Operators Shouldn’t Mutate Their Operands......Page 141 Does Parameter Order Matter?......Page 142 Overloading the Addition Operator......Page 143 Comparison Operators......Page 144 Boolean Comparisons......Page 148 Conversion Operators......Page 151 Overloading the CType Operator......Page 152 Summary......Page 154 Handling Exceptions......Page 155 Syntax Overview of the Try Statement......Page 156 Unhandled Exceptions in .NET 3.5......Page 158 Re-throwing Exceptions and Translating Exceptions......Page 159 Exceptions Thrown in Finally Blocks......Page 161 Exceptions Thrown in Finalizers......Page 162 Exceptions Thrown in Shared Constructors......Page 163 Basic Structure of Exception-Neutral Code......Page 165 Constrained Execution Regions......Page 170 Critical Finalizers and SafeHandle......Page 172 Creating Custom Exception Classes......Page 177 Working with Allocated Resources and Exceptions......Page 180 Providing Rollback Behavior......Page 184 Summary......Page 187 String Overview......Page 189 String Literals......Page 190 Object.ToString(), IFormattable, and CultureInfo......Page 191 Creating and Registering Custom CultureInfo Types......Page 193 Format Strings......Page 195 Console.WriteLine() and String.Format()......Page 196 Examples of String Formatting in Custom Types......Page 197 ICustomFormatter......Page 199 Comparing Strings......Page 202 Working with Strings from Outside Sources......Page 203 StringBuilder......Page 206 Searching Strings with Regular Expressions......Page 207 Searching with Regular Expressions......Page 208 Searching and Grouping......Page 209 Replacing Text with Regex......Page 213 Regex Creation Options......Page 216 Summary......Page 218 Introduction to Arrays......Page 219 Type Convertibility......Page 220 Array Covariance......Page 221 Multidimensional Arrays......Page 222 Multidimensional Jagged Arrays......Page 224 Comparing ICollection(Of T) with ICollection......Page 225 Collection Synchronization......Page 227 Lists......Page 228 System.Collections.ObjectModel......Page 230 How Iteration Works......Page 233 Summary......Page 237 Overview of Delegates......Page 239 Single Delegate......Page 240 Delegate Chaining......Page 242 Iterating Through Delegate Chains......Page 244 Open-Instance Delegates......Page 246 Strategy Pattern......Page 250 Events......Page 251 Custom Events......Page 254 Events and Relaxed Delegates......Page 255 Summary......Page 257 Introduction to Generics......Page 259 Efficiency and Type Safety of Generics......Page 260 Generic Type Definitions and Constructed Types......Page 262 Generic Classes and Structures......Page 263 Generic Interfaces......Page 264 Generic Methods......Page 265 Generic Delegates......Page 267 Generic Type Conversion......Page 270 Generic Nullable Types......Page 271 Constraints......Page 273 Generic System Collections......Page 277 Conversion and Operators Within Generic Types......Page 279 Creating Constructed Types Dynamically......Page 290 Summary......Page 292 Threading in VB 2008 and .NET 3.5......Page 293 Starting Threads......Page 294 States of a Thread......Page 297 Terminating Threads......Page 299 Halting and Waking Threads......Page 301 Foreground and Background Threads......Page 302 Thread-Local Storage......Page 303 Synchronizing Threads......Page 307 Lightweight Synchronization with the Interlocked Class......Page 308 Monitor Class......Page 313 Beware of Boxing......Page 317 Pulse and Wait......Page 318 Locking Objects......Page 322 ReaderWriterLock......Page 323 ReaderWriterLockSlim......Page 325 Mutex......Page 327 Events......Page 328 Win32 Synchronization Objects and WaitHandle......Page 329 Using the Thread Pool......Page 331 The IOU Pattern......Page 332 Asynchronous Method Calls......Page 333 Timers......Page 337 Summary......Page 338 Default to NotInheritable Classes......Page 339 Use the NVI Pattern......Page 340 Is the Object Cloneable?......Page 342 Is the Object Formattable?......Page 348 Is the Object Convertible?......Page 351 Does the Object Support Ordering?......Page 353 Is the Object Disposable?......Page 356 Does the Object Need a Finalizer?......Page 359 Reference Types and Identity Equality......Page 366 Overriding Object.Equals() for Reference Types......Page 369 If You Override Equals(), Override GetHashCode()......Page 372 Prefer Type Safety at All Times......Page 375 Using Immutable Reference Types......Page 380 Value-Type Best Practices......Page 384 Override Equals() for Better Performance......Page 385 Implement Type-Safe Forms of Interface Members and Derived Methods......Page 390 Summary......Page 394 Query Comprehensions......Page 395 Distinct......Page 396 Anonymous Types and Type Inference......Page 397 Extension Methods......Page 398 Inline Function......Page 400 From......Page 401 Order By......Page 402 Min, Max, and Count......Page 403 Count Using a Lambda Expression......Page 404 Let......Page 405 A LINQ Subquery......Page 406 Using LINQ to Find Your Documents......Page 407 Query Evaluation......Page 408 Create an XML Document......Page 411 Add Elements to an XML Document......Page 412 Save an XML Document......Page 414 Create an XML Document......Page 415 Add Elements to an XML Document......Page 416 Add Elements to a Specific Location in an XML Document......Page 417 Save an XML Document......Page 420 From......Page 421 Select......Page 422 Where......Page 423 Join......Page 424 Using LINQ to Create an Excel Spreadsheet......Page 425 LINQ to SQL......Page 427 Musician Data......Page 428 Genre Class......Page 430 Musician Class......Page 432 Create Musicians......Page 433 Update a Musician......Page 435 Delete a Musician......Page 436 Review Generated SQL......Page 437 Summary......Page 438 Books......Page 439 Web......Page 440 Console Applications......Page 443 A Few Words Regarding Modules......Page 444 Index......Page 445 Accelerated VB 9.0 is the fastest path to VB mastery. All VB programmers need to know and understand how VB really works but very few books address this. No other book covers the topic in the depth that this book does. It teaches both core VB language concepts and how to use them in high-performance code. All programmers moving to VB from any language or moving up to VB 9.0 from VB 2005 will find this book well worth buying, reading, and using as a reference.