Professional
Visual Basic® 2010 and .neT 4
inTroducTion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
⊲ ParT i
language consTrucTs and enVironmenT
chaPTer 1
Visual Studio 2010 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
chaPTer 2
Objects and Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
chaPTer 3
Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137
chaPTer 4
The Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
chaPTer 5
Declarative Programming with Visual Basic . . . . . . . . . . . . . . . . . . . . . . 253
chaPTer 6
Exception Handling and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
chaPTer 7
Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
⊲ ParT ii
Business oBJecTs and daTa access
chaPTer 8
Arrays, Collections, and Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
chaPTer 9
Using XML with Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
chaPTer 10
ADO .NET and LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
chaPTer 11
Data Access with the Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . 437
chaPTer 12
Working with SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
chaPTer 13
Services (XML/WCF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
⊲ ParT iii
smarT clienT aPPlicaTions
chaPTer 14
Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
chaPTer 15
Advanced Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
chaPTer 16
User Controls Combining WPF and Windows Forms . . . . . . . . . . . . . . 599
chaPTer 17
WPF Desktop Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .617
chaPTer 18
Expression Blend 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
chaPTer 19
Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
⊲ ParT iV inTerneT aPPlicaTions
chaPTer 20
Silverlight and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
chaPTer 21
Working with ASP .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
chaPTer 22
ASP .NET Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .751
chaPTer 23
ASP .NET MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
chaPTer 24
SharePoint 2010 Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
⊲ ParT V
liBraries and sPecialiZed ToPics
chaPTer 25
Visual Studio Tools for Office . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
chaPTer 26
Windows Workflow Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
chaPTer 27
Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .905
chaPTer 28
COM-Interop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
chaPTer 29
Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
chaPTer 30
Application Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
chaPTer 31
Assemblies and Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
chaPTer 32
Security in the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1021
chaPTer 33
Parallel Programming Using Tasks and Threads . . . . . . . . . . . . . . . . . .1051
chaPTer 34
Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
aPPendix a
The Visual Basic Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1137
aPPendix B
Visual Basic Power Packs Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151
aPPendix c
Workflow 2008 Specifics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1165
aPPendix d
Enterprise Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1185
aPPendix e
Programming for the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205
index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
Professional
Visual Basic® 2010 and .neT 4
Professional
Visual Basic® 2010 and .neT 4
Bill Sheldon
Billy Hollis
Kent Sharkey
Jonathan Marbutt
Rob Windsor
Gastón C. Hillar
Professional Visual Basic® 2010 and .neT 4
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-50224-2
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of
the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through
payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923,
(978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or
online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect
to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without
limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional
materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the
understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional
assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author
shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation
and/or a potential source of further information does not mean that the author or the publisher endorses the information the
organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web
sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United
States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
Library of Congress Control Number: 2010921246
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries,
and may not be used without written permission. Visual Basic is a registered trademark of Microsoft Corporation in the United
States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not
associated with any product or vendor mentioned in this book.
To my wonderful wife, Tracie, who is forced to carry on while
I isolate myself to concentrate on writing. And to the next
generation of children who have joined our extended Sheldon
family (my own and my brothers’) in the past five years — Nick,
Elena, Ben, Billy V, Abigail, and Johnny — each and every
one of you is a valuable part of our lives.
—Bill Sheldon
I owe tremendous thanks to my family, who have somehow
learned to put up with marathon writing sessions, and
to my business partner, Gary Bailey, for keeping
our clients happy while I’m writing.
—Billy Hollis
To Babi, for keeping me alive and putting up with
me — hopefully, for a long time to come.
—Kent Sharkey
To my beautiful wife, Jennifer, who was my greatest
encouragement despite the long days of writing and working.
And to my loving daughter, Kathryn, who always managed to
put a smile on my face after a long day.
—Jonathan Marbutt
To my Dad, who did not complain too much about the
weekends I spent writing instead of playing golf with him.
—Rob Windsor
To my son, Kevin
—Gastón Hillar
aBouT The auThors
Bill sheldon is a software architect and engineer, originally from Baltimore, Maryland.
Holding a degree in computer science from the Illinois Institute of Technology (IIT), Bill has
been actively employed as a software engineer since resigning his commission with the United
States Navy. He is a Microsoft MVP for Visual Basic employed in Carlsbad, California. Bill also
works as an instructor for .NET courses at the University of California San Diego Extension. In
addition to writing books, Bill has published dozens of articles, including the Developer Update
Newsletter, SQL Server Magazine feature articles, and other Penton publications. He is an established online
presenter for MSDN and speaks at live events such as VSLive, DevConnections, Office Developers Conference,
and community events such as user groups and code camp. Bill is an avid cyclist and is active in the fight against
diabetes. Bill can be tracked down through his blog: www.nerdnotes.net/blog or via Twitter: NerdNotes.
Billy hollis is an author and software consultant based in Nashville, Tennessee. Billy was
co-author of the first book ever published on Visual Basic .NET, as well as many other
books on software development. He is a member of the Microsoft Regional Director program
and a Microsoft MVP. In 2002, Billy was selected as one of the original .NET “Software
Legends.” He is heavily involved with consulting, training, and development on the .NET
platform, focusing on architecture, smart-client development, commercial packages, and
user-interface technologies. He regularly speaks on software development at major conferences all over the
world, including Microsoft’s PDC and TechEd events, DevConnections, VSLive, and architecture events such
as the Patterns and Practices Architect Summit.
KenT sharKey is an independent consultant who lives and codes in Comox, British
Columbia. Before going solo, Kent worked at Microsoft as a technical evangelist and content
strategist, promoting the use of .NET technologies. He lives with his wife, Margaret, and three
“children” — Squirrel, Cica, and Toffee.
JonaThan marBuTT is Vice President of Development for WayCool Software, Inc., based
in Birmingham, AL. He has been working professionally in software development since
1996, where he has covered various Microsoft technologies from VB6 to .NET. Over the
recent years, Jonathan has been developing with Silverlight to build Rich Internet Line of
Business applications for the non-profit sector. Through this development, he is beginning
to focus on User Experience (UX) by utilizing Microsoft products like Expression Blend
and technologies like Silverlight. For more information,contact Jonathan at www.jmtechware.com.
roB Windsor is a developer, trainer, writer and Senior Consultant with ObjectSharp
Consulting — a Microsoft Gold Partner based in Toronto, Canada. He has over fifteen years
experience developing rich-client and web applications with Delphi, VB, C# and VB.NET and
is currently spending a majority of his time working with SharePoint. Rob is a member of both
the INETA Speakers Bureau and the MSDN Canada Speakers Bureau and is a regular speaker
at conferences, code camps, and user groups across North America and Europe. He is President
of the Toronto Visual Basic User Group and has been recognized as a Microsoft Most Valuable
Professional for his involvement in the developer community.
gasTón c. hillar has been working with computers since he was eight. He began
programming with the legendary Texas TI-99/4A and Commodore 64 home computers
in the early 80s. He has worked as developer, architect, and project manager for many
companies in Buenos Aires, Argentina. Now, he is an independent IT consultant working
for several Spanish, German, and Latin American companies, and a freelance author. He is
always looking for new adventures around the world.
Gastón is the author of more than forty books in Spanish and has written two books in English. He
contributes to Dr. Dobb’s Go Parallel programming portal at www.ddj.com/go-parallel/, Dr. Dobb’s at
http://drdobbs.com, and is a guest blogger at Intel Software Network at http://software.intel.com.
He lives with his wife, Vanesa, and his son, Kevin. When not tinkering with computers, he enjoys
developing and playing with wireless virtual reality devices and electronics toys with his father, his son, and
his nephew Nico.
You can reach him at:
[email protected]
You can follow him on Twitter at: http://twitter.com/gastonhillar
Gastón’s blog is at: http://csharpmulticore.blogspot.com
aBouT The Technical ediTors
dianne sieBold is a software developer and writer specializing in VB, C#, .NET Framework, WCF, ADO,
and SQL Server. She has worked for a number of Microsoft partners writing enterprise applications with an
emphasis on services and data access. Currently, she works for Microsoft writing developer documentation in
the Dynamics product group. Reach her by e-mail at
[email protected].
doug Parsons is a .NET Software Architect and professional Technical Editor who specializes in C#,
SQL Server, and numerous architectural paradigms. Over the course of his career, he has worked on a
myriad of projects; most notably, however, was the United States 2008 Presidential Campaign website of
John McCain. He is currently employed by NJI New Media, writing software for clients of a predominantly
political nature. He can be reached by email at
[email protected].
doug WaTerfield is a software architect and engineer who lives with his family in Avon, Indiana.
Since earning a degree in computer science from Rose-Hulman Institute of Technology, Doug has been
designing and developing commercial and enterprise applications in a wide variety of technologies. Doug
led development teams and departments for several firms before becoming an independent consultant with a
focus on .NET technologies. He is a retired officer in the US Army Reserve and serves as a volunteer leader
in Cub Scouts and Boy Scouts.
crediTs
acquisiTions ediTor
Paul Reese
Vice PresidenT and execuTiVe grouP
PuBlisher
Richard Swadley
senior ProJecT ediTor
Adaobi Obi Tulton
Vice PresidenT and execuTiVe PuBlisher
Barry Pruett
Technical ediTors
Dianne Siebold
Doug Parsons
Doug Waterfield
associaTe PuBlisher
Jim Minatel
ProJecT coordinaTor, coVer
senior ProducTion ediTor
Lynsey Stanford
Debra Banninger
Proofreaders
Luann Rouff
Nancy Carrasco
Beth Prouty, Word One
ediTorial direcTor
indexer
Robyn B . Siesky
Robert Swanson
ediTorial manager
coVer designer
Mary Beth Wakefield
Michael E . Trent
associaTe direcTor of marKeTing
coVer image
David Mayhew
© Dan Barnes/istockphoto
coPy ediTor
ProducTion manager
Tim Tate
acKnoWledgmenTs
as WiTh any maJor ProJecT PuTTing a BooK of this size and scope together is a team effort and
we as authors were really lucky to have such a great support team helping to keep us on track and focused.
I’d like to publicly call out and thank our editor, Adaobi, who managed to turn around several of the late
chapters (those would be mine) in an incredibly short time, and our technical editors, in particular Dianne,
who did an outstanding job. They found hundreds of issues so that our readers wouldn’t need to, and
helped ensure that everything worked and flowed in a logical fashion.
—Bill Sheldon
While WriTing can be a solitary activity, writing for a book definitely is not, and I have many people to
thank for getting me here.
Thank you to all my coworkers who picked up the slack while I worked on this (Oh, wait — I work alone —
never mind). Thank you to my co-authors, and my fine editors: Adaobi Obi Tulton, Paul Reese, and Dianne
Siebold. If there is any quality here, it is likely due to their hard work.
I am definitely grateful to all the people who regularly put up with my negativity, and help me to become
better: Eileen, Chris, Tharun, Kraig and Duncan. Thank you, thank you, thank you.
And finally, thanks to all of you that have selected this book. I hope it helps.
—Kent Sharkey
ThanKs To BeTh massi for being too busy to work on this project and thanks to the people at Wrox for
accepting Beth’s suggestion that I would be a suitable replacement.
As a first time author, I have to give special thanks to Adaobi Obi Tulton, Dianne Siebold, Doug Parsons,
Doug Waterfield, and Luann Rouff for helping me make my work appear as if it was written by a
professional author. I’m sure it wasn’t easy.
Finally, I’d like to thank those who helped me advance professionally to the point that this opportunity was
even possible: Craig Flanagan, Sasha Krsmanovic, Jean-Rene Roy, Mark Dunn, Carl Franklin, Richard
Campbell, all the Canadian RDs, and everyone at ObjectSharp.
—Rob Windsor
i Wish To acKnoWledge Paul Reese and Adaobi Obi Tulton; they had a lot of patience and they allowed
me to make the necessary changes to my chapter in order to include the most appropriate information about
the new parallel programming features introduced in .NET Framework 4 and Visual Basic 2010.
Special thanks go to my wife, Vanesa S. Olsen, because she understood that I needed to work with many
computers at the same time to test each code snippet.
—Gastón C. Hillar
conTenTs
IntroductIon
xxxv
ParT i: language consTrucTs and enVironmenT
chaPTer 1: Visual sTudio 2010
Visual studio 2010: express through Ultimate
Visual Basic Keywords and syntax
Console Applications
Creating a Project from a Project Template
The Solution Explorer
Project Properties
Assembly Information Screen
Compiler Settings
Debug Properties
References
Resources
Settings
Other Project Property Tabs
Project ProVB_Vs2010
Form Properties Set in Code
Additional Components for Visual Studio
3
4
7
10
11
14
15
16
17
21
22
24
25
28
28
31
39
enhancing a sample application
40
Customizing the Code
Building Applications
Reusing Your First Windows Form
41
45
52
Useful features of Visual studio 2010
Build Configurations
The Task List
The Command Window
Server Explorer
Recording and Using Macros in Visual Studio 2010
Class Diagrams
Application Lifecycle Management
Performance Tools
summary
54
54
56
57
57
58
60
61
64
66
conTenTs
chaPTer 2: oBJecTs and Visual Basic
object-oriented Terminology
67
68
Objects, Classes, and Instances
Composition of an Object
System .Object
68
69
72
Working With Visual Basic Types
72
Value and Reference Types
Primitive Types
Commands: Conditional
73
75
76
If Then
Comparison Operators
Select Case
77
77
79
Value Types (structures)
79
Boolean
Integer Types
Unsigned Types
Decimal Types
Char and Byte
DateTime
reference Types (Classes)
The Object Class
The String Class
XML Literals
The DBNull Class and IsDBNull Function
Parameter Passing
ParamArray
Variable scope
Working with objects
Objects Declaration and Instantiation
Object References
Dereferencing Objects
Early Binding versus Late Binding
Data Type Conversions
Performing Explicit Conversions
Creating Classes
Basic Classes
Handling Events
Handling Multiple Events
xvi
80
81
82
82
85
85
86
86
87
91
92
93
94
94
95
95
96
97
97
98
99
103
103
113
113
ConTenTs
The WithEvents Keyword
Raising Events
Declaring and Raising Custom Events
Receiving Events with WithEvents
Receiving Events with AddHandler
Constructor Methods
Termination and Cleanup
advanced Concepts
Overloading Methods
Overloading Constructor Methods
Shared Methods, Variables, and Events
Operator Overloading
Delegates
Classes versus Components
Lambdas
summary
chaPTer 3: cusTom oBJecTs
inheritance
Implementing Inheritance
Interacting with the Base Class, Your Class, and Your Object
Simple Constructors
Creating an Abstract Base Class
Multiple interfaces
Object Interfaces
Secondary Interfaces
abstraction
encapsulation
Polymorphism
Method Signatures
inheritance
When to Use Inheritance
How Deep to Go?
summary
chaPTer 4: The common language runTime
elements of a .neT application
Modules
Assemblies
Types
114
114
115
116
117
119
119
120
121
123
124
128
130
134
135
136
137
138
139
159
164
175
177
177
178
184
187
189
189
198
198
205
208
211
212
212
213
214
xvii
conTenTs
Versioning and Deployment
Better Support for Versioning
Major .Minor .Build .Revision
Better Deployment
214
214
215
215
Cross-language integration
216
The Common Type System
Metadata
Better Support for Metadata
Attributes
The Reflection API
216
217
218
218
220
il Disassembler
Memory Management
Traditional Garbage Collection
Faster Memory Allocation for Objects
Garbage Collector Optimizations
namespaces
What Is a Namespace?
Namespaces and References
Common Namespaces
Importing and Aliasing Namespaces
Aliasing Namespaces
Referencing Namespaces in ASP .NET
Creating Your own namespaces
The My Keyword
My .Application
My .Computer
My .Forms Namespace
My .Resources
My .User
extending the My namespace
summary
221
221
222
228
229
231
231
234
236
238
239
240
240
242
243
246
249
249
250
250
252
chaPTer 5: declaraTiVe Programming
WiTh Visual Basic
253
Declarative Programming and Visual Basic
Using XaMl to Create a Window
XaMl syntax
254
255
258
XAML Language Basics
XAML Directives
Using XaMl to Declare a Workflow
summary
xviii
259
261
262
264