1426 Professional Visual Basic 2012 and .NET 4.5 Programming

  

  PROFESSIONAL VISUAL BASIC® 2012 AND .NET 4.5 PROGRAMMING INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

  ⊲ PART I LANGUAGE CONSTRUCTS AND ENVIRONMENT

CHAPTER 1 Visual Studio 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 CHAPTER 2 The Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 CHAPTER 3 Objects and Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 CHAPTER 4 Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 CHAPTER 5 Advanced Language Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 CHAPTER 6 Exception Handling and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 ⊲ PART II BUSINESS OBJECTS AND DATA ACCESS CHAPTER 7 Arrays, Collections, and Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 CHAPTER 8 Using XML with Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 CHAPTER 9 ADO.NET and LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 CHAPTER 10 Data Access with the Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . 407 CHAPTER 11 Services (XML/WCF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 ⊲ PART III SPECIALIZED TOPICS AND LIBRARIES CHAPTER 12 XAML Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 CHAPTER 13 Creating XAML Applications for Windows 8 . . . . . . . . . . . . . . . . . . . . . .517 CHAPTER 14 Applications with ASP.NET, MVC, JavaScript, and HTML . . . . . . . . . . . 561 CHAPTER 15 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 CHAPTER 16 Application Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667 CHAPTER 17 Assemblies and Refl ection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693 CHAPTER 18 Security in the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 CHAPTER 19 Parallel Programming Using Tasks and Threads . . . . . . . . . . . . . . . . . . 757 CHAPTER 20 Deploying XAML Applications via the Windows 8 Windows Store . . 815 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  833

  

PROFESSIONAL

Visual Basic® 2012 and .NET 4.5

Programming

  

PROFESSIONAL

Visual Basic® 2012 and .NET 4.5

Programming

  

Bill Sheldon

Billy Hollis

Rob Windsor

David McCarter

  

Gastón C. H illar

Todd H erman

  Professional Visual Basic® 2012 and .NET 4.5 Programming Published by John Wiley & Sons, Inc.

  10475 Crosspoint Boulevard Indianapolis, IN 46256 Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada

  ISBN : 978-1-118-31445-6

  ISBN : 978-1-118-33213-9 (ebk)

  ISBN : 978-1-118-33542-0 (ebk)

  ISBN : 978-1-118-39638-4 (ebk) M anufactured in the United States of America 10 9 8 7 6 5 4 3 2 1

N o 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, M A 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, H oboken, N J 07030, (201) 748-6011, fax (201) 748-6008, or online at

  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 specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. N o 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. N either

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 publishes in a variety of print and electronic formats and by print-on-demand. Some material included with

standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media

such as a CD or DVD that is not included in the version you purchased, you may download this material at

  Library of Congress Control N umber: 2012946061 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 affi liates, in the United States and other countries, and may not be used without written permission. Visual Basic is a registered trademark of M icrosoft

Corporation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated

with any product or vendor mentioned in this book.

  

T his book is dedicated to Tracie, Billy, and Johnny, w ho had to

put up w ith m e lock ing m yself aw ay in m y hom e of fi ce and not

spending as m uch tim e w ith them as I'd lik e and they deserved.

  —Bill Sheldon I’d lik e to dedicate this book to those in the softw are developm ent com m unity w ho put users fi rst. I’ve w atched w ith regret as our profession has becom e inw ardly focused,

w orrying far m ore about technology and process than w hat w e

can accom plish for our users and the businesses for w hich they

w ork . I salute those w ho invest the tim e and effort to deliver

com pelling and w onderful ex periences to their users, and I hope

the m aterial I contributed to this book w ill help them do that.

  —Billy Hollis T his book is dedicated to you, the reader. Unless you didn’t pay for the book — in that case it’s dedicated to m y M om

  (love ya, M om ).

  —Rob Windsor To m y son, Kevin. —Gastón C. Hillar

  

For m y w ife, A m y. T hank you for your support w hile I w ork ed

on this project. I m ust also thank m y son, A idan, and daughter,

A laina, for their support and understanding w hile I w as busy in

m y of fi ce rather than spending tim e w ith them . I love all of you.

  T hank you.

  —Todd Herman

ABOUT THE AUTHORS

  BILL SHELDON is a software architect and engineer, originally from Baltimore, M aryland. H olding

  a degree in computer science from the Illinois Institute of Technology (IIT), Bill has worked in the

  IT industry since resigning his commission with the United States Navy. H e is the Vice President of Information Technology for Rubio’s Restaurants and has eight years as a M icrosoft M VP for Visual Basic. Bill lives in O ceanside, California, with his wife and two sons. Bill is an avid cyclist and is active in the fi ght against diabetes. You can track Bill down via Twitter: @NerdNotes .

  BILLY HOLLIS is a developer and user-experience designer based in N ashville, Tennessee. H is

  consulting company, N ext Version Systems, offers design and development on software applications requiring innovative and intuitive user experiences. H e speaks regularly at major industry confer- ences, usually on design concepts and user experience technologies. H e is also available for training in XAM L technologies and in user experience design concepts.

  ROB WINDSOR is a Lead SharePoint Consultant with Portal Solutions—a M icrosoft Gold Partner

  based in Washington, D.C., and Boston. H e has 20 years’ experience developing rich-client and web applications with Delphi, VB, C#, and VB.N ET, and is currently spending a majority of his time working with SharePoint. Rob is a regular speaker at conferences, code camps, and user groups across N orth America and Europe. H e regularly contributes articles and videos to M SDN , TechN et, and the Pluralsight O n-Demand library, and is the coauthor of Professional V isual Basic 2010 and

  .N ET 4

  . Rob is the founder and past president of the N orth Toronto .N ET User Group and has been recognized as a M icrosoft M ost Valuable Professional for his involvement in the developer community. You can follow Rob on Twitter at @robwindsor .

  DAVID MCCARTER is a M icrosoft M VP and a principal software engineer/architect in San Diego.

  H e is the editor-in-chief of dotN etTips.com, a website dedicated to helping programmers in all aspects of programming. David has written for programming magazines and has published four books, the latest of which is D avid M cCarter’s .N ET Coding Standards, and is available at:

   H e is one of the founders and directors of the 18-year-

  old San Diego .N ET Developers Group 2008 David won the IN ETA Community Excellence Award for his involvement in the .N ET community. David is also an inventor of a software printing system that was approved by the U.S. Patent O ffi ce in M ay 2008.

  GASTÓN C. HILLAR is an Italian living in Argentina. H e has been working with computers since he

  was eight years old. H e began programming with the legendary Texas TI-99/4A and Commodore 64 home computers in the early ’80s. H e has worked as developer, architect, project manager, and IT consultant for many companies around the world. H e is always looking for new adventures around the world. Gastón has written four books in English, contributed chapters to three other books, and has written more than 40 books in Spanish. H e contributes to Dr. Dobbs at In 2009, 2010, 2011, and 2012, he received the Intel® Black Belt Software Developer award. In 2011, he received the M icrosoft M VP on Technical Computing award.

  Gastón lives in Argentina with his wife, Vanesa, and his son, Kevin. When not tinkering with computers, he enjoys developing and playing with wireless virtual reality devices and electronic toys with his father, his son, and his nephew N ico. You can reach him at gastonhillar@hotmail

  .com . You can follow him on Twitter at

TODD HERMAN works for APX Labs as a senior software engineer. H is current focus is developing a

  robust library to support the XM PP standard. H e has been programming since he received his fi rst computer, a Commodore 64, on his 11th birthday. H is experience ranges from developing data entry software in FoxPro for a water research laboratory, to writing biometric applications in Visual Basic for N EC. H e lives in Virginia with his wife and children, spending his free time programming, playing computer games, and watching the SyFy Channel or reruns of Firefl y.

ABOUT THE TECHNICAL EDITORS

  DOUG WATERFIELD has been a software developer and architect for over 20 years and has been

  working with .N ET languages and related technologies since their fi rst release. H e has designed and constructed solutions for Fortune 500 and Defense Department clients through Chameleon Consulting, and he is a Senior Software Engineer with Interactive Intelligence, Inc. Doug graduated from Rose-H ulman Institute of Technology in 1988 and recently earned PM P (Project M anagement Professional) certifi cation from PM I. Doug and his family are very active in the Avon, Indiana, community through the Boy Scouts of America and other organizations. H e can be reached at

  

  

DOUG PARSONS lives in N ortheast O hio and has been developing software professionally for over

  15 years. H e has a diverse background, having worked in the political, fi nancial, medical, and manufacturing sectors over the course of his career. H e is currently employed as a Senior .N ET Developer with H arley-Davidson M otor Company. In his free time he tinkers with his various motorcycles, sits on the advisory committee of a H igh School Technology program, and spends time with his family.

  ACQUISITIONS EDITOR

  Katie Crocker

  VICE PRESIDENT AND EXECUTIVE GROUP PUBLISHER

  Richard Swadley

  VICE PRESIDENT AND EXECUTIVE PUBLISHER

  Neil Edde

  ASSOCIATE PUBLISHER

  Jim Minatel

  PROJECT COORDINATOR, COVER

  PROOFREADER

  PRODUCTION MANAGER

  Mark Steven Long

  INDEXER

  Robert Swanson

  COVER DESIGNER

  LeAndra Young

  COVER IMAGE

  © dan_prat / iStock

  Tim Tate

  Amy Knies

  Mary James

  Christina Haviland

  PROJECT EDITOR

  Christina Haviland

  TECHNICAL EDITORS

  Doug Waterfi eld Doug Parsons

  PRODUCTION EDITOR

  Daniel Scribner

  COPY EDITOR

  EDITORIAL MANAGER

  BUSINESS MANAGER

  Mary Beth Wakefi eld

  FREELANCER EDITORIAL MANAGER

  Rosemarie Graham

  ASSOCIATE DIRECTOR OF MARKETING

  David Mayhew

  MARKETING MANAGER

  Ashley Zurcher

  CREDITS

  ACKNOWLEDGMENTS

MANY THANKS TO ALL OF THE PEOPLE associated with getting this book together and out the door.

  M ore so than any other edition, there seemed to be a real struggle as we made some truly major changes to much of the content. Thanks to those who stepped up and met the challenges that we were presented with during the production cycle.

  —Bill Sheldon 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. I’d also 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, M ark Dunn, Carl Franklin, Richard Campbell, Barry Gervin, Dave Lloyd, Bruce Johnson, Donald Belcham, and everyone at Portal Solutions.

  —Rob Windsor

  CONTENTS

  

INTRODUCTION xxxi

PART I: LANGUAGE CONSTRUCTS AND ENVIRONMENT CHAPTER 1: VISUAL STUDIO 2012

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

CHAPTER 2: THE COMMON LANGUAGE RUNTIME

  

  

  

  

  

  

  CONTENTS

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

CHAPTER 3: OBJECTS AND VISUAL BASIC 103 Object-Oriented Terminology 105 Objects, Classes, and Instances 105 Composition of an Object

  

  

  

  

  

   xviii

  xix CONTENTS

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

CHAPTER 4: CUSTOM OBJECTS 179 Inheritance 180 When to Use Inheritance

  CONTENTS

  

  

  

  

  

  

  

  

  CHAPTE

  

  

  

  

  

  

  

  

  CHAPTE

  

   xx

  CONTENTS

  

  

  

  

  

  

  

  

  

  

  

  

CHAPTER 8: USING XML WITH VISUAL BASIC 315 An Introduction to XML

  

  

  

  

  

  

   xxi

  CONTENTS

  

  

  

  

  

   The XmlDataSource Control’s Namespace Problem 365

  

CHAPTER 9: ADO.NET AND LINQ 369 ADO.NET Architecture

  

  

  

  .

  

  

  

  

  

  

  

  

   xxii

  CONTENTS

  CHAPTE

  

  

  

  

  

  

  

  CHAPTE

  

  

  

  

  

  

  

  

  

  

   Looking at WSDL and the Schema for

   xxiii

  CONTENTS

  PART III: SPECIALIZED TOPICS AND LIBRARIES CHAPTE

  

  

  

  

  

  

  

  

  

  

  

  

  

  

   xxiv

  CONTENTS

  

  

  

  

  

  

  CHAPTE

  

  

  

  

  

  

  

  

   xxv

  CONTENTS

  

  

  

  

CHAPTER 14: APPLICATIONS WITH ASP.NET, MVC, JAVASCRIPT, AND HTML

  

  

  

  

  

  

CHAPTER 15: LOCALIZATION

  

  

  

  

  

   xxvi

  xxvii CONTENTS

  CHAPTE

  

  

  

  

  

  

  

  CHAPTE

  

  

  

  

  

  

  

  

  CONTENTS

  

  

  

CHAPTER 18: SECURITY IN THE .NET FRAMEWORK 719 Security Concepts and Defi nitions 720 Windows Store Projects

  

  

  

  

  

  

  

  

  

CHAPTER 19: PARALLEL PROGRAMMING USING TASKS AND THREADS

  

  

  

  

  

  

   xxviii

  CONTENTS

  

   Understanding a Task’s Life Cycle 791

  

  

  

  

  

  

  

  

  

  

  

  

  INDEX 833 xxix

  INTRODUCTION WELCOME TO THE NEXT ERA in .N ET development. .N ET has moved from a set of developer-

  focused tools and a runtime environment to the core of the latest M icrosoft operating system. In 2002, M icrosoft released .N ET and introduced developers to a new paradigm for building applica- tions. For Visual Basic it was not only a new environment, but really a new language.

  Visual Basic .N ET (as it was initially called) went beyond an overhaul. .N ET changed core elements of the syntax affecting every aspect of development with Visual Basic. The entire runtime model shifted to a new common language runtime (CLR) environment and the language went from object-based to object-oriented. Although most people didn’t recognize it, we were moving to a new language.

  N ow with the introduction of Windows RT, M icrosoft has again shifted the paradigm. N ot so much at the language level, but as it relates to how user interfaces are developed and work. The original runtime environment, although enhanced for .N ET 4.5, risks being fl agged as being on the path to that fatal “legacy” designator. Windows 8 introduces the concept of Windows Store applications, which are built on a version of the CLR that has different features, and that’s important. Because while client applications may view the core CLR as legacy, server-based applications have probably never been in a position to leverage it more. This book provides details about not only the latest version of Visual Basic and the new .N ET Framework 4.5. More important, it carries this coverage to a new platform, Windows RT, and a new class of Windows Store applications. As a result the contents of this book have been turned upside down. This book doesn’t just indicate that there are new Windows Store applications, but focuses in directly on how to build and deploy this new class of applications. The result is a very dif- ferent book from the previous edition. If you compare this edition to an older edition you’ll immediately realize that this edition is visibly smaller. Just as you saw Visual Basic evolve nearly 10 years ago, .N ET is going through an evolu- tion of its own. The result was a need to refocus on what this book covers. This has resulted in a sea change with regard to where to focus coverage for Visual Basic.

  The most noticeable omission from the book is in fact the original smart client development model—Windows Forms. When M icrosoft introduced WPF it informally announced that the era of Windows Forms was ending. It has taken some time, and certainly support will continue for many more years, but the reality is that the evolution of Windows Forms is complete. The information around Windows Forms provided in past editions of this book is essentially complete. While one or two of the chapters do still reference Windows Forms in their samples, by and large the book has moved beyond the use of this technology. The result was that Billy H ollis, who has a passion for user interface design, agreed to take on the rather signifi cant task of re-imagining how to approach user interface design in a world that includes Windows RT. The new XAM L-based interface design chapters are completely redone from the ground up and focused on teaching developers how to approach XAM L development from the ground up. The last version of the book approached the user interface model from Windows Forms

  INTRODUCTION

  and transitioning to XAM L. H owever, in this version the approach takes on XAM L as the primary user interface development model. As such these chapters address Windows Store application development head-on, not as an afterthought. However, Windows Forms wasn’t alone in being moved into the past. We’ve eliminated several appen- dices, M icrosoft Offi ce (both VSTO and SharePoint) integration, and references to classic COM . Some, for example, development around M icrosoft Offi ce, is facing its own set of changes as M icrosoft Offi ce prepares to evolve. O thers, such as classic COM and Windows Forms, are no longer technologies that developers should be targeting. We also found ourselves needing to change out how we addressed larger topics such as ASP.N ET and Silverlight. The result is that this book is much more focused on building applications using Visual Basic that target M icrosoft’s core next generation of technologies.

THE FUTURE OF VISUAL BASIC

  Early in the adoption cycle of .N ET, M icrosoft’s new language, C#, got the lion’s share of atten- tion. H owever, as .N ET adoption has increased, Visual Basic’s continuing importance has also been apparent. M icrosoft has publicly stated that it considers Visual Basic to be the language of choice for applications for which developer productivity is one of the highest priorities.

  In the past, it was common for M icrosoft and others to “target” different development styles; with Visual Studio 2010, M icrosoft announced that VB and C# will follow a process of coevolution. As new language features are developed, they will be introduced to both Visual Basic and C# at the same time. This release is the fi rst step in that process, although it’s not complete at this time.

  Coevolution does not mean that the languages will look the same, but rather that they will support the same capabilities. For example, Visual Basic has XM L literals, but that doesn't mean C# will get exactly the same functionality, as C# has the capability to work with XM L through the existing framework classes. The old process of fi rst introducing a 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 are being introduced to both Visual Basic and C# at the same time.

  This leads to a discussion of the “ Roslyn” compiler implementation. It seems like almost fi ve years ago that the fi rst whispers of a new 64-bit Visual Basic compiler implemented with Visual Basic started. Considered the standard for a serious language, implementing a language compiler in the language it compiles has become something of a standard.

  H owever, over time this project evolved. M icrosoft, seeing commonalities across the C# and Visual Basic compilation projects, realized that once the core syntax had been consumed the remainder of the compilation process was common across languages. While each implementation language needed a custom solution to handle parsing and interpreting the raw “code,” once that code had been con- verted to Intermediate Language (IL) the remaining compiler steps were essentially the same.

  Suddenly a new concept—the compiler as a service—was created. Code-named Roslyn, this is the future for both Visual Basic and C# compilation. Roslyn takes the traditional compiler as a “black-box” and provides an interface that for interacting with the creation of .N ET assemblies. Introducing an API that exposes your components during the compilation process is a powerful tool. Roslyn has been in a technology preview model since well before the release of Visual Studio

  xxxii

  INTRODUCTION

  2012 and .N ET 4.5—however, M icrosoft isn’t quite ready to declare it ready for prime time. As a result it’s still an optional add-on with Visual Studio 2012. H owever, even though Roslyn isn’t part of Visual Studio 2012, Visual Studio 2012 includes a few paradigm shifts. For starters you’ll fi nd that you can now work on projects that targeted older versions of Visual Studio without breaking backward compatibility in those projects. Visual Studio 2012 was designed so that those people who move to the latest tools are limited when working with a team that hasn’t fully migrated to that new version. More important, Visual Studio 2012 comes with a promise of updates. It’ll be interesting to see how this plays out over the coming months, but the Visual Studio team has indicated that they will be releasing regular updates to Visual Studio. Update 1 has already been announced as this book goes to print, and the team has indicated that they would like to continue with updates on a quarterly basis. This goes beyond what we’ve seen in the past, with Power Pack style updates that occurred out of the standard release cycle. Instead we see that M icrosoft is committing to keeping Visual Studio on the cutting edge of evolving technology. As the environments we developers need to support change, we can expect that Visual Studio will be adapting and incrementally improving to help us. While these changes may not involve changes to the core of the .N ET framework, we can expect .N ET to remain the most productive environment for custom applications. O ne of the most important advan- tages of the .N ET Framework is that it enables applications to be written with dramatically less code then other alternatives. Originally this was in comparison to older technologies, but today the compari- son is as opposed to writing native solutions that support the many different platforms and operating systems you need to support. In the world of business applications, the goal is to concentrate on writing business logic and to eliminate routine coding tasks as much as possible. In other words, of greatest value in this new paradigm is writing robust, useful applications without churning out a lot of code. Visual Basic is an excellent fi t for this type of development, which makes up the bulk of software development in today’s economy. M oreover, it will grow to be an even better fi t as it is refi ned and evolves for exactly that purpose.

WHO THIS BOOK IS FOR

  This book was written to help experienced developers learn Visual Basic. For those who are just starting the transition from other languages or earlier versions to those who have used Visual Basic for a while and need to gain a deeper understanding, this book provides information on the most common programming tasks and concepts you need.

  Profession al V isual Basic 2012 and .N ET 4.5

  Program m ing offers a wide-ranging presentation of Visual Basic concepts, but the .N ET Framework is so large and comprehensive that no single book can cover it all. The focus in this book is providing a working knowledge of key technologies that are important to Visual Basic developers. It provides adequate knowledge for a developer to work across both Windows Store applications through WCF services. This book is meant to provide a breadth of knowledge about how to leverage Visual Basic when developing applications. For certain specifi c technologies, developers may choose to add to their knowledge by following this book with a book dedicated entirely to a single technology area.

  xxxiii

  INTRODUCTION WHAT THIS BOOK COVERS This book covers Visual Basic from start to fi nish. It starts by introducing Visual Studio 2010.

  As the tool you’ll use to work with Visual Basic, understanding Visual Studio’s core capabilities is key to your success and enjoyment with building .N ET applications. In these pages, you have the opportunity to learn everything from database access, Language Integrated Q ueries (LIN Q ), and the Entity Framework, to integration with other technologies such as WPF, WCF, and service- based solutions. Along with investigating new features in detail, you’ll see that Visual Basic 10 has emerged as a powerful yet easy-to-use language that enables you to target the Internet just as easily as the desktop. This book covers the .N ET Framework 4.

HOW THIS BOOK IS STRUCTURED

  Part I, “Language Constructs and Environment” —The fi rst six chapters of the book focus on core

  language elements and development tools used by Visual Basic developers. This section introduces Visual Studio 2012, objects, syntax, and debugging.

  ‰

  Chapter 1, “Visual Studio 2012” —Start with the environment where you will work with Visual Basic. This chapter looks at the Visual Studio development environment. Introducing

  a simple WPF application project and reviewing key capabilities like the debugger, this chap- ter will help you to prepare for and become comfortable with this powerful environment.

  ‰

  Chapter 2, “The Common Language Runtime”

  —This chapter examines the core of the .N ET platform: the common language runtime (CLR). The CLR is responsible for manag- ing the execution of code compiled for the .N ET platform, as well as on the Windows RT platform. This chapter introduces you to how the different versions of the CLR are in fact closer to different operating systems than to a common environment. You’ll learn about versioning and deployment, memory management, cross-language integration, metadata, and the IL Disassembler. The chapter also introduces namespaces and their hierarchi- cal structure. An explanation of namespaces and some common examples are provided. In addition, you learn about custom namespaces, and how to import and alias existing namespaces within projects. This chapter also looks at the My namespace available in Visual Basic.

  ‰

  Chapter 3, “Objects and Visual Basic” —This is the fi rst of two chapters that explore object-

  oriented programming in Visual Basic. This chapter introduces the basics of objects, types, type conversion, reference types, and the key syntax which make up the core of Visual Basic.

  ‰

  Chapter 4, “Custom Objects”

  —This chapter examines creating objects, and describes how they fi t within Visual Basic. Starting with inheritance, you create simple and abstract classes and learn how to create base classes from which other classes can be derived. This chapter puts the theory of object-oriented development into practice. The four defi ning object-ori- ented concepts (abstraction, encapsulation, polymorphism, inheritance) are described, and you will learn how these concepts can be applied in design and development to create effec- tive object-oriented applications.

  xxxiv

  INTRODUCTION

  

Chapter 5, “Advanced Language Constructs” —This chapter looks at some of the more

  advanced language concepts such as lambda expressions, the new Async keyword, and Iterators. Each of these provides key capabilities that are new to Visual Basic 2012, and this new chapter provides details on how you can leverage these new language constructs.

  ‰

  Chapter 6, “Exception Handling and Debugging” —This chapter covers how error handling and debugging work in Visual Basic by discussing the CLR exception handler and the Try ... Catch ... Finally structure. Also covered are error and trace logging, and how you can use these methods to obtain feedback about how your program is working. Part II, “Business Objects and Data Access” —The next fi ve chapters, Chapter 7 through Chapter

  11, look at common structures used to contain and access data. This includes framework elements such as arrays and collections, XM L, database access, and Windows Communication Foundation (WCF) services. These chapters focus on gathering data for use within your applications.

  ‰

  Chapter 7, “Arrays, Collections, Generics” —This chapter focuses on introducing arrays and

  collections as a baseline for having a set of related items. It then expands on these basic struc- tures by exploring generics. Introduced with version 2.0 of the .N ET Framework, generics enable strongly typed collections.

  ‰

  

Chapter 8, “Using XML with Visual Basic” —This chapter presents the features of the

  .N ET Framework that facilitate the generation and manipulation of XM L. We describe the .N ET Framework’s XM L-related namespaces, and a subset of the classes exposed by these namespaces is examined in detail.

  ‰

  Chapter 9, “ADO.N ET and LIN Q” —This chapter focuses on what you need to know about

  the ADO .N ET object model in order to build fl exible, fast, and scalable data-access objects and applications. The evolution of ADO into ADO .N ET is explored, and the main objects in ADO .N ET that you need to understand in order to build data access into your .N ET appli- cations are explained. Additionally, this chapter delves into LIN Q to SQ L. LIN Q offers the capability to easily access underlying data—basically a layer on top of ADO .N ET. M icrosoft has provided LIN Q as a lightweight façade that provides a strongly typed interface to the underlying data stores.

  ‰

  Chapter 10, “Data Access with the Entity Framework” —The EF represents M icrosoft’s

  implementation of an Entity Relationship M odeling (ERM ) tool. Using EF, developers can generate classes to represent the data structures that are defi ned within SQ L Server, and leverage these objects within their applications.

  ‰

  Chapter 11, “Services (XML/ WCF)” —This chapter looks at how to build service-oriented components that allow for standards-based communications over a number of protocols. WCF is M icrosoft’s answer for component communications within and outside of the enterprise. Part III, “Specialized Topics and Libraries” —Chapters 12 through Chapter 14 focus on creating

  client applications. These chapters address Windows Store applications, which are exclusive to the Windows RT CLR. In parallel it discusses building applications for WPF that are compatible with earlier versions of Windows and which represent the majority of corporate applications. Chapter 14

  xxxv

  INTRODUCTION

  moves to looking at web-based applications and interfaces. Chapters 15 through 20 then focus on topics such as localization, windows services, security, multi-threaded applications, and deployment.

  ‰

  Chapter 12, “XAML Essentials” —Introduced in .N ET 3.0, XAM L is the syntax originally

  associated with the Windows Presentation Foundation. With the transition to Windows Store applications, XAM L is still applicable, although it behaves slightly differently. This chap- ter introduces you to building applications focused on the XAM L model of user interface declaration.

  ‰

  Chapter 13, “Creating XAML Applications for Windows 8” —In this chapter you go deeper

  into the specifi cs around building Windows Store applications. The chapter looks at spe- cifi c conventions around Windows 8 user interfaces, new features specifi c to Windows 8, and leveraging the visual designer that is part of Visual Studio 2012. This chapter gets into the nuts-and-bolts steps for handling things like Live Tiles and contracts for Windows 8 integration.

  ‰

  Chapter 14, “Applications with ASP.N ET, MVC, JavaScript, and HTML” —This chapter

  goes through web-based application development. It covers examples of everything from ASP.N ET with AJAX and CSS to M VC (M odel-View-Controller) applications.

  ‰

  Chapter 15, “Localization” —This chapter looks at some of the important items to con- sider when building your applications for worldwide use. It looks closely at the System. Globalization namespace and everything it offers your applications.

  ‰

  Chapter 16, “Application Services” —This chapter examines how Visual Basic is used in the

  production of Windows Services. The creation, installation, running, and debugging of Windows Services are covered.

  ‰

  Chapter 17, “Assemblies and Refl ection” —This chapter examines assemblies and their use

  within the CLR. The structure of an assembly, what it contains, and the information it con- tains are described. In addition, you will look at the manifest of the assembly and its role in deployment, and how to use remoting.

  ‰

  

Chapter 18, “Security in the .N ET Framework” —This chapter examines the System

.Security.Permissions namespace including how it relates to managing permissions. You

  also look at the System.Security.Cryptography namespace and run through some code that demonstrates its capabilities.

  ‰

  Chapter 19, “Parallel Programming Using Tasks and Threads” —This chapter explores

  threading and explains how the various objects in the .N ET Framework enable any of its consumers to develop multithreaded applications. You will learn how threads can be created, how they relate to processes, and the differences between multitasking and multithreading.

  ‰

  Chapter 20, “Deploying XAML Applications via the Windows 8 Windows Store” —This

  chapter takes a close look at using the Windows Store to deploy applications. You will see the new Windows 8 deployment options and how to set up an account with the Windows Store for deploying your applications. It also looks at how enterprise developers will deploy custom internal line-of-business applications on Windows 8.

  xxxvi

INTRODUCTION WHAT YOU NEED TO USE THIS BOOK

  The source code for the samples is available for download from the Wrox website at:

  ‰ We italicize new terms and important words when we introduce them. ‰ We show keyboard strokes like this: Ctrl+A.

  As for styles in the text:

  NOTE T ips, hints, trick s, and asides to the current discussion are offset and placed in italics lik e this.

  

WARNING Box es lik e this one hold im portant, not-to-be forgotten inform ation

that is directly relevant to the surrounding tex t.

  To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.

   CONVENTIONS

  xxxvii

  Although it is possible to create Visual Basic applications using the command-line tools contained in the .N ET Framework, you’ll want Visual Studio 2012, which includes the .N ET Framework 4.5, to get the most out of this book. In addition, note the following:

  To build Windows Store applications you’ll need to be running Windows 8 or Windows Server 2012. Visual Studio 2012 doesn’t require this operating system but building these applications does.

  ‰

  Some chapters make use of SQ L Server 2008. You can run the example code using M icrosoft’s SQ L Express, which is a free download.

  ‰

  You’ll need .N ET Framework 4.5, which is installed with whatever version of Visual Studio 2012 you select.

  ‰