ebook img

Pro VB 2010 and the .Net 4.0 Platform PDF

1767 Pages·2016·15.43 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro VB 2010 and the .Net 4.0 Platform

Pro VB 2010 and the .NET 4 Platform ■ ■ ■ Andrew Troelsen and Vidya Vrat Agarwal Pro VB 2010 and the .NET 4 Platform Copyright © 2010 by Andrew Troelsen and Vidya Vrat Agarwal All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2985-8 ISBN-13 (electronic): 978-1-4302-2986-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Andy Olsen Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editors: Mary Behr, Patrick Meader, Katie Stence, and Sharon Terdeman Production Support: Patrick Cunningham Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. Contents at a Glance About the Authors ..................................................................................................... liv About the Technical Reviewer ................................................................................... lv Acknowledgments .................................................................................................... lvi Introduction ............................................................................................................. lvii ■ Chapter 1: Introducing VB 2010 .............................................................................. 1 ■ Chapter 2: Building Visual Basic 2010 Applications ............................................. 41 ■ Chapter 3: Core VB 2010 Programming Constructs, Part I .................................... 71 ■ Chapter 4: Core VB 2010 Programming Constructs, Part II ................................. 125 ■ Chapter 5: Defining Encapsulated Class Types .................................................... 169 ■ Chapter 6: Understanding Inheritance and Polymorphism .................................. 227 ■ Chapter 7: Understanding Structured Exception Handling .................................. 273 ■ Chapter 8: Understanding Object Lifetime ........................................................... 303 ■ Chapter 9: Working with Interfaces ..................................................................... 333 ■ Chapter 10: Understanding Generics ................................................................... 375 ■ Chapter 11: Delegates, Events, and Lambdas ..................................................... 415 ■ Chapter 12: Advanced VB 2010 Language Features ............................................ 461 ■ Chapter 13: LINQ to Objects ................................................................................. 507 ■ Chapter 14: Configuring .NET Assemblies ........................................................... 543 ■ Chapter 15: Type Reflection, Late Binding, ■ and Attribute-Based Programming ..................................................................... 605 iv ■ CONTENTS AT A GLANCE ■ Chapter 16: Processes, AppDomains, and Object Contexts ................................. 653 ■ Chapter 17: Understanding CIL and the Role of Dynamic Assemblies ................. 687 ■ Chapter 18: Dynamic Types and the Dynamic Language Runtime ...................... 739 ■ Chapter 19: Multithreaded and Parallel Programming ........................................ 765 ■ Chapter 20: File I/O and Object Serialization ....................................................... 817 ■ Chapter 21: ADO.NET Part I: The Connected Layer .............................................. 869 ■ Chapter 22: ADO.NET Part II: The Disconnected Layer ......................................... 931 ■ Chapter 23: ADO.NET Part III: The Entity Framework .......................................... 999 ■ Chapter 24: Introducing LINQ to XML ................................................................ 1041 ■ Chapter 25: Introducing Windows Communication Foundation ........................ 1061 ■ Chapter 26: Introducing Windows Workflow Foundation 4.0 ............................ 1127 ■ Chapter 27: Introducing Windows Presentation Foundation and XAML ............ 1165 ■ Chapter 28: Programming with WPF Controls ................................................... 1227 ■ Chapter 29: WPF Graphics Rendering Services ................................................. 1295 ■ Chapter 30: WPF Resources, Animations, and Styles ........................................ 1335 ■ Chapter 31: WPF Control Templates and UserControls ...................................... 1377 ■ Chapter 32: Building ASP.NET Web Pages ......................................................... 1427 ■ Chapter 33: ASP.NET Web Controls, Master Pages, and Themes ...................... 1477 ■ Chapter 34: ASP.NET State Management Techniques ....................................... 1523 ■ Appendix A: Programming with Windows Forms .............................................. 1561 ■ Appendix B: Platform-Independent .NET Development with Mono .................... 1613 Index ..................................................................................................................... 1635 v Contents About the Authors ..................................................................................................... liv About the Technical Reviewer ................................................................................... lv Acknowledgments .................................................................................................... lvi Introduction ............................................................................................................. lvii ■ Chapter 1: Introducing VB 2010 .............................................................................. 1 Understanding the Previous State of Affairs ...................................................................... 1 Life As a C/Windows API Programmer ...................................................................................................... 1 Life As a C++/MFC Programmer .............................................................................................................. 2 Life As a Visual Basic 6.0 Programmer ..................................................................................................... 2 Life As a Java Programmer ...................................................................................................................... 3 Life As a COM Programmer ...................................................................................................................... 3 The .NET Solution .............................................................................................................. 4 Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS) ................. 5 The Role of the Base Class Libraries ........................................................................................................ 6 What Visual Basic 2010 Brings to the Table ............................................................................................. 6 Additional .NET-Aware Programming Languages .............................................................. 8 Life in a Multi-Language World .............................................................................................................. 10 An Overview of .NET Assemblies ..................................................................................... 10 Single-File and Multi-File Assemblies .................................................................................................... 12 The Role of the Common Intermediate Language .................................................................................. 12 The Role of .NET Type Metadata ............................................................................................................. 15 The Role of the Assembly Manifest ........................................................................................................ 17 vi ■ CONTENTS Understanding the Common Type System ....................................................................... 18 CTS Class Types ..................................................................................................................................... 18 CTS Interface Types ................................................................................................................................ 19 CTS Structure Types ............................................................................................................................... 19 CTS Enumeration Types .......................................................................................................................... 20 CTS Delegate Types ................................................................................................................................ 20 CTS Type Members ................................................................................................................................. 21 Intrinsic CTS Data Types ......................................................................................................................... 21 Understanding the Common Language Specification ...................................................... 22 Ensuring CLS Compliance ....................................................................................................................... 24 Understanding the Common Language Runtime ............................................................. 24 The Assembly/Namespace/Type Distinction ................................................................... 26 The Role of the Microsoft Root Namespace ........................................................................................... 29 Accessing a Namespace Programmatically ........................................................................................... 29 Referencing External Assemblies ........................................................................................................... 31 Exploring an Assembly Using ildasm.exe ........................................................................ 32 Viewing CIL Code .................................................................................................................................... 33 Viewing Type Metadata .......................................................................................................................... 34 Viewing Assembly Metadata (aka the Manifest) .................................................................................... 34 Exploring an Assembly Using Reflector ........................................................................... 35 Deploying the .NET Runtime ............................................................................................ 37 The .NET Client Profile Runtime .............................................................................................................. 37 The Platform-Independent Nature of .NET ....................................................................... 37 Summary ......................................................................................................................... 39 ■ Chapter 2: Building Visual Basic 2010 Applications ............................................. 41 The Role of the .NET Framework 4.0 SDK ....................................................................... 41 The Visual Studio 2010 Command Prompt ............................................................................................. 42 vii ■ CONTENTS Building VB 2010 Applications Using vbc.exe .................................................................. 43 Specifying Input and Output Targets ...................................................................................................... 44 Referencing External Assemblies ........................................................................................................... 45 Referencing Multiple External Assemblies ............................................................................................. 46 Compiling Multiple Source Files ............................................................................................................. 46 Working with VB 2010 Response Files ................................................................................................... 47 Building .NET Applications Using Notepad++ ................................................................. 49 Building .NET Applications Using Visual Basic 2010 Express .......................................... 50 Some Unique Features of Visual Basic 2010 Express ............................................................................ 51 Building .NET Applications Using Visual Studio 2010 ...................................................... 52 Some Unique Features of Visual Studio 2010 ......................................................................................... 53 Targeting the .NET Framework Using the New Project Dialog Box ........................................................ 53 Using the Solution Explorer Utility .......................................................................................................... 54 The Class View Utility ............................................................................................................................. 57 The Object Browser Utility ...................................................................................................................... 58 Integrated Support for Code Refactoring ................................................................................................ 59 Code Expansions Techniques ................................................................................................................. 61 The Visual Class Designer ...................................................................................................................... 62 The Integrated .NET Framework 4.0 SDK Documentation System ......................................................... 68 Summary ......................................................................................................................... 70 ■ Chapter 3: Core VB 2010 Programming Constructs, Part I .................................... 71 The Role of the Module Type ........................................................................................... 71 Projects with Multiple Modules .............................................................................................................. 73 Modules Are Not Creatable ..................................................................................................................... 74 Renaming Your Initial Module ................................................................................................................. 75 Members of Modules .............................................................................................................................. 75 viii ■ CONTENTS The Anatomy of a Simple VB 2010 Program .................................................................... 76 Variations on the Main() Method ............................................................................................................. 77 Specifying an Application Error Code ..................................................................................................... 78 Processing Command-Line Arguments .................................................................................................. 80 Specifying Command-Line Arguments with Visual Studio 2010 ............................................................ 81 An Interesting Aside: Some Additional Members of the System.Environment Class ....... 82 The System.Console Class ............................................................................................... 84 Basic Input and Output with the Console Class ...................................................................................... 85 Formatting Console Output ..................................................................................................................... 86 Formatting Numerical Data .................................................................................................................... 86 Formatting Numerical Data Beyond Console Applications ..................................................................... 88 System Data Types and VB 2010 Shorthand Notation ..................................................... 89 Variable Declaration and Initialization .................................................................................................... 91 Intrinsic Data Types and the New Operator ............................................................................................ 92 The Data Type Class Hierarchy ............................................................................................................... 93 Members of Numerical Data Types ........................................................................................................ 95 Members of System.Boolean .................................................................................................................. 96 Members of System.Char ....................................................................................................................... 96 Parsing Values from String Data ............................................................................................................. 97 System.DateTime and System.TimeSpan ............................................................................................... 98 The .NET 4.0 System.Numerics Namespace .......................................................................................... 98 Working with String Data ............................................................................................... 100 Basic String Manipulation .................................................................................................................... 101 String Concatenation ............................................................................................................................ 102 Special Character Constants ................................................................................................................ 103 End SubStrings and Equality ................................................................................................................ 104 Strings Are Immutable .......................................................................................................................... 105 The System.Text.StringBuilder Type .................................................................................................... 106 ix ■ CONTENTS Narrowing and Widening Data Type Conversions ......................................................... 108 Trapping Narrowing Data Conversions ................................................................................................ 111 Understanding Option Strict ................................................................................................................ 112 Setting Project-wide Overflow Checking ............................................................................................. 113 The Role of System.Convert ................................................................................................................ 114 Understanding Implicitly Typed Local Variables ........................................................... 115 Restrictions on Implicitly Typed Variables ........................................................................................... 116 Implicit Typed Data Is Strongly Typed Data ......................................................................................... 117 Usefulness of Implicitly Typed Local Variables .................................................................................... 118 VB 2010 Iteration Constructs ........................................................................................ 118 The For Loop ........................................................................................................................................ 119 > m o ok.c The For Each Loop ............................................................................................................................... 119 o b we The While and Do/While Looping Constructs ....................................................................................... 120 o w w. w Decision Constructs and the Relational/Equality Operators ......................................... 121 w < ook The If/Then/Else Statement ................................................................................................................. 121 B e ow! The Select/Case Statement ................................................................................................................. 123 W m d fro Summary ....................................................................................................................... 124 a o wnl ■Chapter 4: Core VB 2010 Programming Constructs, Part II . ............................... 125 o D Methods and Parameter Modifiers ............................................................................... 125 The ByVal Parameter Modifier ............................................................................................................. 126 The ByRef Modifier .............................................................................................................................. 128 The <Out()> Attribute .......................................................................................................................... 129 The ParamArray Modifier ..................................................................................................................... 130 Defining Optional Parameters .............................................................................................................. 132 Invoking Methods using Named Parameters ........................................................................................ 133 Understanding Method Overloading .................................................................................................... 135 x ■ CONTENTS Understanding VB 2010 Arrays ...................................................................................... 138 VB 2010 Array Initialization Syntax ....................................................................................................... 139 Implicitly Typed Local Arrays ................................................................................................................ 139 Defining an Array of Objects ................................................................................................................. 140 Working with Multidimensional Arrays ................................................................................................. 141 Arrays As Arguments or Return Values ................................................................................................ 143 The System.Array Base Class ............................................................................................................... 144 Understanding the Enum Type ....................................................................................... 146 Controlling the Underlying Storage for an Enum .................................................................................. 147 Declaring Enum Variables .................................................................................................................... 147 The System.Enum Type ........................................................................................................................ 148 Dynamically Discovering an Enum’s Name/Value Pairs ....................................................................... 149 Understanding the Structure Type ................................................................................. 152 Creating Structure Variables ................................................................................................................ 153 Understanding Value Types and Reference Types ......................................................... 155 Value Types, References Types, and the Assignment Operator ........................................................... 156 Value Types Containing Reference Types ............................................................................................. 158 Passing Reference Types by Value ....................................................................................................... 160 Passing Reference Types by Reference ............................................................................................... 162 Final Details Regarding Value Types and Reference Types .................................................................. 163 Understanding VB 2010 Nullable Types ......................................................................... 164 Working with Nullable Types ................................................................................................................ 165 The Nullable If Operator ........................................................................................................................ 166 Summary ....................................................................................................................... 167 ■ Chapter 5: Defining Encapsulated Class Types .................................................... 169 Introducing the VB 2010 Class Type .............................................................................. 169 Allocating Objects with the New Keyword ............................................................................................ 172 xi

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.