Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

pro vb 2010 and the .net 4.0 platform
Nội dung xem thử
Mô tả chi tiết
this print for content only—size & color not accurate
Agarwal
Troelsen
CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
Companion
eBook Available
BOOKS FOR PROFESSIONALS BY PROFESSIONALS®
Pro VB 2010 and the .NET 4
Platform
Dear Reader,
With the core VB language stable, the 4th edition of the .NET platform has
brought with it a massive wealth of new functionality. The new edition of this
book is your guide to learning the ins and outs of the .NET 4 platform and
Visual Studio 2010, including:
• the Dynamic Language Runtime (DLR)
• the Task Parallel Library (TPL, including PLINQ)
• the completely revamped WF 4 API
• the ADO.NET Entity Framework (as well as LINQ to EF)
• the expansion of the Windows Presentation Foundation (WPF) API
• improved COM interop
If you’re checking out this book for the first time, understand that it targets
experienced software professionals and students of computer science—so
please don’t expect three chapters devoted to “variable scope” here! The
mission of this text is to provide you with a rock-solid foundation in the VB
programming language (including a full discussion of OOP) and the critical
aspects of the .NET 4 platform.
Once you digest the information presented in the book you hold in your
hands, you’ll be in a perfect position to apply this knowledge to your specific
programming assignments, and you’ll be well-equipped to explore the .NET
universe on your own terms.
Andrew Troelsen and Vidya Vrat Agarwal
US $59.99
Shelve in:
Programming/VB
User level:
Intermediate–Advanced
THE APRESS ROADMAP
Pro
ASP.NET 4 in VB 2010
Introducing
.NET 4.0
Pro
Windows Azure
Pro Dynamic
.NET 4.0 Applications
Pro
VB 2010 and the
.NET 4 Platform
Pro
WPF in VB 2010
www.apress.com
SOURCE CODE ONLINE
Companion eBook
See last page for details
on $10 eBook version
ISBN 978-1-4302-2985-8
9 781430 229858
5 59 9 9
Vidya Vrat Agarwal, Author of
Pro ASP.NET 3.5 in VB 2008
Beginning C# 2008 Databases
Beginning VB 2008 Databases
spine = 2.15625" 1800 page count 840 PPI
THE EXPERT’S VOICE® IN .NET
Pro
VB 2010
.NET 4 Platform
Andrew Troelsen and Vidya Vrat Agarwal
Exploring the .NET universe with
Visual Basic 2010
and
the
VB
Pro
2010
and the Platform .NET 4
Andrew Troelsen, Author of
Pro C# 2010 and the .NET 4
Platform
Pro VB 2008 and the .NET 3.5
Platform
Pro Expression Blend 4
Expert ASP.NET 2.0
Advanced Application Design
www.it-ebooks.info
www.it-ebooks.info
Pro VB 2010 and
the .NET 4 Platform
■ ■ ■
Andrew Troelsen
and Vidya Vrat Agarwal
www.it-ebooks.info
Pro VB 2010 and the .NET 4 Platform
Copyright © 2010 by Andrew Troelsen and Vidya Vrat Agarwal
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-2985-8
ISBN-13 (electronic): 978-1-4302-2986-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of
the trademark owner, with no intention of infringement of the trademark.
President and Publisher: Paul Manning
Lead Editor: Ewan Buckingham
Technical Reviewer: Andy Olsen
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Coordinating Editor: Debra Kelly
Copy Editors: Mary Behr, Patrick Meader, Katie Stence, and Sharon Terdeman
Production Support: Patrick Cunningham
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or
visit www.springeronline.com.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have
any liability to any person or entity with respect to any loss or damage caused or alleged to be caused
directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com.
www.it-ebooks.info
To the memory of my wonderful grandmother, Maxine. I love you, Gerta.
Rest peacefully.
–Andrew Troelsen
To my lovely wife, sweet daughters, and my parents. I love you all.
–Vidya Vrat Agarwal
www.it-ebooks.info
iv
Contents at a Glance
About the Authors ..................................................................................................... liv
About the Technical Reviewer ................................................................................... lv
Acknowledgments .................................................................................................... lvi
Introduction ............................................................................................................. lvii
■Chapter 1: Introducing VB 2010 .............................................................................. 1
■Chapter 2: Building Visual Basic 2010 Applications ............................................. 41
■Chapter 3: Core VB 2010 Programming Constructs, Part I .................................... 71
■Chapter 4: Core VB 2010 Programming Constructs, Part II ................................. 125
■Chapter 5: Defining Encapsulated Class Types .................................................... 169
■Chapter 6: Understanding Inheritance and Polymorphism .................................. 227
■Chapter 7: Understanding Structured Exception Handling .................................. 273
■Chapter 8: Understanding Object Lifetime ........................................................... 303
■Chapter 9: Working with Interfaces ..................................................................... 333
■Chapter 10: Understanding Generics ................................................................... 375
■Chapter 11: Delegates, Events, and Lambdas ..................................................... 415
■Chapter 12: Advanced VB 2010 Language Features ............................................ 461
■Chapter 13: LINQ to Objects ................................................................................. 507
■Chapter 14: Configuring .NET Assemblies ........................................................... 543
■Chapter 15: Type Reflection, Late Binding,
■and Attribute-Based Programming ..................................................................... 605
www.it-ebooks.info
■ CONTENTS AT A GLANCE
v
■Chapter 16: Processes, AppDomains, and Object Contexts ................................. 653
■Chapter 17: Understanding CIL and the Role of Dynamic Assemblies ................. 687
■Chapter 18: Dynamic Types and the Dynamic Language Runtime ...................... 739
■Chapter 19: Multithreaded and Parallel Programming ........................................ 765
■Chapter 20: File I/O and Object Serialization ....................................................... 817
■Chapter 21: ADO.NET Part I: The Connected Layer .............................................. 869
■Chapter 22: ADO.NET Part II: The Disconnected Layer ......................................... 931
■Chapter 23: ADO.NET Part III: The Entity Framework .......................................... 999
■Chapter 24: Introducing LINQ to XML ................................................................ 1041
■Chapter 25: Introducing Windows Communication Foundation ........................ 1061
■Chapter 26: Introducing Windows Workflow Foundation 4.0 ............................ 1127
■Chapter 27: Introducing Windows Presentation Foundation and XAML ............ 1165
■Chapter 28: Programming with WPF Controls ................................................... 1227
■Chapter 29: WPF Graphics Rendering Services ................................................. 1295
■Chapter 30: WPF Resources, Animations, and Styles ........................................ 1335
■Chapter 31: WPF Control Templates and UserControls ...................................... 1377
■Chapter 32: Building ASP.NET Web Pages ......................................................... 1427
■Chapter 33: ASP.NET Web Controls, Master Pages, and Themes ...................... 1477
■Chapter 34: ASP.NET State Management Techniques ....................................... 1523
■Appendix A: Programming with Windows Forms .............................................. 1561
■Appendix B: Platform-Independent .NET Development with Mono .................... 1613
Index ..................................................................................................................... 1635
www.it-ebooks.info
vi
Contents
About the Authors ..................................................................................................... liv
About the Technical Reviewer ................................................................................... lv
Acknowledgments .................................................................................................... lvi
Introduction ............................................................................................................. lvii
■Chapter 1: Introducing VB 2010 .............................................................................. 1
Understanding the Previous State of Affairs ...................................................................... 1
Life As a C/Windows API Programmer ...................................................................................................... 1
Life As a C++/MFC Programmer .............................................................................................................. 2
Life As a Visual Basic 6.0 Programmer ..................................................................................................... 2
Life As a Java Programmer ...................................................................................................................... 3
Life As a COM Programmer ...................................................................................................................... 3
The .NET Solution .............................................................................................................. 4
Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS) ................. 5
The Role of the Base Class Libraries ........................................................................................................ 6
What Visual Basic 2010 Brings to the Table ............................................................................................. 6
Additional .NET-Aware Programming Languages .............................................................. 8
Life in a Multi-Language World .............................................................................................................. 10
An Overview of .NET Assemblies ..................................................................................... 10
Single-File and Multi-File Assemblies .................................................................................................... 12
The Role of the Common Intermediate Language .................................................................................. 12
The Role of .NET Type Metadata ............................................................................................................. 15
The Role of the Assembly Manifest ........................................................................................................ 17
www.it-ebooks.info
■ CONTENTS
vii
Understanding the Common Type System ....................................................................... 18
CTS Class Types ..................................................................................................................................... 18
CTS Interface Types ................................................................................................................................ 19
CTS Structure Types ............................................................................................................................... 19
CTS Enumeration Types .......................................................................................................................... 20
CTS Delegate Types ................................................................................................................................ 20
CTS Type Members ................................................................................................................................. 21
Intrinsic CTS Data Types ......................................................................................................................... 21
Understanding the Common Language Specification ...................................................... 22
Ensuring CLS Compliance ....................................................................................................................... 24
Understanding the Common Language Runtime ............................................................. 24
The Assembly/Namespace/Type Distinction ................................................................... 26
The Role of the Microsoft Root Namespace ........................................................................................... 29
Accessing a Namespace Programmatically ........................................................................................... 29
Referencing External Assemblies ........................................................................................................... 31
Exploring an Assembly Using ildasm.exe ........................................................................ 32
Viewing CIL Code .................................................................................................................................... 33
Viewing Type Metadata .......................................................................................................................... 34
Viewing Assembly Metadata (aka the Manifest) .................................................................................... 34
Exploring an Assembly Using Reflector ........................................................................... 35
Deploying the .NET Runtime ............................................................................................ 37
The .NET Client Profile Runtime .............................................................................................................. 37
The Platform-Independent Nature of .NET ....................................................................... 37
Summary ......................................................................................................................... 39
■Chapter 2: Building Visual Basic 2010 Applications ............................................. 41
The Role of the .NET Framework 4.0 SDK ....................................................................... 41
The Visual Studio 2010 Command Prompt ............................................................................................. 42
www.it-ebooks.info
■ CONTENTS
viii
Building VB 2010 Applications Using vbc.exe .................................................................. 43
Specifying Input and Output Targets ...................................................................................................... 44
Referencing External Assemblies ........................................................................................................... 45
Referencing Multiple External Assemblies ............................................................................................. 46
Compiling Multiple Source Files ............................................................................................................. 46
Working with VB 2010 Response Files ................................................................................................... 47
Building .NET Applications Using Notepad++ ................................................................. 49
Building .NET Applications Using Visual Basic 2010 Express .......................................... 50
Some Unique Features of Visual Basic 2010 Express ............................................................................ 51
Building .NET Applications Using Visual Studio 2010 ...................................................... 52
Some Unique Features of Visual Studio 2010 ......................................................................................... 53
Targeting the .NET Framework Using the New Project Dialog Box ........................................................ 53
Using the Solution Explorer Utility .......................................................................................................... 54
The Class View Utility ............................................................................................................................. 57
The Object Browser Utility ...................................................................................................................... 58
Integrated Support for Code Refactoring ................................................................................................ 59
Code Expansions Techniques ................................................................................................................. 61
The Visual Class Designer ...................................................................................................................... 62
The Integrated .NET Framework 4.0 SDK Documentation System ......................................................... 68
Summary ......................................................................................................................... 70
■Chapter 3: Core VB 2010 Programming Constructs, Part I .................................... 71
The Role of the Module Type ........................................................................................... 71
Projects with Multiple Modules .............................................................................................................. 73
Modules Are Not Creatable ..................................................................................................................... 74
Renaming Your Initial Module ................................................................................................................. 75
Members of Modules .............................................................................................................................. 75
www.it-ebooks.info
■ CONTENTS
ix
The Anatomy of a Simple VB 2010 Program .................................................................... 76
Variations on the Main() Method ............................................................................................................. 77
Specifying an Application Error Code ..................................................................................................... 78
Processing Command-Line Arguments .................................................................................................. 80
Specifying Command-Line Arguments with Visual Studio 2010 ............................................................ 81
An Interesting Aside: Some Additional Members of the System.Environment Class ....... 82
The System.Console Class ............................................................................................... 84
Basic Input and Output with the Console Class ...................................................................................... 85
Formatting Console Output ..................................................................................................................... 86
Formatting Numerical Data .................................................................................................................... 86
Formatting Numerical Data Beyond Console Applications ..................................................................... 88
System Data Types and VB 2010 Shorthand Notation ..................................................... 89
Variable Declaration and Initialization .................................................................................................... 91
Intrinsic Data Types and the New Operator ............................................................................................ 92
The Data Type Class Hierarchy ............................................................................................................... 93
Members of Numerical Data Types ........................................................................................................ 95
Members of System.Boolean .................................................................................................................. 96
Members of System.Char ....................................................................................................................... 96
Parsing Values from String Data ............................................................................................................. 97
System.DateTime and System.TimeSpan ............................................................................................... 98
The .NET 4.0 System.Numerics Namespace .......................................................................................... 98
Working with String Data ............................................................................................... 100
Basic String Manipulation .................................................................................................................... 101
String Concatenation ............................................................................................................................ 102
Special Character Constants ................................................................................................................ 103
End SubStrings and Equality ................................................................................................................ 104
Strings Are Immutable .......................................................................................................................... 105
The System.Text.StringBuilder Type .................................................................................................... 106
www.it-ebooks.info
■ CONTENTS
x
Narrowing and Widening Data Type Conversions .........................................................108
Trapping Narrowing Data Conversions. ............................................................................................... 111
Understanding Option Strict . ............................................................................................................... 112
Setting Project-wide Overflow Checking. ............................................................................................ 113
The Role of System.Convert . ............................................................................................................... 114
Understanding Implicitly Typed Local Variables . ..........................................................115
Restrictions on Implicitly Typed Variables. .......................................................................................... 116
Implicit Typed Data Is Strongly Typed Data. ........................................................................................ 117
Usefulness of Implicitly Typed Local Variables. ................................................................................... 118
VB 2010 Iteration Constructs ........................................................................................118
The For Loop. ....................................................................................................................................... 119
The For Each Loop. .............................................................................................................................. 119
The While and Do/While Looping Constructs. ...................................................................................... 120
Decision Constructs and the Relational/Equality Operators . ........................................121
The If/Then/Else Statement. ................................................................................................................ 121
The Select/Case Statement . ................................................................................................................ 123
Summary .......................................................................................................................124
■Chapter 4: Core VB 2010 Programming Constructs, Part II. ...............................125
Methods and Parameter Modifiers . ..............................................................................125
The ByVal Parameter Modifier. ............................................................................................................ 126
The ByRef Modifier. ............................................................................................................................. 128
The <Out()> Attribute . ......................................................................................................................... 129
The ParamArray Modifier. .................................................................................................................... 130
Defining Optional Parameters. ............................................................................................................. 132
Invoking Methods using Named Parameters........................................................................................ 133
Understanding Method Overloading . ................................................................................................... 135
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
■ CONTENTS
xi
Understanding VB 2010 Arrays ...................................................................................... 138
VB 2010 Array Initialization Syntax ....................................................................................................... 139
Implicitly Typed Local Arrays ................................................................................................................ 139
Defining an Array of Objects ................................................................................................................. 140
Working with Multidimensional Arrays ................................................................................................. 141
Arrays As Arguments or Return Values ................................................................................................ 143
The System.Array Base Class ............................................................................................................... 144
Understanding the Enum Type ....................................................................................... 146
Controlling the Underlying Storage for an Enum .................................................................................. 147
Declaring Enum Variables .................................................................................................................... 147
The System.Enum Type ........................................................................................................................ 148
Dynamically Discovering an Enum’s Name/Value Pairs ....................................................................... 149
Understanding the Structure Type ................................................................................. 152
Creating Structure Variables ................................................................................................................ 153
Understanding Value Types and Reference Types ......................................................... 155
Value Types, References Types, and the Assignment Operator ........................................................... 156
Value Types Containing Reference Types ............................................................................................. 158
Passing Reference Types by Value ....................................................................................................... 160
Passing Reference Types by Reference ............................................................................................... 162
Final Details Regarding Value Types and Reference Types .................................................................. 163
Understanding VB 2010 Nullable Types ......................................................................... 164
Working with Nullable Types ................................................................................................................ 165
The Nullable If Operator ........................................................................................................................ 166
Summary ....................................................................................................................... 167
■Chapter 5: Defining Encapsulated Class Types .................................................... 169
Introducing the VB 2010 Class Type .............................................................................. 169
Allocating Objects with the New Keyword ............................................................................................ 172
www.it-ebooks.info
■ CONTENTS
xii
Understanding Constructors .......................................................................................... 173
The Role of the Default Constructor ..................................................................................................... 173
Defining Custom Constructors .............................................................................................................. 174
The Default Constructor Revisited ........................................................................................................ 175
The Role of the Me Keyword .......................................................................................... 176
Chaining Constructor Calls Using Me ................................................................................................... 178
Observing Constructor Flow ................................................................................................................. 181
Revisiting Optional Arguments ............................................................................................................. 183
Understanding the Shared Keyword .............................................................................. 184
Defining Shared Methods ..................................................................................................................... 184
Defining Shared Field Data ................................................................................................................... 185
Defining Shared Constructors ............................................................................................................... 188
Defining Modules in VB 2010................................................................................................................ 190
Defining the Pillars of OOP ............................................................................................. 193
The Role of Encapsulation .................................................................................................................... 193
The Role of Inheritance ......................................................................................................................... 194
The Role of Polymorphism .................................................................................................................... 195
VB 2010 Access Modifiers ............................................................................................. 197
The Default Access Modifiers ............................................................................................................... 197
Access Modifiers and Nested Types ..................................................................................................... 198
The First Pillar: VB 2010 Encapsulation Services .......................................................... 198
Encapsulation Using Traditional Accessors and Mutators .................................................................... 199
Encapsulation Using .NET Properties ................................................................................................... 202
Using Properties within a Class Definition ............................................................................................ 206
Internal Representation of Properties ................................................................................................... 207
Controlling Visibility Levels of Property Get/Set Statements ................................................................ 210
Read-Only and Write-Only Properties ................................................................................................... 210
Shared Properties ................................................................................................................................. 211
www.it-ebooks.info
■ CONTENTS
xiii
Understanding Automatic Properties ............................................................................. 213
Interacting with Automatic Properties .................................................................................................. 214
Regarding Automatic Properties and Default Values ............................................................................ 215
Understanding Object Initializer Syntax ......................................................................... 217
Calling Custom Constructors with Initialization Syntax ........................................................................ 218
Nested Objects ..................................................................................................................................... 220
Working with Constant Field Data ................................................................................. 221
Understanding Read-Only Fields .......................................................................................................... 222
Shared Read-Only Fields ...................................................................................................................... 223
Understanding Partial Types .......................................................................................... 224
Summary ....................................................................................................................... 225
■Chapter 6: Understanding Inheritance and Polymorphism .................................. 227
The Basic Mechanics of Inheritance .............................................................................. 227
Specifying the Parent Class of an Existing Class .................................................................................. 228
Regarding Multiple Base Classes ......................................................................................................... 230
The NotInheritable Keyword ................................................................................................................. 230
Revising Visual Studio Class Diagrams ......................................................................... 232
The Second Pillar of OOP: The Details of Inheritance .................................................... 235
Controlling Base Class Creation with the MyBase Keyword ................................................................. 237
Keeping Family Secrets: The Protected Keyword ................................................................................. 239
Adding a NotInheritable Class .............................................................................................................. 240
Programming for Containment/Delegation .................................................................... 241
Understanding Nested Type Definitions ............................................................................................... 243
The Third Pillar of OOP: VB’s Polymorphic Support ....................................................... 245
The Overridable and Overrides Keywords ............................................................................................ 246
Overriding Overridable Members Using Visual Studio 2010 ................................................................. 249
NotOverridable Members ...................................................................................................................... 250
Understanding Abstract Classes and the MustInherit Keyword ........................................................... 250
www.it-ebooks.info