HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Foreword by Beth Massi
Senior Program Manager, Microsoft Visual Studio
Visual Basic 2010 Unleashed is the most comprehensive, practical reference to modern object-oriented programming with Visual Basic 2010. Written by Visual Basic MVP Alessandro Del Sole, a long-time leader of the global VB community, this book illuminates the core of the VB language and demonstrates its effective use in a wide variety of programming scenarios.
Del Sole covers both Visual Basic 2010 Professional Edition for professional developers and the Express Edition for hobbyists, novices, and students. Writing for VB programmers at all levels of experience, he walks through using VB 2010 for data access, user interface development, networking, communication, and many other tasks. For those moving from structured languages–including VB 6–he offers detailed guidance on building effective object-oriented code. He also demonstrates how to make the most of Microsoft’s underlying .NET platform to write more robust and powerful software.
This book’s broad coverage includes advanced features such as generics and collections; a thorough introduction to the Visual Studio 2010 IDE and Visual Studio Team System; a full section on data access with ADO.NET and LINQ; practical overviews of WPF and WCF; coverage of web and cloud development with Silverlight and Azure; and advanced topics such as multithreading, testing, and deployment.
Alessandro Del Sole, Microsoft MVP for Visual Basic, is well-known throughout the global VB community. He is community leader on the Italian VB Tips and Tricks website that serves more than 41,000 developers and a frequent contributor and blogger at MSDN’s Visual Basic Developer Center. He is the author of three books about .NET development with Visual Basic.
ON THE WEB:
Download all examples and source code presented in this book from informit.com/title/9780672331008
Category: Microsoft Programming
Covers: Visual Basic 2010
User Level: Intermediate
Download the example files.
Download the Chapter 56: Advanced IDE Features.
Download the Chapter 57: Introducing the Visual Studio Extensibility.
Download the Chapter 58: Advanced Analysis Tools.
Download the Chapter 59: Testing Code with Unit Tests, Test-Driven Development, and Code Contracts.
Using Silverlight 4 APIs to Interact with Webcams and Microphones from VB 2010
The Anatomy of a Visual Basic Project
Download the sample pages (includes Chapter 3 and Index)
Part I Learning the Basics of VB
1 Introducing the .NET Framework 4.0 1
What Is the .NET Framework?........................................................................1
Where Is the .NET Framework ..............................................................2
The .NET Framework Architecture ........................................................2
The Common Language Runtime ..................................................................4
Writing Managed Code .........................................................................4
.NET Assemblies.....................................................................................5
The Base Class Library ....................................................................................5
.NET Languages...............................................................................................6
.NET Framework Tools ....................................................................................7
Windows Software Development Kit ....................................................7
What’s New in .NET Framework 4.0 ..............................................................8
2 Getting Started with the Visual Studio 2010 IDE 11
What’s New in Visual Studio 2010 ...............................................................11
Start Page.......................................................................................................12
Get Started Tab ....................................................................................13
The Guidance and Resources Tab........................................................13
The Latest News Tab ............................................................................14
Working with Projects and Solutions...........................................................15
Creating Visual Basic Projects .............................................................16
Multitargeting......................................................................................17
Accessing Recent and Online Templates.............................................18
Searching for Installed Templates .......................................................20
Creating Reusable Projects and Items Templates................................21
Creating Your First Visual Basic 2010 Project .....................................21
Finding Visual Basic Projects...............................................................23
Working with the Code Editor............................................................24
Working with Tool Windows........................................................................26
The Solution Explorer Window...........................................................27
Error List Window ...............................................................................28
The Properties Window .......................................................................30
Output Window...................................................................................31
My Project .....................................................................................................31
Application Tab....................................................................................33
Compiling Projects .......................................................................................37
Debug and Release Configurations .....................................................38
Other Compile Options.......................................................................41
Advanced Compile Options ................................................................44
Debugging Overview.....................................................................................47
Debugging an Application...................................................................47
Breakpoints and Data Tips ..................................................................49
About Runtime Errors..........................................................................52
Edit and Continue ...............................................................................53
Browsing the Visual Basic and .NET Documentation..................................54
Online Help and the MSDN Library ...................................................55
Object Browser Window......................................................................55
3 The Anatomy of a Visual Basic Project 59
Brief Overview of Types and Members.........................................................59
Classes ..................................................................................................60
Properties .............................................................................................60
Methods ...............................................................................................60
Modules ...............................................................................................61
Structures .............................................................................................61
Inheritance...........................................................................................61
Namespaces..........................................................................................62
Accessing Members..............................................................................63
Imports Directives................................................................................64
#Region..#End Region Directives........................................................64
Attributes .............................................................................................65
A New Feature: Implicit Line Continuation .......................................65
Visual Basic 2010 Reserved Keywords ..........................................................67
Understanding Project Files..........................................................................70
Dissecting My Project ..........................................................................70
Application.MyApp .............................................................................70
AssemblyInfo.vb ..................................................................................72
Resources and the Resources.resx File .................................................73
Application Settings.............................................................................77
Understanding References ............................................................................79
Adding References to COM Libraries ..................................................82
Deploy Without PIAs...........................................................................83
Final Considerations............................................................................84
4 Data Types and Expressions 85
Common Type System..................................................................................85
Everything Is an Object.......................................................................86
Introducing Value Types and Reference Types ...................................86
System.Object and System.ValueType...............................................87
Understanding Value Types..........................................................................89
.NET Framework Primitive Value Types ..............................................90
Using Value Types................................................................................92
Working with BigInteger ...................................................................100
Building Custom Value Types ...........................................................101
Understanding Reference Types .................................................................101
.NET Framework Primitive Reference Types .....................................103
Differences Between Value Types and Reference Types .............................104
Memory Allocation............................................................................104
Object-Oriented Differences..............................................................107
Performance Differences....................................................................109
What Custom Type Should I Choose?..............................................109
Converting Between Value Types and Reference Types.............................110
Understanding Implicit Conversions................................................110
Boxing and Unboxing .......................................................................112
Deep Copy and Shallow Copy ..........................................................114
The GetType Keyword........................................................................118
Conversion Operators.................................................................................119
Widening and Narrowing Conversions ............................................119
Working with .NET Fundamental Types ....................................................124
Working with Strings.........................................................................125
Working with Dates...........................................................................137
Working with Time............................................................................143
Working with TimeZone and TimeZoneInfo ......................................144
Working with GUIDs.........................................................................147
Working with Arrays..........................................................................148
Common Operators ....................................................................................155
Arithmetic Operators.........................................................................155
Assignment Operators .......................................................................157
Logical, Bitwise and Shift Operators .................................................158
Concatenation Operators ..................................................................163
Comparison Operators ......................................................................164
Iterations, Loops, and Conditional Code Blocks .......................................166
Iterations............................................................................................166
Loops..................................................................................................169
Conditional Code Blocks...................................................................171
Constants...........................................................................................174
With..End With statement................................................................175
5 Debugging Visual Basic 2010 Applications 177
Preparing an Example.................................................................................177
Debugging Instrumentation .......................................................................178
Debugging in Steps............................................................................178
Mixed Mode Debugging ....................................................................180
“Just My Code” Debugging ...............................................................180
Working with Breakpoints and Trace Points.....................................182
Locals Window ..................................................................................185
Command Window...........................................................................185
Call Stack Window ............................................................................186
Watch Windows.................................................................................187
Threads Window................................................................................189
Autos Window ...................................................................................190
Debugger Visualizers ...................................................................................191
Debugging in Code .....................................................................................192
The Debug Class .................................................................................192
The Trace Class .................................................................................194
Understanding Trace Listeners ..........................................................195
Using Debug Attributes in Your Code...............................................201
6 Handling Errors and Exceptions 207
Introducing Exceptions...............................................................................207
Handling Exceptions...................................................................................208
Are You Upgrading from Visual Basic 6? ..........................................209
System.Exception, Naming Conventions and Specialization .........209
Try..Catch..Finally........................................................................209
The Throw Keyword ...........................................................................218
The When Keyword .............................................................................222
Catching Exceptions Without a Variable..........................................224
Part II Object-Oriented Programming with Visual Basic 2010
7 Class Fundamentals 225
Declaring Classes.........................................................................................225
Nested Classes....................................................................................226
Fields ...........................................................................................................227
Avoiding Ambiguities with Local Variables ......................................228
Properties.....................................................................................................229
Read-Only Properties .........................................................................231
Write-Only Properties ........................................................................231
Exposing Custom Types ....................................................................232
Accessing Properties...........................................................................232
Default Properties ..............................................................................233
Scope ...........................................................................................................234
Methods ......................................................................................................236
Invoking Methods .............................................................................236
Methods Arguments: ByVal and ByRef .............................................237
Overloading Methods........................................................................242
Exit from Methods.............................................................................245
Partial Classes..............................................................................................246
Partial Methods ...........................................................................................249
Constructors................................................................................................251
Overloading Constructors .................................................................254
Object Initializers...............................................................................256
Shared Members..........................................................................................257
Shared Classes....................................................................................258
Shared Fields ......................................................................................258
Shared Properties ...............................................................................259
Shared Methods .................................................................................259
Shared Constructors ..........................................................................261
Common Language Specification...............................................................262
Where Do I Need to Apply? ..............................................................262
Marking Assemblies and Types as CLS-Compliant ...........................263
Naming Conventions ........................................................................263
Rules About Classes ...........................................................................265
Rules About Properties.......................................................................265
Rules About Methods ........................................................................265
Rules About Arrays.............................................................................266
8 Managing an Object’s Lifetime 267
Understanding Memory Allocation............................................................267
Understanding Garbage Collection............................................................268
Understanding the Finalize Method........................................................269
Understanding Dispose and the IDisposable Interface............................271
Using..End Using Statement.............................................................273
Putting Dispose and Finalize Together...........................................273
Object Resurrection.....................................................................................276
Advanced Garbage Collection ....................................................................277
Interacting with the Garbage Collector ............................................277
Understanding Generations and Operation Modes................................278
9 Organizing Types Within Namespaces 281
Understanding What Namespaces Are.......................................................281
Organizing Types Within Namespaces.......................................................282
Why Are Namespaces So Useful? ......................................................285
Nested Namespaces............................................................................286
Scope ..................................................................................................289
Root Namespace ................................................................................289
Global Keyword.................................................................................290
Imports Directives .............................................................................291
Namespaces and Common Language Specification .........................294
10 Modules 295
Modules Overview ......................................................................................295
Scope ..................................................................................................296
Differences Between Modules and Classes .................................................297
No Constructor..................................................................................297
No Inheritance Support.....................................................................297
No Interface Implementation............................................................297
11 Structures and Enumerations 299
Assignments ................................................................................................302
Passing Structures to Methods....................................................................302
Members’ Visibility .....................................................................................303
Inheritance Limitations and Interfaces Implementation ..........................303
Memory Allocation .....................................................................................303
Organizing Structures .................................................................................304
Overloading Operators................................................................................305
Overloading CType.............................................................................307
Structures and Common Language Specification ......................................308
Enumerations..............................................................................................309
Using Enumerations ..........................................................................310
Useful Methods from System.Enum ...................................................311
Using Enums As Return Values From Methods ................................314
Enum Values As Bit Flags...................................................................315
Enumerations and Common Language Specification ......................315
12 Inheritance 317
Applying Inheritance ..................................................................................318
Illustrating System.Object in Detail ..........................................................321
Introducing Polymorphism ........................................................................323
Overriding Members ...................................................................................325
NotOverridable Keyword..................................................................328
Overloading Derived Members .........................................................328
Conditioning Inheritance...........................................................................328
NotInheritable Keyword..................................................................329
MustInherit and MustOverride Keywords .......................................330
Accessing Base Classes Members ................................................................331
MyBase Keyword.................................................................................332
MyClass Keyword...............................................................................334
Constructors’ Inheritance ...........................................................................335
Shadowing...................................................................................................336
Overriding Shared Members.......................................................................337
Practical Inheritance: Building Custom Exceptions...................................338
13 Interfaces 341
Defining Interfaces......................................................................................341
Implementing and Accessing Interfaces.....................................................343
Passing Interfaces As Method Arguments .........................................345
Interfaces and Polymorphism.....................................................................345
Interfaces Inheritance .................................................................................346
Defining CLS-Compliant Interfaces ...........................................................347
Most Common .NET Interfaces ..................................................................348
The IEnumerable Interface ................................................................348
The IComparable Interface ................................................................351
The IConvertible Interface ..............................................................353
The IFormattable Interface ..............................................................356
14 Generics and Nullable Types 359
Introducing Generics ..................................................................................359
Creating and Consuming Generics ............................................................360
Consuming Generic Types ................................................................362
Implementing Generic Methods .......................................................363
Understanding Constraints ...............................................................363
Overloading Type Parameters............................................................366
Introducing Nullable Types ........................................................................367
15 Delegates and Events 371
Understanding Delegates............................................................................371
Declaring Delegates ...........................................................................372
Combining Delegates: Multicast Delegates.......................................374
Handling Events..........................................................................................375
Registering for events: AddHandler and RemoveHandler...................375
Declaring Objects with the WithEvents Keyword.............................377
Offering Events to the External World.......................................................378
Raising Events....................................................................................378
Creating Custom Events....................................................................381
16 Working with Collections 385
Understanding Collections Architecture....................................................385
Working with Nongeneric Collections.......................................................386
The ArrayList Collection .................................................................386
The Queue Collection.........................................................................389
The Stack Collection.........................................................................390
The HashTable Collection .................................................................390
The ListDictionary Collection ........................................................391
The OrderedDictionary Collection ..................................................391
The SortedList Collection ...............................................................392
The HybridDictionary Collection ....................................................392
The StringCollection Collection ....................................................392
The StringDictionary Collection ....................................................393
The NameValueCollection Collection...............................................393
The BitArray Collection ...................................................................393
The Bitvector32 Collection..............................................................394
Working with Generic Collections .............................................................395
The List(Of T) Collection...................................................................395
Working with Collection Initializers.................................................397
The ReadOnlyCollection(Of T) Collection .......................................398
The Dictionary(Of TKey, TValue) Collection ....................................399
The SortedDictionary(Of TKey, TValue) Collection..........................400
The ObservableCollection(Of T) Collection .....................................400
The ReadonlyObservableCollection(Of T) Collection ......................402
The LinkedList(Of T) Collection .......................................................403
The Queue(Of T) and Stack(Of T) Collections..................................405
Building Custom Collections......................................................................405
Concurrent Collections ..............................................................................406
17 Visually Designing Objects 407
Visual Studio Class Designer.......................................................................407
Enabling the Class Designer..............................................................408
Adding and Designing Objects..........................................................409
Implementing Derived Classes..........................................................413
Creating Multiple Diagrams ..............................................................416
Exporting the Diagram......................................................................416
Class View Window ....................................................................................417
Class Details Window .................................................................................418
18 “Generate From Usage” Coding Techniques 421
Coding New Types ......................................................................................421
Generating Shared Members .............................................................425
On-the-Fly Code and Object Initializers ...........................................425
Generating Complex Objects .....................................................................426
Interfaces Additions...........................................................................428
Part III Advanced VB Language features
19 Manipulating Files and Streams 429
Manipulating Directories and Pathnames..................................................429
The System.IO.Path Class.................................................................430
The System.IO.Directory Class .......................................................431
The System.IO.DirectoryInfo Class................................................434
The System.IO.DriveInfo Class .......................................................434
Handling Exceptions for Directories and Pathnames ................................435
Manipulating Files ......................................................................................436
The System.IO.File Class.................................................................436
The System.IO.FileInfo Class .........................................................438
Handling File Exceptions...................................................................439
Understanding Permissions...............................................................439
Introducing Streams....................................................................................440
Reading and Writing Text Files .........................................................441
Reading and Writing Binary Files......................................................442
Using Memory Streams .....................................................................442
Using Streams with Strings................................................................443
Compressing Data with Streams .......................................................444
Networking with Streams ..................................................................448
20 The My Namespace 451
Introducing My ............................................................................................451
My.Application...........................................................................................452
Retrieving Assembly Information .....................................................452
Working with Cultures ......................................................................453
Deployment and Environment Information ....................................454
My.Computer ................................................................................................456
Working with the File System ...........................................................457
Working with the Clipboard .............................................................458
Playing Audio Files ............................................................................459
Managing the Keyboard ....................................................................460
Working with the Registry ................................................................460
Accessing the Network ......................................................................461
Getting Computer Information ........................................................462
My.Settings ................................................................................................464
My.Settings Events ...........................................................................469
My.Resources ..............................................................................................470
Getting Resources by Name in Code ................................................474
My.User........................................................................................................474
My.WebServices...........................................................................................476
Extending My..............................................................................................476
Extending My.Application and My.Computer...................................478
Extending My.Resources and My.Settings ......................................480
My in Different Applications .......................................................................480
Understanding Application Events ...................................................483
21 Advanced Language Features 485
Local Type Inference...................................................................................485
Option Infer Directive .....................................................................487
Local Type Inference Scope ...............................................................488
Array Literals ...............................................................................................489
Multidimensional and Jagged Arrays ................................................489
Extension Methods .....................................................................................490
Coding Custom Extension Methods.................................................495
Exporting Extension Methods...........................................................497
Anonymous Types ......................................................................................498
Relaxed Delegates........................................................................................500
Lambda Expressions....................................................................................500
Type Inference and Lambda Expressions..........................................503
Multiline Lambdas.............................................................................504
Sub Lambdas......................................................................................505
Lexical Closures .................................................................................506
Ternary If Operator....................................................................................507
Generic Variance .........................................................................................509
Covariance .........................................................................................509
Contra Variance .................................................................................510
Part IV Data Access with ADO.NET and LINQ
22 Introducing ADO.NET and DataSets 513
Introducing ADO.NET ................................................................................513
Data Providers....................................................................................514
Connection Modes ............................................................................515
Understanding Connections and Data Readers................................515
Introducing DataSets ..................................................................................517
Creating DataSets...............................................................................517
23 Introducing LINQ 523
What Is LINQ? ............................................................................................523
LINQ Examples ...........................................................................................525
Language Support .......................................................................................526
Understanding Providers ............................................................................527
Overview of LINQ Architecture ..................................................................528
24 LINQ to Objects 531
Introducing LINQ to Objects......................................................................531
Querying in Memory Objects.....................................................................532
Understanding Deferred Execution...................................................539
Introducing Standard Query Operators......................................................541
Projection Operators..........................................................................542
Restriction Operators.........................................................................543
Aggregation Operators.......................................................................544
Understanding the Let Keyword.......................................................546
Conversion Operators........................................................................546
Generation Operators ........................................................................548
Ordering Operators............................................................................549
Set Operators......................................................................................550
Grouping Operators...........................................................................551
Union Operators................................................................................553
Equality Operators .............................................................................556
Quantifiers .........................................................................................556
Concatenation Operators ..................................................................557
Elements Operators............................................................................557
Partitioning Operators.......................................................................558
25 LINQ to SQL 561
Introducing LINQ to SQL ...........................................................................561
Prerequisites .......................................................................................562
Understanding LINQ to SQL Classes ................................................562
Behind the Scenes of LINQ to SQL Classes.......................................573
Querying Data with LINQ to SQL ..............................................................574
Insert/Update/Delete Operations with LINQ .............................................579
Inserting Entities................................................................................579
Updating Entities...............................................................................583
Deleting Entities ................................................................................584
Mapping Stored Procedures...............................................................584
Using the Log.....................................................................................587
Advanced LINQ to SQL...............................................................................588
Custom Validations ...........................................................................588
Handling Optimistic Concurrency....................................................590
Using SQL Syntax Against Entities....................................................591
LINQ to SQL with SQL Server Compact Edition........................................592
Writing the Connection String .........................................................593
26 LINQ to DataSets 595
Querying Datasets with LINQ ....................................................................595
Building Complex Queries with Anonymous Types ........................598
LINQ to DataSets’ Extension Methods .......................................................598
Understanding CopyToDataTable ......................................................598
Understanding Field(Of T) and SetField(Of T)...........................600
27 Introducing ADO.NET Entity Framework 603
Introducing Entity Framework ...................................................................603
Understanding Entity Data Models............................................................604
Understanding the ObjectContext class: The Visual Basic Mapping .........612
Entity Designer Tool Windows..........................................................617
Insert/Update/Delete Operations for Entities.............................................620
Instantiating the ObjectContext .......................................................620
Adding Entities ..................................................................................623
Deleting Entities ................................................................................624
Updating Entities...............................................................................625
Handling Optimistic Concurrency....................................................626
Validating Data ..................................................................................627
Querying EDMs with LINQ to Entities.......................................................628
Querying EDMs with Entity SQL................................................................629
Mapping Stored Procedures ........................................................................631
28 Manipulating Xml Documents with LINQ and Xml Literals 635
Introducing LINQ to Xml...........................................................................635
The System.Xml.Linq Namespace.....................................................636
Xml Literals.................................................................................................641
LINQ Queries with Xml Literals........................................................644
Understanding Embedded Expressions.............................................646
Xml Schema Inference................................................................................649
29 Overview of Parallel LINQ 655
Introducing PLINQ .....................................................................................655
Simulating an Intensive Work...........................................................656
Measuring Performances of a Classic LINQ Query...........................656
Measuring Performances of a PLINQ Query .....................................657
Ordering Sequences ...........................................................................659
AsParallel and Binary Operators.....................................................660
Using ParallelEnumerable ...............................................................660
Controlling PLINQ Queries ...............................................................661
Handling Exceptions...................................................................................663
Part V Building Windows Applications
30 Creating Windows Forms 4.0 Applications 665
What Windows Forms Is Today..................................................................665
Creating Windows Forms Applications......................................................666
What’s New in Windows Forms 4.0..................................................667
Available Windows Forms Controls..................................................667
Building Windows Forms Applications with ADO.NET Entity Framework and Chart Control.....668
Providing Custom Validation............................................................674
Understanding Chart Control...........................................................675
Populating the Chart Control ...........................................................677
Running the Sample Application......................................................680
31 Creating WPF Applications 681
What Is WPF?..............................................................................................682
Improvements in WPF 4 ...................................................................682
WPF Architecture ........................................................................................683
Building WPF Applications with Visual Studio 2010.................................685
Understanding the eXtensible Application Markup Language (XAML)....687
Declaring and Using Controls with the Designer and XAML..........689
Understanding Visual Tree and Logical Tree..............................................692
Handling Events in WPF.............................................................................694
A More Thorough Discussion: Introducing the Routed Events .......695
Arranging Controls with Panels .................................................................697
The Grid Panel...................................................................................697
The StackPanel Panel .......................................................................700
The WrapPanel Panel .........................................................................701
The Canvas Panel ...............................................................................703
The DockPanel Panel .........................................................................703
The ViewBox Panel .............................................................................704
Managing Windows....................................................................................704
Instantiating Windows at Runtime...................................................706
Introducing the Application Object .........................................................707
Brief Overview of WPF Browser Applications ............................................710
32 WPF Common Controls 715
Introducing WPF Controls Features ...........................................................715
Understanding the ContentControl...........................................................716
Understanding Common Controls ............................................................717
Border ................................................................................................717
Button ................................................................................................718
Calendar ............................................................................................718
CheckBox ............................................................................................719
ComboBox ............................................................................................720
DataGrid ............................................................................................721
DatePicker.........................................................................................721
DocumentViewer .................................................................................723
Ellipse ..............................................................................................723
Expander ............................................................................................723
Frame ..................................................................................................724
GroupBox ............................................................................................725
Image ..................................................................................................726
Label ..................................................................................................726
ListBox ..............................................................................................727
ListView ............................................................................................728
MediaElement .....................................................................................729
Menu....................................................................................................729
PasswordBox.......................................................................................732
ProgressBar.......................................................................................732
RadioButton.......................................................................................734
Rectangle...........................................................................................734
RichTextBox.......................................................................................734
ScrollBar...........................................................................................735
ScrollViewer .....................................................................................735
Separator...........................................................................................736
Slider ................................................................................................736
StatusBar...........................................................................................737
TabControl..........................................................................................737
TextBlock...........................................................................................738
TextBox ..............................................................................................739
ToolBar ..............................................................................................740
TreeView ............................................................................................741
WebBrowser.........................................................................................742
WindowsFormsHost..............................................................................742
Using Common Dialogs .............................................................................743
xx Visual Basic 2010 Unleashed
33 Brushes, Styles, Templates, and Animations in WPF 745
Introducing Brushes....................................................................................745
Applying a SolidColorBrush.............................................................747
Applying a LinearGradientBrush .....................................................748
Applying a RadialGradientBrush .....................................................749
Applying an ImageBrush....................................................................751
Applying SelectionBrush and CaretBrush ......................................753
Applying a VisualBrush ....................................................................754
Applying a DrawingBrush ..................................................................755
Applying a BitmapCacheBrush...........................................................757
Introducing Styles .......................................................................................759
Styles Inheritance ..............................................................................761
Understanding Triggers .....................................................................761
Introducing Control Templates ..................................................................763
Introducing Transformations......................................................................766
Applying RotateTransform ...............................................................767
Applying ScaleTransform .................................................................767
Applying SkewTransform ...................................................................768
Applying TranslateTransform..........................................................768
Applying Multiple Transforms ..........................................................769
Introducing Animations .............................................................................770
Applying DoubleAnimation ...............................................................771
Applying ColorAnimation .................................................................773
Working with Animation Events ......................................................775
Creating Animations with Visual Basic.............................................776
34 Manipulating Documents and Media 781
Viewing Images ...........................................................................................781
Playing Media .............................................................................................783
Manipulating Documents...........................................................................786
Understanding the RichTextBox Control .........................................795
Viewing XPS Documents ............................................................................796
35 Introducing Data-Binding 799
Introducing the Data-Binding ....................................................................799
Binding UI Elements with the Binding Markup Extension..............800
Understanding the DataGrid and the ObservableCollection.........802
Discussing the New Drag’n’Drop Data-Binding.........................................806
Creating Tabular Data Forms.............................................................807
Creating Master-Details Forms ..........................................................813
Understanding Views and Binding Lists ...........................................818
Implementing String Formatters and Value Converters...................822
36 Localizing Applications 829
Introducing .NET Localization ...................................................................829
Windows Forms Localization .....................................................................830
WPF Localization ........................................................................................832
Preparing the LocBaml tool...............................................................832
Localizing a WPF Application ...........................................................833
Part VI Building Web Applications
37 Building ASP.NET Web Applications 839
Introducing the ASP.NET Model.................................................................839
Understanding Page Requests ...........................................................840
Scalability and Performances.............................................................840
Available Project Templates...............................................................841
Web Forms and Master Pages .....................................................................842
Web Forms .........................................................................................842
ASP.NET Controls........................................................................................845
Server Controls ..................................................................................845
HTML Controls..................................................................................847
Handling Events..........................................................................................848
Understanding State Management.............................................................848
The Application State.......................................................................849
The Cache State..................................................................................849
The Context State ..............................................................................850
Using Cookies for Saving Information..............................................850
The Session State ..............................................................................851
The ViewState State ..........................................................................851
Creating a Web Application with VB 2010 with Navigation and Data-Binding .....851
Master Pages.......................................................................................852
Adding the Data Model .....................................................................855
Adding a New Web Form ..................................................................855
Adding Data Controls........................................................................857
Adding Filtering Capabilities.............................................................860
Adding Navigation Controls .............................................................861
Running the Application...................................................................861
Configuring a Web Application for Security..............................................863
38 Publishing ASP.NET Web Applications 865
Deployment Overview................................................................................865
The 1-Click Deployment ...................................................................866
Classic Publishing .......................................................................................866
MSDeploy Publish.......................................................................................868
Understanding Packages....................................................................868
Deploy with MSDeploy .....................................................................868
39 Building Rich Internet Applications with Silverlight 871
Introducing Silverlight................................................................................871
Creating Silverlight Projects with Visual Basic 2010 .................................872
Adding Controls and Handling Events ......................................................875
How Silverlight Applications Are Packaged ......................................876
Playing Media .............................................................................................876
Animating UI Elements ..............................................................................880
Introducing Navigation Applications.........................................................882
Introducing WCF RIA Services ...................................................................886
Adding the Data Source.....................................................................887
Adding the Domain Service Class .....................................................887
Data-Binding to Controls ..................................................................891
Running the Application...................................................................893
“Out of Browser” Applications ...................................................................894
40 Building and Deploying Applications for Windows Azure 899
About Windows Azure Platform.................................................................899
Registering for the Windows Azure Developer Portal................................901
Downloading and Installing Tools for Visual Studio .................................901
Additional Tools.................................................................................902
Creating a Demo Project.............................................................................902
Understanding Web Roles and Web Configuration .........................902
Adding a Silverlight 3 Project............................................................905
Testing the Application Locally.........................................................911
Deploying Applications to Windows Azure ...............................................913
Activating the Storage Account ..................................................................921
Using the Windows Azure Management Console Snap-In ..............922
Part VII Networking and Exposing Data Through Networks
41 Creating and Consuming WCF Services 927
Introducing Windows Communication Foundation.................................928
Understanding Endpoints .................................................................928
Address, Binding, Contract: The ABC of WCF .................................928
Implementing WCF Services ......................................................................929
Implementing Custom Logic for the WCF Service...........................933
Consuming WCF Services...........................................................................937
Creating the Client and Adding a Service Reference........................938
Understanding the Proxy Class.........................................................938
Invoking Members from the Service.................................................939
Handling Exceptions in WCF .....................................................................943
Hosting WCF Services in Internet Information Services ...........................944
Configuring Services with the Configuration Editor .................................945
42 Implementing and Consuming WCF Data Services 949
What Are Data Services? .............................................................................949
Querying Data via Http Requests......................................................950
Implementing WCF Data Services..............................................................951
Deploying WCF Data Services to Internet Information Services .....957
Consuming WCF Data Services ..................................................................957
Creating a Client Application ...........................................................957
Querying Data ...................................................................................962
Implementing Service Operations ..............................................................963
Implementing Query Interceptors..............................................................965
Understanding Query Interceptors ...................................................965
Understanding Change Interceptors.................................................967
Understanding Server-Driven Paging .........................................................968
Part VIII Advanced .NET Framework with VB 2010
43 Serialization 971
Objects Serialization ...................................................................................972
Binary Serialization............................................................................972
Soap Serialization...............................................................................975
Providing Serialization for Custom Objects......................................976
NonSerialized events .........................................................................978
XML Serialization........................................................................................979
Customizing Xml Serialization .........................................................980
Custom Serialization...................................................................................982
Serialization Events............................................................................983
Serialization with XAML.............................................................................984
Serialization in Windows Communication Foundation............................987
JSON Serialization..............................................................................989
Serialization in the ADO.NET Entity Framework.......................................990
44 Processes and Multithreading 993
Managing Processes ....................................................................................994
Querying Existing Processes ..............................................................995
Introducing Multithreading .......................................................................996
Creating Threads................................................................................996
Passing Parameters.............................................................................997
Understanding the .NET Thread Pool ........................................................997
Getting and Setting Information in the Thread Pool.......................998
Threads Synchronization............................................................................999
The SyncLock..End SyncLock Statement .......................................1000
Synchronization with the Monitor Class........................................1001
Read/Write Locks .............................................................................1001
45 Parallel Programming 1005
Introducing Parallel Computing...............................................................1006
Introducing Parallel Classes.............................................................1006
Understanding and Using Tasks ...............................................................1007
What Is a Task? ................................................................................1007
Running Tasks with Parallel.Invoke ............................................1008
Creating, Running, and Managing Tasks: The Task Class..............1009
Creating Tasks That Return Values..................................................1009
Exception Handling.........................................................................1011
Cancelling Tasks ..............................................................................1012
The Barrier Class............................................................................1014
Parallel Loops ............................................................................................1015
Parallel.For Loop..........................................................................1017
Parallel.ForEach Loop ..................................................................1018
Debugging Tools For Parallel Tasks...........................................................1021
Concurrent Collections ............................................................................1022
ConcurrentBag(Of T)......................................................................1023
ConcurrentQueue(Of T) ..................................................................1024
ConcurrentStack(Of T) ..................................................................1024
ConcurrentDictionary(Of TKey, TValue) ....................................1025
BlockingCollection(Of T) ............................................................1026
46 Working with Assemblies 1029
Assembly Overview...................................................................................1029
Information Stored Within Assemblies...........................................1030
Assembly Location...........................................................................1030
Signing Assemblies ..........................................................................1031
Assembly Information and Attributes.............................................1031
Understanding Application Domains.......................................................1031
Creating Application Domains and Executing Assemblies ............1031
Overview of Security Changes in .NET 4.0 ..............................................1034
Permissions ......................................................................................1035
The Transparency Level 2................................................................1036
Sandboxing ......................................................................................1038
Conditional APTCA .........................................................................1041
Migrating from Old CAS-Based Code..............................................1041
47 Reflection 1043
Introducing Reflection..............................................................................1043
Understanding Assemblies’ Metadata.......................................................1044
Preparing a Sample Assembly..........................................................1045
Getting Assembly Information.................................................................1046
Reflecting Types ........................................................................................1048
Reflecting a Single Type...................................................................1053
Invoking Code Dynamically.....................................................................1055
Generating Code at Runtime with Reflection.Emit ..............................1057
Late Binding Concepts ....................................................................1062
48 Coding Attributes 1065
Applying Attributes...................................................................................1065
Coding Custom Attributes........................................................................1068
Applying Custom Attributes............................................................1070
Applying Attributes Multiple Times................................................1071
Defining Inheritance .......................................................................1071
Reflecting Attributes .................................................................................1073
49 Platform Invokes and Interoperability with the COM Architecture 1075
Importing and Using COM Objects .........................................................1075
Importing COM Components into Visual Studio ..........................1076
Using COM Objects in Code...........................................................1077
Catching Exceptions........................................................................1078
Releasing COM Objects ...................................................................1078
Exposing .NET Objects to the COM World..............................................1078
P/Invokes and Unmanaged Code.............................................................1081
Understanding P/Invokes ................................................................1081
Encapsulating P/Invokes..................................................................1082
Converting Types to Unmanaged ...................................................1084
The StructLayout Attribute ............................................................1085
The VBFixedString attribute...........................................................1087
Handling Exceptions .......................................................................1087
References to the Win32 API calls............................................................1089
50 Documenting the Source Code 1091
Understanding XML Documents..............................................................1091
Enabling XML Comments...............................................................1092
Implementing XML Comments ...............................................................1093
Defining Complex Code Documentation.......................................1095
Generating Compiled Help Files ..............................................................1102
51 Advanced Compilations with MSBuild 1105
Introducing MSBuild.................................................................................1105
Introducing Projects ........................................................................1107
Understanding Tasks and Creating Targets.....................................1111
Advanced MSBuild Features......................................................................1114
Batching...........................................................................................1114
Logging ............................................................................................1116
Transformations...............................................................................1116
52 Building Customizations for Microsoft Office 1119
Introducing the Visual Studio Tools for Office ........................................1120
Understanding Application-Level Solutions and Document-Level Solutions..1120
What Are Office Business Applications? .........................................1121
Creating an Application-Level Add-In for Microsoft Word .....................1121
Creating a Document-Level Add-In for Microsoft Excel..........................1126
Designing the Add-In ......................................................................1129
Interacting with Documents via Visual Basic 2010 Code ..............1130
Running the Customized Document ..............................................1132
Deploying VSTO Add-Ins..........................................................................1132
Part IX Applications Deployment
53 Understanding the Global Assembly Cache 1137
The Dll Hell Problem ................................................................................1137
XCopy Deployment.........................................................................1138
The Global Assembly Cache .....................................................................1139
Installing and Uninstalling Assemblies...........................................1140
Signing Assemblies with Strong Names ..........................................1141
Top Reasons for Installing (or Not) Assemblies to the GAC...........1143
Adding References from Visual Studio to Your Own Assemblies ...1143
54 Setup & Deployment Projects for Windows Installer 1145
Windows Installer Overview.....................................................................1145
Creating a Setup Project ...........................................................................1146
Configuring the Setup Project ..................................................................1149
Editing the File System ....................................................................1150
Editing Registry Values ....................................................................1152
Customizing Dialogs........................................................................1152
Creating File Types ..........................................................................1153
Providing Custom Actions ..............................................................1154
Specifying Launch Conditions ........................................................1155
Package Configuration and Prerequisites........................................1156
Building and Deploying the Windows Installer Package.........................1158
55 Deploying Applications with ClickOnce 1159
Introducing ClickOnce .............................................................................1159
How ClickOnce Handles Applications ............................................1160
When Should I Use ClickOnce? ......................................................1160
Deploying Applications with ClickOnce..................................................1161
Structure of a ClickOnce Deployment ............................................1164
Configuring ClickOnce.............................................................................1165
Application Files ..............................................................................1166
Prerequisites .....................................................................................1166
Updates ............................................................................................1167
Options ............................................................................................1168
Security Considerations ............................................................................1169
Providing Certificates ......................................................................1170
Programmatically Accessing ClickOnce ...................................................1171
Registration-Free COM..............................................................................1172
Part X Mastering the Visual Studio 2010 IDE
Online Chapters (available at www.informit.com/title/9780672331008)
56 Advanced IDE Features Web: 1175
Exporting Templates........................................................................Web: 1175
Exporting Project Templates ..................................................Web: 1175
Exporting Item Templates ......................................................Web: 1178
Customizing Visual Studio 2010 .....................................................Web: 1181
Customizing the Tools Menu.................................................Web: 1181
Customizing Commands and Toolbars .................................Web: 1182
Managing User Settings ...................................................................Web: 1185
Exporting Settings ..................................................................Web: 1185
Importing Settings..................................................................Web: 1186
Customizing the Toolbox................................................................Web: 1189
Using, Creating, and Managing Reusable Code Snippets...............Web: 1190
Consuming Code Snippets.....................................................Web: 1190
The Code Snippet Manager....................................................Web: 1192
Creating and Consuming Custom Code Snippets ................Web: 1193
57 Introducing the Visual Studio Extensibility Web: 1199
Introducing Visual Studio Extensibility ..........................................Web: 1199
What’s New in the Extensibility with Visual Studio 2010 ....Web: 1200
The Visual Studio 2010 SDK ..................................................Web: 1201
Building a Visual Studio Package ....................................................Web: 1201
Deploying Visual Studio Extensions ...............................................Web: 1212
Managing Extensions with the Extension Manager.......................Web: 1214
Managing Add-Ins with the Add-In Manager.................................Web: 1216
Extending the Code Editor..............................................................Web: 1217
58 Advanced Analysis Tools Web: 1223
Introducing Analysis Tools ..............................................................Web: 1224
Performing Code Analysis ...............................................................Web: 1224
Calculating Code Metrics ................................................................Web: 1230
Profiling Applications......................................................................Web: 1231
Profiling External Executables ...............................................Web: 1241
IntelliTrace, the Historical Debugger...............................................Web: 1242
IntelliTrace Options................................................................Web: 1242
Creating a Sample Application ..............................................Web: 1243
Tracking Application Events and Exceptions with IntelliTrace....Web: 1245
Analyzing IntelliTrace Logs ....................................................Web: 1247
Using IntelliTrace for Unit Tests ............................................Web: 1248
Generating Dependency Graphs.....................................................Web: 1248
59 Testing Code with Unit Tests, Test-Driven Development, and Code Contracts Web: 1251
Testing Code with Unit Tests ..........................................................Web: 1251
Creating Unit Tests.................................................................Web: 1252
Running Unit Tests.................................................................Web: 1256
Enabling Code Coverage........................................................Web: 1257
Unit Tests and IntelliTrace .....................................................Web: 1259
Introducing Test-Driven Development ...........................................Web: 1261
Creating a Test Project............................................................Web: 1261
Creating Unit Tests.................................................................Web: 1263
Refactoring Code ....................................................................Web: 1266
Understanding Code Contracts.......................................................Web: 1267
Setting Up the Environment..................................................Web: 1267
Setting Contracts Properties...................................................Web: 1268
Tools for Code Contracts .......................................................Web: 1269
Preconditions..........................................................................Web: 1269
Post-Conditions......................................................................Web: 1271
Invariants................................................................................Web: 1272
Assertions and Assumptions ..................................................Web: 1273
Contract Events ......................................................................Web: 1273
Appendixes
A Installing Visual Studio 2010 1175
B Useful Resources and Tools for Visual Basic 1181
TOC, 9780672311008, 4/20/10