DOC

Visual+Basic+Language+Specification+9.0

By Clarence Gibson,2014-11-14 17:02
9 views 0
Visual+Basic+Language+Specification+9.0

The Microsoft Visual Basic

    Language Specification

    Version 9.0

    Paul Vick

    Microsoft Corporation

    Copyright ? Microsoft Corporation 2007. All Rights Reserved. Please send corrections, comments, and other feedback to basic@microsoft.com.

    The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

    This Language Specification is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

    Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

     2007 Microsoft Corporation. All rights reserved.

    Microsoft, MS-DOS, Visual Basic, Windows 2000, Windows 95, Windows 98, Windows ME, Windows NT, Windows XP, Windows Vista and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

    Table of Contents

    Table of Contents

1. Introduction .................................................................................................................................................. 1

    1.1 Grammar Notation .................................................................................................................................. 1 1.2 Compatibility ........................................................................................................................................... 2 1.2.1 Kinds of compatibility breaks ............................................................................................................ 2 1.2.2 Impact Criteria.................................................................................................................................. 3 1.2.3 Language deprecation ...................................................................................................................... 3 2. Lexical Grammar ........................................................................................................................................... 5

    2.1 Characters and Lines................................................................................................................................ 5 2.1.1 Line Terminators .............................................................................................................................. 5 2.1.2 Line Continuation ............................................................................................................................. 5 2.1.3 White Space ..................................................................................................................................... 6 2.1.4 Comments ........................................................................................................................................ 6 2.2 Identifiers ................................................................................................................................................ 6 2.2.1 Type Characters ................................................................................................................................ 7 2.3 Keywords ................................................................................................................................................ 8 2.4 Literals .................................................................................................................................................. 10 2.4.1 Boolean Literals .............................................................................................................................. 10 2.4.2 Integer Literals ............................................................................................................................... 10 2.4.3 Floating-Point Literals ..................................................................................................................... 11 2.4.4 String Literals.................................................................................................................................. 12 2.4.5 Character Literals ........................................................................................................................... 12 2.4.6 Date Literals ................................................................................................................................... 13 2.4.7 Nothing .......................................................................................................................................... 14 2.5 Separators ............................................................................................................................................. 14 2.6 Operator Characters .............................................................................................................................. 14 3. Preprocessing Directives ............................................................................................................................. 15

    3.1 Conditional Compilation ........................................................................................................................ 15 3.1.1 Conditional Constant Directives ...................................................................................................... 16 3.1.2 Conditional Compilation Directives ................................................................................................. 17 3.2 External Source Directives ..................................................................................................................... 18 3.3 Region Directives ................................................................................................................................... 18 3.4 External Checksum Directives ................................................................................................................ 19 4. General Concepts ........................................................................................................................................ 21

    4.1 Declarations .......................................................................................................................................... 21 4.1.1 Overloading and Signatures ............................................................................................................ 22 4.2 Scope .................................................................................................................................................... 23 4.3 Inheritance ............................................................................................................................................ 24 4.3.1 MustInherit and NotInheritable Classes .......................................................................................... 25

    Copyright ? Microsoft Corporation 2007. All Rights Reserved. i

Visual Basic Language Specification

    4.3.2 Interfaces and Multiple Inheritance ................................................................................................ 26 4.3.3 Shadowing ...................................................................................................................................... 27 4.4 Implementation ..................................................................................................................................... 32 4.4.1 Implementing Methods .................................................................................................................. 35 4.5 Polymorphism ....................................................................................................................................... 36 4.5.1 Overriding Methods ....................................................................................................................... 38 4.6 Accessibility ........................................................................................................................................... 41 4.6.1 Constituent Types ........................................................................................................................... 44 4.7 Type and Namespace Names ................................................................................................................. 44 4.7.1 Qualified Name Resolution ............................................................................................................. 45 4.7.2 Unqualified Name Resolution ......................................................................................................... 46 4.8 Variables ............................................................................................................................................... 47 4.9 Generic Types and Methods .................................................................................................................. 47 4.9.1 Type Parameters ............................................................................................................................ 48 4.9.2 Type Constraints ............................................................................................................................. 50 5. Attributes .................................................................................................................................................... 55

    5.1 Attribute Classes.................................................................................................................................... 56 5.2 Attribute Blocks ..................................................................................................................................... 57 5.2.1 Attribute Names ............................................................................................................................. 59 5.2.2 Attribute Arguments....................................................................................................................... 59 6. Source Files and Namespaces ..................................................................................................................... 63

    6.1 Program Startup and Termination ......................................................................................................... 63 6.2 Compilation Options .............................................................................................................................. 64 6.2.1 Option Explicit Statement ............................................................................................................... 64 6.2.2 Option Strict Statement .................................................................................................................. 65 6.2.3 Option Compare Statement ............................................................................................................ 65 6.2.4 Integer Overflow Checks ................................................................................................................. 66 6.2.5 Option Infer Statement................................................................................................................... 66 6.3 Imports Statement ................................................................................................................................ 66 6.3.1 Import Aliases ................................................................................................................................ 67 6.3.2 Namespace Imports........................................................................................................................ 69 6.3.3 XML Namespace Imports ................................................................................................................ 71 6.4 Namespaces .......................................................................................................................................... 71 6.4.1 Namespace Declarations ................................................................................................................ 72 6.4.2 Namespace Members ..................................................................................................................... 72 7. Types........................................................................................................................................................... 75

    7.1 Value Types and Reference Types .......................................................................................................... 75 7.1.1 Nullable Value Types ...................................................................................................................... 76 7.2 Interface Implementation ...................................................................................................................... 77 7.3 Primitive Types ...................................................................................................................................... 78 7.4 Enumerations ........................................................................................................................................ 79 7.4.1 Enumeration Members ................................................................................................................... 80 7.4.2 Enumeration Values ....................................................................................................................... 80 7.5 Classes................................................................................................................................................... 81 7.5.1 Class Base Specification .................................................................................................................. 83 ii Copyright ? Microsoft Corporation 2007. All Rights Reserved.

    Table of Contents

    7.5.2 Class Members ............................................................................................................................... 83 7.6 Structures .............................................................................................................................................. 84 7.6.1 Structure Members ........................................................................................................................ 85 7.7 Standard Modules ................................................................................................................................. 86 7.7.1 Standard Module Members ............................................................................................................ 86 7.8 Interfaces .............................................................................................................................................. 87 7.8.1 Interface Inheritance ...................................................................................................................... 88 7.8.2 Interface Members ......................................................................................................................... 89 7.9 Arrays .................................................................................................................................................... 90 7.10 Delegates ............................................................................................................................................ 92 7.11 Partial types ........................................................................................................................................ 93 7.12 Constructed Types ............................................................................................................................... 95 7.12.1 Open Types and Closed Types ....................................................................................................... 96 7.13 Special Types ....................................................................................................................................... 96 8. Conversions ................................................................................................................................................ 99

    8.1 Implicit and Explicit Conversions ............................................................................................................ 99 8.2 Boolean Conversions ............................................................................................................................. 99 8.3 Numeric Conversions ........................................................................................................................... 100 8.4 Reference Conversions ........................................................................................................................ 101 8.5 Array Conversions ............................................................................................................................... 101 8.6 Value Type Conversions ....................................................................................................................... 102 8.6.1 Nullable Value Type Conversions .................................................................................................. 105 8.7 String Conversions ............................................................................................................................... 106 8.8 Widening Conversions ......................................................................................................................... 106 8.9 Narrowing Conversions........................................................................................................................ 108 8.10 Type Parameter Conversions ............................................................................................................. 109 8.11 User-Defined Conversions ................................................................................................................. 110 8.11.1 Most Specific Widening Conversion ............................................................................................ 112 8.11.2 Most Specific Narrowing Conversion .......................................................................................... 112

    8.12 Native Conversions ............................................................................................................................ 113 8.13 Dominant Type .................................................................................................................................. 114 9. Type Members .......................................................................................................................................... 115

    9.1 Interface Method Implementation ...................................................................................................... 115 9.2 Methods .............................................................................................................................................. 117 9.2.1 Regular Method Declarations ....................................................................................................... 119 9.2.2 External Method Declarations ...................................................................................................... 120 9.2.3 Overridable Methods ................................................................................................................... 122 9.2.4 Shared Methods ........................................................................................................................... 123 9.2.5 Method Parameters ..................................................................................................................... 124

    9.2.5.1 Value Parameters .................................................................................................................. 124

    9.2.5.2 Reference Parameters ........................................................................................................... 125

    9.2.5.3 Optional Parameters ............................................................................................................. 127

    9.2.5.4 ParamArray Parameters ........................................................................................................ 127 9.2.6 Event Handling ............................................................................................................................. 128 9.2.7 Extension Methods ....................................................................................................................... 129 9.2.8 Partial Methods ............................................................................................................................ 133 Copyright ? Microsoft Corporation 2007. All Rights Reserved. iii

Visual Basic Language Specification

    9.3 Constructors ........................................................................................................................................ 134 9.3.1 Instance Constructors ................................................................................................................... 134 9.3.2 Shared Constructors ..................................................................................................................... 136 9.4 Events ................................................................................................................................................. 138 9.4.1 Custom Events .............................................................................................................................. 140 9.5 Constants ............................................................................................................................................ 142 9.6 Instance and Shared Variables ............................................................................................................. 143 9.6.1 Read-Only Variables ..................................................................................................................... 145 9.6.2 WithEvents Variables .................................................................................................................... 146 9.6.3 Variable Initializers ....................................................................................................................... 147

    9.6.3.1 Regular Initializers ................................................................................................................. 150

    9.6.3.2 Object Initializers ................................................................................................................... 150

    9.6.3.3 Array-Size Initializers ............................................................................................................. 151

    9.6.3.4 Array-Element Initializers ...................................................................................................... 151 9.6.4 System.MarshalByRefObject Classes ............................................................................................. 153 9.7 Properties............................................................................................................................................ 153 9.7.1 Get Accessor Declarations ............................................................................................................ 158 9.7.2 Set Accessor Declarations ............................................................................................................. 159 9.7.3 Default Properties ........................................................................................................................ 160 9.8 Operators ............................................................................................................................................ 161 9.8.1 Unary Operators ........................................................................................................................... 163 9.8.2 Binary Operators .......................................................................................................................... 163 9.8.3 Conversion Operators ................................................................................................................... 164 9.8.4 Operator Mapping ........................................................................................................................ 166 10. Statements .............................................................................................................................................. 167

    10.1 Blocks and Labels ............................................................................................................................... 167 10.1.1 Local Variables and Parameters .................................................................................................. 168 10.2 Local Declaration Statements ............................................................................................................ 170 10.2.1 Implicit Local Declarations .......................................................................................................... 172 10.3 With Statement ................................................................................................................................. 173 10.4 SyncLock Statement........................................................................................................................... 173 10.5 Event Statements .............................................................................................................................. 174 10.5.1 RaiseEvent Statement................................................................................................................. 174 10.5.2 AddHandler and RemoveHandler Statements ............................................................................. 175

    10.6 Assignment Statements ..................................................................................................................... 176 10.6.1 Regular Assignment Statements ................................................................................................. 176 10.6.2 Compound Assignment Statements ............................................................................................ 178

    10.6.3 Mid Assignment Statement ........................................................................................................ 179 10.7 Invocation Statements ....................................................................................................................... 179 10.8 Conditional Statements ..................................................................................................................... 179 10.8.1 If...Then...Else Statements .......................................................................................................... 179 10.8.2 Select Case Statements ............................................................................................................... 180 10.9 Loop Statements ............................................................................................................................... 182 10.9.1 While...End While and Do...Loop Statements .............................................................................. 182

    10.9.2 For...Next Statements ................................................................................................................. 183 10.9.3 For Each...Next Statements......................................................................................................... 184 10.10 Exception-Handling Statements ....................................................................................................... 187 iv Copyright ? Microsoft Corporation 2007. All Rights Reserved.

    Table of Contents

    10.10.1 Structured Exception-Handling Statements............................................................................... 187

    10.10.1.1 Finally Blocks ..................................................................................................................... 188

    10.10.1.2 Catch Blocks ...................................................................................................................... 188

    10.10.1.3 Throw Statement ............................................................................................................... 189 10.10.2 Unstructured Exception-Handling Statements .......................................................................... 190

    10.10.2.1 Error Statement ................................................................................................................. 190

    10.10.2.2 On Error Statement ........................................................................................................... 191

    10.10.2.3 Resume Statement ............................................................................................................ 191 10.11 Branch Statements .......................................................................................................................... 192 10.12 Array-Handling Statements .............................................................................................................. 193 10.12.1 ReDim Statement ..................................................................................................................... 193 10.12.2 Erase Statement ....................................................................................................................... 194 10.13 Using statement .............................................................................................................................. 194 11. Expressions ............................................................................................................................................. 197

    11.1 Expression Classifications .................................................................................................................. 197 11.1.1 Expression Reclassification ......................................................................................................... 198 11.2 Constant Expressions ......................................................................................................................... 200 11.3 Late-Bound Expressions ..................................................................................................................... 200 11.4 Simple Expressions ............................................................................................................................ 201 11.4.1 Literal Expressions ...................................................................................................................... 201 11.4.2 Parenthesized Expressions .......................................................................................................... 202 11.4.3 Instance Expressions ................................................................................................................... 202 11.4.4 Simple Name Expressions ........................................................................................................... 203 11.4.5 AddressOf Expressions................................................................................................................ 205 11.5 Type Expressions ............................................................................................................................... 205 11.5.1 GetType Expressions ................................................................................................................... 205 11.5.2 TypeOf...Is Expressions ............................................................................................................... 206 11.5.3 Is Expressions ............................................................................................................................. 206 11.5.4 GetXmlNamespace Expressions .................................................................................................. 207 11.6 Member Access Expressions .............................................................................................................. 207 11.6.1 Identical Type and Member Names ............................................................................................ 210 11.6.2 Default Instances ........................................................................................................................ 210

    11.6.2.1 Default Instances and Type Names ...................................................................................... 211

    11.6.2.2 Group Classes ...................................................................................................................... 212 11.6.3 Extension Method Collection ...................................................................................................... 213 11.7 Dictionary Member Access ................................................................................................................ 217 11.8 Invocation Expressions ...................................................................................................................... 217 11.8.1 Overloaded Method Resolution .................................................................................................. 218 11.8.2 Applicable Methods .................................................................................................................... 226 11.8.3 Passing Parameters .................................................................................................................... 227 11.8.4 Conditional Methods .................................................................................................................. 228 11.8.5 Type Argument Inference ........................................................................................................... 228 11.9 Index Expressions .............................................................................................................................. 230 11.10 New Expressions .............................................................................................................................. 231 11.10.1 Object-Creation Expressions ..................................................................................................... 231 11.10.2 Array-Creation Expressions ....................................................................................................... 233 11.10.3 Delegate-Creation Expressions ................................................................................................. 234 Copyright ? Microsoft Corporation 2007. All Rights Reserved. v

Visual Basic Language Specification

    11.10.4 Anonymous Object-Creation Expressions .................................................................................. 237

    11.11 Cast Expressions .............................................................................................................................. 240 11.12 Operator Expressions ....................................................................................................................... 241 11.12.1 Operator Precedence and Associativity ..................................................................................... 241 11.12.2 Object Operands....................................................................................................................... 242 11.12.3 Operator Resolution ................................................................................................................. 242 11.13 Arithmetic Operators ....................................................................................................................... 245 11.13.1 Unary Plus Operator ................................................................................................................. 245 11.13.2 Unary Minus Operator .............................................................................................................. 245 11.13.3 Addition Operator .................................................................................................................... 246 11.13.4 Subtraction Operator ................................................................................................................ 247 11.13.5 Multiplication Operator ............................................................................................................ 248 11.13.6 Division Operators .................................................................................................................... 248 11.13.7 Mod Operator .......................................................................................................................... 250 11.13.8 Exponentiation Operator .......................................................................................................... 251 11.14 Relational Operators ........................................................................................................................ 252 11.15 Like Operator ................................................................................................................................... 254 11.16 Concatenation Operator .................................................................................................................. 255 11.17 Logical Operators ............................................................................................................................. 256 11.17.1 Short-circuiting Logical Operators ............................................................................................. 258 11.18 Shift Operators ................................................................................................................................ 259 11.19 Boolean Expressions ........................................................................................................................ 260 11.20 Lambda Expressions ........................................................................................................................ 261 11.20.1 Closures .................................................................................................................................... 262 11.21 Query Expressions ........................................................................................................................... 265 11.21.1 Range Variables ........................................................................................................................ 265 11.21.2 Queryable Types ....................................................................................................................... 266 11.21.3 Default Query Indexer .............................................................................................................. 268 11.21.4 From Query Operator ............................................................................................................... 269 11.21.5 Join Query Operator ................................................................................................................. 270 11.21.6 Let Query Operator .................................................................................................................. 271 11.21.7 Select Query Operator .............................................................................................................. 271 11.21.8 Distinct Query Operator ........................................................................................................... 272 11.21.9 Where Query Operator ............................................................................................................. 273 11.21.10 Partition Query Operators ...................................................................................................... 273 11.21.11 Order By Query Operator........................................................................................................ 274 11.21.12 Group By Query Operator ....................................................................................................... 275 11.21.13 Aggregate Query Operator ..................................................................................................... 277 11.21.14 Group Join Query Operator..................................................................................................... 278 11.22 Conditional Expressions ................................................................................................................... 279 11.23 XML Literal Expressions ................................................................................................................... 280 11.23.1 Lexical rules .............................................................................................................................. 281 11.23.2 Embedded expressions ............................................................................................................. 282 11.23.3 XML Documents ....................................................................................................................... 282 11.23.4 XML Elements ........................................................................................................................... 284 11.23.5 XML Namespaces ..................................................................................................................... 286 11.23.6 XML Processing Instructions ..................................................................................................... 287 11.23.7 XML Comments ........................................................................................................................ 288 vi Copyright ? Microsoft Corporation 2007. All Rights Reserved.

    Table of Contents

    11.23.8 CDATA sections......................................................................................................................... 288 11.24 XML Member Access Expressions .................................................................................................... 288 12. Documentation Comments ..................................................................................................................... 291

    12.1 Documentation Comment Format ..................................................................................................... 291 12.2 Recommended tags ........................................................................................................................... 292 12.2.1 <c> ............................................................................................................................................. 292 12.2.2 <code> ....................................................................................................................................... 293 12.2.3 <example> .................................................................................................................................. 293 12.2.4 <exception> ................................................................................................................................ 293 12.2.5 <include> .................................................................................................................................... 294 12.2.6 <list> .......................................................................................................................................... 294 12.2.7 <para> ........................................................................................................................................ 295 12.2.8 <param> ..................................................................................................................................... 295 12.2.9 <paramref> ................................................................................................................................ 296 12.2.10 <permission> ............................................................................................................................ 296 12.2.11 <remarks> ................................................................................................................................ 296 12.2.12 <returns> .................................................................................................................................. 296 12.2.13 <see> ........................................................................................................................................ 297 12.2.14 <seealso> ................................................................................................................................. 297 12.2.15 <summary> .............................................................................................................................. 297 12.2.16 <typeparam> ............................................................................................................................ 298 12.2.17 <value> ..................................................................................................................................... 298 12.3 ID Strings ........................................................................................................................................... 298 12.3.1 ID string examples ...................................................................................................................... 299 12.4 Documentation comments example .................................................................................................. 302 13. Grammar Summary ................................................................................................................................. 309

    13.1 Lexical Grammar ................................................................................................................................ 309 13.1.1 Characters and Lines................................................................................................................... 309 13.1.2 Identifiers ................................................................................................................................... 309 13.1.3 Keywords ................................................................................................................................... 310 13.1.4 Literals ....................................................................................................................................... 310 13.2 Preprocessing Directives .................................................................................................................... 313 13.2.1 Conditional Compilation ............................................................................................................. 313 13.2.2 External Source Directives .......................................................................................................... 314 13.2.3 Region Directives ........................................................................................................................ 314 13.2.4 External Checksum Directives ..................................................................................................... 314 13.3 Syntactic Grammar ............................................................................................................................ 314 13.3.1 Attributes ................................................................................................................................... 315 13.3.2 Source Files and Namespaces ..................................................................................................... 315 13.3.3 Types .......................................................................................................................................... 317 13.3.4 Type Members ........................................................................................................................... 319 13.3.5 Statements ................................................................................................................................. 324 13.3.6 Expressions................................................................................................................................. 329 14. Change List .............................................................................................................................................. 339

    14.1 Version 7.1 to Version 8.0 .................................................................................................................. 339 Copyright ? Microsoft Corporation 2007. All Rights Reserved. vii

Visual Basic Language Specification

    14.1.1 Major changes ............................................................................................................................ 339 14.1.2 Minor changes ............................................................................................................................ 340 14.1.3 Clarifications/Errata .................................................................................................................... 341 14.1.4 Miscellaneous............................................................................................................................. 343 nd Edition) .............................................................................................. 343 14.2 Version 8.0 to Version 8.0 (2

    14.2.1 Minor changes ............................................................................................................................ 343 14.2.2 Clarifications/Errata .................................................................................................................... 344 14.2.3 Miscellaneous............................................................................................................................. 345 nd14.3 Version 8.0 (2 Edition) to Version 9.0 .............................................................................................. 345

    14.3.1 Major Changes ........................................................................................................................... 345 14.3.2 Minor Changes ........................................................................................................................... 346 14.3.3 Clarifications/Errata .................................................................................................................... 346 14.3.4 Miscellaneous............................................................................................................................. 347 viii Copyright ? Microsoft Corporation 2007. All Rights Reserved.

Report this document

For any questions or suggestions please email
cust-service@docsford.com