www.it-ebooks.info PROFESSIONAL Visual Basic® 2010 and .NET 4 Bill Sheldon Billy Hollis Kent Sharkey Jonathan Marbutt Rob Windsor Gastón C. Hillar www.it-ebooks.info 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. www.it-ebooks.info 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 www.it-ebooks.info 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 Oce 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 www.it-ebooks.info 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. www.it-ebooks.info 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: gastonhillar@hotmail.com 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 dsiebold@earthlink.net. 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 douglas.c.parsons@gmail.com. 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. www.it-ebooks.info 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 www.it-ebooks.info CONTENTS INTRODUCTION xxxv PART I: LANGUAGE CONSTRUCTS AND ENVIRONMENT CHAPTER 1: VISUAL STUDIO 2010 3 Visual Studio 2010: Express through Ultimate 4 Visual Basic Keywords and Syntax 7 Console Applications 10 Creating a Project from a Project Template 11 The Solution Explorer 14 Project Properties 15 Assembly Information Screen 16 Compiler Settings 17 Debug Properties 21 References 22 Resources 24 Settings 25 Other Project Property Tabs 28 Project ProVB_VS2010 28 Form Properties Set in Code 31 Additional Components for Visual Studio 39 Enhancing a Sample Application 40 Customizing the Code 41 Building Applications 45 Reusing Your First Windows Form 52 Useful Features of Visual Studio 2010 54 Build Configurations 54 The Task List 56 The Command Window 57 Server Explorer 57 Recording and Using Macros in Visual Studio 2010 58 Class Diagrams 60 Application Lifecycle Management 61 Performance Tools 64 Summary 66 www.it-ebooks.info CONTENTS xvi CHAPTER 2: OBJECTS AND VISUAL BASIC 67 Object-Oriented Terminology 68 Objects, Classes, and Instances 68 Composition of an Object 69 System.Object 72 Working With Visual Basic Types 72 Value and Reference Types 73 Primitive Types 75 Commands: Conditional 76 If Then 77 Comparison Operators 77 Select Case 79 Value Types (Structures) 79 Boolean 80 Integer Types 81 Unsigned Types 82 Decimal Types 82 Char and Byte 85 DateTime 85 Reference Types (Classes) 86 The Object Class 86 The String Class 87 XML Literals 91 The DBNull Class and IsDBNull Function 92 Parameter Passing 93 ParamArray 94 Variable Scope 94 Working with Objects 95 Objects Declaration and Instantiation 95 Object References 96 Dereferencing Objects 97 Early Binding versus Late Binding 97 Data Type Conversions 98 Performing Explicit Conversions 99 Creating Classes 103 Basic Classes 103 Handling Events 113 Handling Multiple Events 113 www.it-ebooks.info [...]... Object 41 0 41 0 41 1 41 1 41 3 41 4 41 6 41 8 41 8 41 9 42 1 42 1 42 2 42 2 42 3 42 4 xxi www.it-ebooks.info CONTENTS How Objects Map to LINQ Objects The DataContext Object The Table(TEntity) Object Querying the Database 42 5 42 6 42 8 42 8 Using Query Expressions Query Expressions in Detail Filtering Using Expressions Performing Joins Grouping Items Stored Procedures Updating the Database Summary 42 9 42 9 42 9 43 0 43 1 43 2 43 3... 43 3 43 5 Chapter 11: Data Access with the Entity Framework Object-Relational Mapping Entity Framework Architecture Conceptual Model Storage Model Mapping Model LINQ to Entities The ObjectContext 43 7 43 8 43 8 43 9 44 2 44 3 44 3 44 5 Mapping Objects to Entities 44 6 Simple Mapping Using a Single Table for Multiple Objects Using Multiple Tables for an Object 44 6 44 8 45 0 Generating the Database from a Model 45 2... VB 2010 Dialog Forms Forms at Runtime Default Instances of Forms Controls 543 543 543 544 544 544 545 547 547 547 548 549 550 551 552 Control Tab Order Properties for All Controls Dynamic Sizing and Positioning of Controls FlowLayoutPanel Control TableLayoutPanel Control Panel and GroupBox Container Controls Extender Providers Advanced Capabilities for Data Entry Validating Data Entry Toolbars and. .. an XML Document 382 3 84 Lambda Expressions in Visual Basic Summary Chapter 10: ADO.NET and LINQ ADO.NET Architecture Basic ADO.NET Features 385 387 389 390 391 Common ADO.NET Tasks Basic ADO.NET Namespaces and Classes ADO.NET Components .NET Data Providers 391 395 396 398 Connection Object Command Object Using Stored Procedures with Command Objects DataReader Object Executing Commands Asynchronously... 40 2 40 4 40 6 40 9 40 9 41 0 DataTableCollection DataRelationCollection ExtendedProperties Creating and Using DataSet Objects ADO.NET DataTable Objects Advanced ADO.NET Features of the DataSet and DataTable Objects Working with the Common Provider Model Connection Pooling in ADO.NET Transactions and System.Transactions Creating Transactions Creating Resource Managers LINQ to SQL LINQ to SQL and Visual Basic. .. Late Binding 931 932 935 935 936 ActiveX Controls 940 The Legacy ActiveX Control A NET Application, Again Trying It All Out, Again Using NET Components in the COM World A NET Component RegAsm TlbExp 940 941 944 944 944 946 947 P/Invoke 947 Windows API Code Pack 948 Summary 948 Chapter 29: Network Programming Protocols, Addresses, and Ports Addresses and Names Ports: They’re Not Just for Ships Firewalls:... feature in Visual Basic and then in the next release having C# catch up, and vice versa, is over As new capabilities and features are introduced, they will be introduced to both Visual Basic and C# at the same time As mentioned earlier, although the changes aren’t complete, the next version of Visual Basic will be coordinated with a new release of Visual Studio, and the capabilities of C# and Visual Basic. .. Working with Both VBA and VSTO Creating a Document Template (Word) Adding Content to the Document Adding a Ribbon and an Actions Pane Activating the Actions Pane Updating a Content Control Creating an Office Add-In (Excel) Outlook Form Regions Summary Chapter 26: Windows Workflow Foundation Workflow in Applications Building Workflows 843 844 844 844 845 846 847 852 8 54 856 859 862 8 64 871 880 881 881 882... 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 231 2 34 236 238 239 240 240 242 243 246 249 249 250 Extending the My Namespace Summary 250 252 Chapter 5: Declarative Programming with Visual Basic 253 Declarative... Server 2008 Features xxii www.it-ebooks.info 45 9 46 2 46 8 47 0 47 0 47 1 48 3 48 9 49 4 CONTENTS WCF Data Services 49 5 REST Atom and JSON Exposing Data Using WCF Data Services WCF Data Services Client Library Summary 49 5 49 5 49 7 501 505 Chapter 13: Services (XML/WCF) Introduction to Services 507 508 The Network Angle Application Development Merging the Network and Application Development The Foundations of Web . www.it-ebooks.info PROFESSIONAL Visual Basic 2010 and .NET 4 Bill Sheldon Billy Hollis Kent Sharkey Jonathan Marbutt Rob Windsor Gastón C. Hillar www.it-ebooks.info Professional Visual Basic 2010 and .NET 4 Published. book. www.it-ebooks.info 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 . . TopMost Property 544 Owned Forms 544 Making Forms Transparent and Translucent 545 Visual Inheritance 547 Scrollable Forms 547 MDI Forms 547 An MDI Example in VB 2010 548 Dialog Forms 549 Forms at Runtime