VB.NET_Language_in_a_Nutshell.pdf

(1745 KB) Pobierz
VB.NET Language in a Nutshell
VB .NET Language in a Nutshell
Steven Roman
Ron Petrusha
Paul Lomax
Publisher: O'Reilly
First Edition August 2001
ISBN: 0-596-00092-8, 654 pages
Need to make sense of the many changes to Visual Basic for the new .NET platform? VB .NET
Language in a Nutshell introduces the important aspects of the language and explains the .NET
framework. An alphabetical reference covers the functions, statements, directives, objects, and object
members that make up the VB .NET language. To ease the transition, each language element
includes a "VB .NET/VB 6 Differences" section.
1
16406355.001.png
Preface ................................................................................................................................ 14
Why Another VB Book? ........................................................................................... 26
Who This Book Is For ............................................................................................... 26
Readers New to Visual Basic ............................................................................. 26
VB and VBScript Developers New to VB .NET ............................................ 26
Existing VB .NET Developers ............................................................................ 27
How This Book Is Structured ................................................................................ 27
The Format of the Language Reference ....................................................... 28
Conventions Used in This Book ............................................................................ 29
How to Contact Us .................................................................................................... 30
Acknowledgments ..................................................................................................... 30
Part I: The Basics .............................................................................................................. 33
Chapter 1. Introduction ..................................................................................................... 33
1.1 Why VB .NET? ..................................................................................................... 34
1.2 What Is VB .NET? .............................................................................................. 37
1.2.1 Object Orientation ..................................................................................... 37
1.2.2 A Common Type System ........................................................................ 38
1.2.3 Access to System Services: The Framework Class Library ...... 39
1.2.4 A Common Runtime Environment ...................................................... 41
1.3 What Can You Do with VB .NET? ................................................................. 42
Chapter 2. Variables and Data Types ............................................................................ 43
2.1 Variables ............................................................................................................... 43
2.1.1 Variable Scope ............................................................................................ 43
2.1.2 Variable Lifetime ........................................................................................ 45
2.2 Declaring Variables and Constants ............................................................. 47
2.3 Data Types ........................................................................................................... 48
2.3.1 Value and Reference Types ................................................................... 49
2.3.2 VB Data Types: A Summary ................................................................. 51
2.3.3 Simple Data Types in Visual Basic ...................................................... 54
2.3.4 Data Type Conversion ............................................................................. 62
2.4 Arrays ..................................................................................................................... 64
2.4.1 Definition of Array ..................................................................................... 64
2.4.2 Dimension of an Array ............................................................................. 64
2.4.3 Size of an Array ......................................................................................... 64
2.4.4 Arrays in VB .NET ...................................................................................... 65
2.5 Object Variables and Their Binding ............................................................ 67
2.5.1 Late Binding Versus Early Binding ...................................................... 68
2.6 The Collection Object ....................................................................................... 69
2.7 Parameters and Arguments ........................................................................... 70
2.7.1 Passing Arguments ................................................................................... 70
2.7.2 Passing Objects .......................................................................................... 71
2.7.3 Optional Arguments .................................................................................. 73
2.7.4 ParamArray .................................................................................................. 73
Chapter 3. Introduction to Object-Oriented Programming .......................................... 75
3.1 Why Learn Object-Oriented Techniques? ................................................ 75
2
3.2 Principles of Object-Oriented Programming ............................................ 75
3.2.1 Abstraction ................................................................................................... 75
3.2.2 Encapsulation .............................................................................................. 76
3.2.3 Interfaces ...................................................................................................... 77
3.3 Classes and Objects .......................................................................................... 79
3.3.1 Class Modules in VB .NET ....................................................................... 79
3.3.2 Class Members ............................................................................................ 79
3.3.3 The Public Interface of a VB .NET Class ........................................... 81
3.3.4 Objects ........................................................................................................... 81
3.3.5 Properties ...................................................................................................... 82
3.3.6 Instance and Shared Members ............................................................. 83
3.3.7 Class Constructors ..................................................................................... 84
3.3.8 Finalize, Dispose, and Garbage Collection ....................................... 85
3.4 Inheritance ........................................................................................................... 86
3.4.1 Permission to Inherit ................................................................................ 88
3.4.2 Overriding ..................................................................................................... 88
3.4.3 Rules of Inheritance .................................................................................. 89
3.4.4 MyBase, MyClass, and Me ...................................................................... 89
3.5 Interfaces, Abstract Members, and Classes ............................................ 91
3.5.1 Interfaces Revisited .................................................................................. 93
3.6 Polymorphism and Overloading ................................................................... 93
3.6.1 Overloading .................................................................................................. 93
3.6.2 Polymorphism .............................................................................................. 94
3.7 Scope and Accessibility in Class Modules ................................................. 95
Chapter 4. The .NET Framework: General Concepts .................................................. 97
4.1 Namespaces ......................................................................................................... 97
4.2 Common Language Runtime (CLR), Managed Code, and Managed
Data ................................................................................................................................. 97
4.3 Managed Execution ........................................................................................... 98
4.4 Assemblies ............................................................................................................ 98
4.5 Assemblies and VB .NET ............................................................................... 100
Chapter 5. The .NET Framework Class Library .......................................................... 103
5.1 The System Namespace ................................................................................ 104
5.1.1 Data Type Conversion ............................................................................ 104
5.1.2 The Array Class ......................................................................................... 105
5.1.3 The Math Class .......................................................................................... 106
5.1.4 The String Class ....................................................................................... 107
5.2 Other Namespaces .......................................................................................... 108
5.2.1 System.Collections .................................................................................. 110
5.2.2 System.Data .............................................................................................. 110
5.2.3 System.IO ................................................................................................... 111
5.2.4 System.Text.RegularExpressions ...................................................... 111
5.2.5 System.Windows.Forms ........................................................................ 113
Chapter 6. Delegates and Events ................................................................................. 115
6.1 Delegates ............................................................................................................ 115
6.1.1 Using a Delegate to Call a Method .................................................... 116
6.1.2 Using a Delegate as a Function Pointer .......................................... 117
3
6.2 Events and Event Binding ............................................................................ 118
6.2.1 Control-Related Events ......................................................................... 119
6.2.2 WithEvents ................................................................................................. 119
6.2.3 AddHandler ................................................................................................ 120
Chapter 7. Error Handling in VB .NET ......................................................................... 123
7.1 Error Detection and Error Handling .......................................................... 123
7.2 Runtime Error Handling ................................................................................ 124
7.2.1 Unstructured Error Handling ............................................................... 124
7.2.2 Structured Exception Handling .......................................................... 127
7.3 Dealing with Logical Errors .......................................................................... 131
7.3.1 Detecting Logical Errors ........................................................................ 131
7.3.2 Where to Handle a Logical Error ....................................................... 132
7.4 Error Constants ................................................................................................ 133
Part II: Reference ............................................................................................................ 135
Chapter 8. The Language Reference ....................................................................... 135
#Const Directive .................................................................................................. 137
#If...Then...#Else Directive ............................................................................ 139
#Region...#End Region Directive ................................................................. 141
Abs Function .......................................................................................................... 141
Acos Function ........................................................................................................ 143
AddHandler Statement ...................................................................................... 143
AddressOf Operator ............................................................................................ 144
AppActivate Procedure ...................................................................................... 145
Application Class .................................................................................................. 147
Application.CompanyName Property ........................................................... 148
Application.DoEvents Method ......................................................................... 149
Application.ExecutablePath Property ........................................................... 150
Application.ProductName Property ............................................................... 151
Application.ProductVersion Property ........................................................... 152
Array Class ............................................................................................................. 152
Array.BinarySearch Method ............................................................................ 153
Array.Copy Method ............................................................................................. 156
Array.IndexOf Method ....................................................................................... 158
Array.LastIndexOf Method ............................................................................... 159
Array.Reverse Method ....................................................................................... 161
Array.Sort Method ............................................................................................... 162
Asc, AscW Functions .......................................................................................... 164
Asin Function ......................................................................................................... 166
Atan Function ........................................................................................................ 167
See Also .................................................................................................................. 168
Atan2 Function ..................................................................................................... 168
Beep Procedure .................................................................................................... 169
Call Statement ...................................................................................................... 170
CallByName Function ......................................................................................... 172
CBool Function ...................................................................................................... 174
CByte Function ..................................................................................................... 175
CChar Function ..................................................................................................... 176
4
CDate Function ..................................................................................................... 177
CDbl Function ........................................................................................................ 178
CDec Function ....................................................................................................... 179
Ceiling Function .................................................................................................... 180
ChDir Procedure ................................................................................................... 181
ChDrive Procedure ............................................................................................... 182
Choose Function ................................................................................................... 184
Chr, ChrW Functions ........................................................................................... 185
CInt Function ......................................................................................................... 187
Class Statement ................................................................................................... 188
Clipboard Class ..................................................................................................... 190
Clipboard.GetDataObject Method .................................................................. 190
Clipboard.SetDataObject Method .................................................................. 192
CLng Function ........................................................................................................ 192
CObj Function ........................................................................................................ 194
Collection Class ..................................................................................................... 195
Collection.Add Method ....................................................................................... 198
Collection.Count Property ................................................................................. 199
Collection.Item Method ..................................................................................... 200
Collection.Remove Method ............................................................................... 202
ColorDialog Class ................................................................................................. 203
Command Function ............................................................................................. 205
Const Statement .................................................................................................. 207
Cos Function .......................................................................................................... 208
Cosh Function ........................................................................................................ 209
CreateObject Function ....................................................................................... 210
CShort Function .................................................................................................... 212
CSng Function ....................................................................................................... 214
CStr Function ......................................................................................................... 215
CType Function ..................................................................................................... 216
CurDir Function ..................................................................................................... 218
DateAdd Function ................................................................................................. 219
DateDiff Function ................................................................................................. 221
DatePart Function ................................................................................................ 224
DateSerial Function ............................................................................................. 226
DateString Property ............................................................................................ 228
DateValue Function ............................................................................................. 229
Day Function .......................................................................................................... 230
DDB Function ......................................................................................................... 231
Debug Class ........................................................................................................... 233
Debug.Assert Method ......................................................................................... 234
Debug.AutoFlush Property ............................................................................... 235
Debug.Close Method ........................................................................................... 235
Debug.Flush Method ........................................................................................... 236
Debug.Indent Method ........................................................................................ 236
Debug.IndentLevel Property ............................................................................ 237
Debug.IndentSize Property .............................................................................. 237
5
Zgłoś jeśli naruszono regulamin