Tải bản đầy đủ (.pdf) (844 trang)

visual basic 2012 programmers reference

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (24.45 MB, 844 trang )

www.it-ebooks.info
ffirs.indd iffirs.indd i 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
VISUAL BASIC® 2012
PROGRAMMER’S REFERENCE
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
 PART I IDE
CHAPTER 1 Introduction to the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2 Menus, Toolbars, and Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
CHAPTER 3 Windows Forms Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
CHAPTER 4 WPF Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
CHAPTER 5 Visual Basic Code Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
CHAPTER 6 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
 PART II GETTING STARTED
CHAPTER 7 Selecting Windows Forms Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
CHAPTER 8 Using Windows Forms Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
CHAPTER 9 Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
CHAPTER 10 Selecting WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
CHAPTER 11 Using WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
CHAPTER 12 WPF Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
CHAPTER 13 Program and Module Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
CHAPTER 14 Data Types, Variables, and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
CHAPTER 15 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
CHAPTER 16 Subroutines and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
CHAPTER 17 Program Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 18 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
CHAPTER 19 Database Controls and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
CHAPTER 20 LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
CHAPTER 21 Metro-Style Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
ffirs.indd iffirs.indd i 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info


 PART III OBJECTORIENTED PROGRAMMING
CHAPTER 22 OOP Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
CHAPTER 23 Classes and Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
CHAPTER 24 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
CHAPTER 25 Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
CHAPTER 26 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
 PART IV INTERACTING WITH THE ENVIRONMENT
CHAPTER 27 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
CHAPTER 28 Confi guration and Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
CHAPTER 29 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
CHAPTER 30 Filesystem Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
 PART V APPENDICES
APPENDIX A Useful Control Properties, Methods, and Events . . . . . . . . . . . . . . . . . . . 611
APPENDIX B Variable Declarations and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
APPENDIX C Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
APPENDIX D Subroutine and Function Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 637
APPENDIX E Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
APPENDIX F Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
APPENDIX G Windows Forms Controls and Components . . . . . . . . . . . . . . . . . . . . . . 649
APPENDIX H WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
APPENDIX I Visual Basic Power Packs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
APPENDIX J Form Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
APPENDIX K Classes and Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
APPENDIX L LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
APPENDIX M Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
APPENDIX N Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
APPENDIX O Useful Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
APPENDIX P Date and Time Format Specifi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
ffirs.indd iiffirs.indd ii 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info

APPENDIX Q Other Format Specifi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
APPENDIX R The Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
APPENDIX S The My Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
APPENDIX T Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
APPENDIX U Filesystem Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
APPENDIX V Visual Studio Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .771
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
ffirs.indd iiiffirs.indd iii 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
ffirs.indd ivffirs.indd iv 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
Visual Basic
®
2012
PROGRAMMER’S REFERENCE
Rod Stephens
John Wiley & Sons, Inc.
ffirs.indd vffirs.indd v 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
Visual Basic® 2012 Programmer’s Reference
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2012 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-31407-4
ISBN: 978-1-118-33208-5 (ebk)
ISBN: 978-1-118-33535-2 (ebk)

ISBN: 978-1-118-43938-8 (ebk)
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 />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 speci cally disclaim all warranties, including
without limitation warranties of  tness for a particular purpose. No warranty may be created or extended by sales or pro-
motional 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 pub-
lisher 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 stan-
dard 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
. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2012940034
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 af liates, in the United States and other coun-
tries, and may not be used without written permission. Visual Basic is a registered trademark of Microsoft 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.
ffirs.indd viffirs.indd vi 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
ABOUT THE AUTHOR
ROD STEPHENS started out as a mathematician, but while studying at MIT, he
discovered how much fun programming is and has been programming profes-
sionally ever since. During his career, he has worked on an eclectic assortment of
applications in such  elds as telephone switching, billing, repair dispatching, tax
processing, wastewater treatment, concert ticket sales, cartography, and training
for professional football players.
Rod is a Microsoft Visual Basic Most Valuable Professional (MVP) and has taught
introductory programming at ITT Technical Institute. He has written more than
two dozen books that have been translated into languages from all over the world, and more than
250 magazine articles covering Visual Basic, C#, Visual Basic for Applications, Delphi, and Java.
Rod’s popular VB Helper website (
www.vb-helper.com) receives several million hits per month and
contains thousands of pages of tips, tricks, and example programs for Visual Basic programmers,
as well as example code for this book. His C# Helper website (
www.csharphelper.com) contains
similar material for C# programmers.
You can contact Rod at
or
ABOUT THE TECHNICAL EDITOR
BRIAN HOCHGURTEL has been doing .NET development for over ten years, and actually started his
.NET experience with Rod Stephens when they wrote the Wiley book Visual Basic .NET and XML
together in 2002. Currently Brian works as a SharePoint Developer and Administrator for a large
defense contractor in Colorado.
ffirs.indd viiffirs.indd vii 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info

Executive Editor
Robert Elliott
Senior Project Editor
Adaobi Obi Tulton
Technical Editor
Brian Hochgurtel
Production Editor
Daniel Scribner
Copy Editor
Kim Cofer
Editorial Manager
Mary Beth Wakefi eld
Freelancer Editorial Manager
Rosemarie Graham
Associate Director of Marketing
David Mayhew
Marketing Manager
Ashley Zurcher
Business Manager
Amy Knies
Production Manager
Tim Tate
Vice President and Executive Group
Publisher
Richard Swadley
Vice President and Executive Publisher
Neil Edde
Associate Publisher
Jim Minatel
Project Coordinator, Cover

Katie Crocker
Proofreader
Nicole Hirschman
Indexer
Ron Strauss
Cover Designer
Ryan Sneed
Cover Image
© Erik Isakson / Tetra Images / JupiterImages
CREDITS
ffirs.indd viiiffirs.indd viii 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
ACKNOWLEDGMENTS
THANKS TO BOB ELLIOTT, Adaobi Obi Tulton, Sydney Jones, Rayna Erlick, Kim Cofer, Daniel
Scribner, and all of the others who worked so hard to make this book possible.
Thanks also to Brian Hochgurtel for giving me another perspective and the bene t of his valuable
experience.
ffirs.indd ixffirs.indd ix 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
ffirs.indd xffirs.indd x 19/07/12 2:03 PM19/07/12 2:03 PM
www.it-ebooks.info
CONTENTS
INTRODUCTION xxvii
PART I: IDE
CHAPTER 1: INTRODUCTION TO THE IDE 3
Introducing the IDE 3
Di erent IDE Appearances 4
IDE Confi gurations 5
Projects and Solutions 6
Starting the IDE 6

Creating a Project 8
Saving a Project 11
Summary 13
CHAPTER 2: MENUS, TOOLBARS, AND WINDOWS 15
IDE Tools 15
Menus 16
File 16
Edit 18
View 19
Project 20
Build 24
Debug 24
Data 24
Format 25
Tools 25
Test 28
Window 28
Help 29
Toolbars 30
Secondary Windows 30
Toolbox 32
Properties Window 33
Summary 33
ftoc.indd xiftoc.indd xi 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xii
CONTENTS
CHAPTER 3: WINDOWS FORMS DESIGNER 35
Introducing Windows Forms Designer 35
Setting Designer Options 35

Adding Controls 37
Selecting Controls 38
Copying Controls 39
Moving and Sizing Controls 40
Arranging Controls 40
Setting Properties 40
Setting Group Properties 41
Using Smart Tags 41
Adding Code to Controls 42
Summary 43
CHAPTER 4: WPF DESIGNER 45
Introducing WPF Designer 45
Editor Weaknesses 46
Recognizing Designer Windows 47
Adding Controls 48
Selecting Controls 49
Moving and Sizing Controls 50
Setting Properties 51
Setting Group Properties 51
Adding Code to Controls 52
Summary 53
CHAPTER 5: VISUAL BASIC CODE EDITOR 55
Editing Code 55
Margin Icons 56
Outlining 58
Tooltips 59
IntelliSense 60
Code Coloring and Highlighting 61
Code Snippets 63
Architectural Tools 64

Rename 64
Go To Defi nition 64
Go To Type Defi nition 64
Highlight References 65
Find All References 65
Generate From Usage 65
ftoc.indd xiiftoc.indd xii 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xiii
CONTENTS
The Code Editor at Run Time 66
Summary 68
CHAPTER 6: DEBUGGING 69
Debugging and Testing 69
The Debug Menu 70
The Debug
➪ Windows Submenu 72
The Breakpoints Window 74
The Command and Immediate Windows 75
Summary 77
PART II: GETTING STARTED
CHAPTER 7: SELECTING WINDOWS FORMS CONTROLS 81
Controls 81
Controls Overview 82
Choosing Controls 86
Containing and Arranging Controls 87
Making Selections 89
Entering Data 90
Displaying Data 90
Providing Feedback 91

Initiating Action 92
Displaying Graphics 94
Displaying Dialog Boxes 94
Third-Party Controls 95
Summary 96
CHAPTER 8: USING WINDOWS FORMS CONTROLS 97
Using Controls and Components 97
Controls and Components 98
Creating Controls 99
Properties 101
Properties at Design Time 101
Properties at Run Time 104
Useful Control Properties 106
Position and Size Properties 109
Methods 110
Events 110
ftoc.indd xiiiftoc.indd xiii 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xiv
CONTENTS
Creating Event Handlers at Design Time 111
Validation Events 112
Summary 114
CHAPTER 9: WINDOWS FORMS 115
Using Forms 115
Transparency 116
About, Splash, and Login Forms 117
Mouse Cursors 118
Icons 120
Application Icons 121

Notifi cation Icons 121
Properties Adopted by Child Controls 122
Property Reset Methods 123
Overriding WndProc 123
MRU Lists 125
Dialog Boxes 126
Wizards 128
Summary 129
CHAPTER 10: SELECTING WPF CONTROLS 131
WPF Controls and Code 131
Controls Overview 132
Containing and Arranging Controls 133
Making Selections 136
Entering Data 137
Displaying Data 137
Providing Feedback 138
Initiating Action 138
Presenting Graphics and Media 139
Providing Navigation 140
Managing Documents 140
Digital Ink 141
Summary 142
CHAPTER 11: USING WPF CONTROLS 143
WPF Controls 143
WPF Concepts 144
Separation of User Interface and Code 144
WPF Control Hierarchies 145
ftoc.indd xivftoc.indd xiv 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xv

CONTENTS
WPF in the IDE 145
Editing XAML 146
Editing Visual Basic Code 147
XAML Features 148
Objects 148
Resources 151
Styles 152
Templates 153
Transformations 156
Animations 156
Drawing Objects 159
Procedural WPF 162
Documents 166
Flow Documents 166
Fixed Documents 168
XPS Documents 169
Summary 169
CHAPTER 12: WPF WINDOWS 171
Using WPF Windows 171
Window Applications 172
Page Applications 174
Browser Applications 174
Frame Applications 176
Summary 177
CHAPTER 13: PROGRAM AND MODULE STRUCTURE 179
Solutions and Projects 179
Hidden Files 180
Code File Structure 184
Code Regions 185

Conditional Compilation 186
Namespaces 193
Typographic Code Elements 195
Comments 195
XML Comments 195
Line Continuation 198
Implicit Line Continuation 199
Line Joining 200
Summary 200
ftoc.indd xvftoc.indd xv 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xvi
CONTENTS
CHAPTER 14: DATA TYPES, VARIABLES, AND CONSTANTS 203
Variables 203
Data Types 204
Type Characters 207
Data Type Conversion 210
Narrowing Conversions 210
Data Type Parsing Methods 212
Widening Conversions 213
The Convert Class 213
ToString 213
Variable Declarations 214
Attribute_List 214
Accessibility 215
Shared 216
Shadows 216
ReadOnly 219
Dim 219

WithEvents 220
Name 221
Bounds_List 222
New 223
As Type and Inferred Types 224
Initialization_Expression 225
Initializing Collections 228
Multiple Variable Declarations 229
Option Explicit and Option Strict 230
Scope 233
Block Scope 233
Procedure Scope 234
Module Scope 234
Namespace Scope 235
Restricting Scope 235
Parameter Declarations 236
Property Procedures 238
Enumerated Data Types 240
Anonymous Types 243
Nullable Types 244
Constants 244
Accessibility 245
As Type 245
Initialization_Expression 246
ftoc.indd xviftoc.indd xvi 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xvii
CONTENTS
Delegates 246
Naming Conventions 248

Summary 249
CHAPTER 15: OPERATORS 251
Understanding Operators 251
Arithmetic Operators 252
Concatenation Operators 253
Comparison Operators 253
Logical Operators 255
Bitwise Operators 257
Operator Precedence 257
Assignment Operators 259
The StringBuilder Class 260
Date and TimeSpan Operations 261
Operator Overloading 262
Summary 266
CHAPTER 16: SUBROUTINES AND FUNCTIONS 267
Managing Code 267
Subroutines 268
Attribute_List 268
Inheritance_Mode 272
Accessibility 273
Subroutine_Name 274
Parameters 274
Implements interface.subroutine 279
Statements 281
Functions 281
Property Procedures 283
Extension Methods 284
Lambda Functions 285
Relaxed Delegates 287
Asynchronous Methods 290

Calling EndInvoke Directly 291
Handling a Callback 293
Using Async and Await 295
Summary 297
ftoc.indd xviiftoc.indd xvii 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xviii
CONTENTS
CHAPTER 17: PROGRAM CONTROL STATEMENTS 299
Controlling Programs 299
Decision Statements 299
Single-Line If Then 300
Multiline If Then 300
Select Case 301
Enumerated Values 304
IIf 304
If 306
Choose 306
Looping Statements 308
For Next 308
Non-Integer For Next Loops 311
For Each 311
Enumerators 314
Iterators 316
Do Loop Statements 316
While End 318
Summary 318
CHAPTER 18: ERROR HANDLING 321
The Struggle for Perfection 321
Bugs versus Unplanned Conditions 322

Catching Bugs 323
Catching Unplanned Conditions 324
Global Exception Handling 326
Structured Error Handling 328
Exception Objects 330
Throwing Exceptions 331
Re-throwing Exceptions 333
Custom Exceptions 334
Debugging 335
Summary 336
CHAPTER 19: DATABASE CONTROLS AND OBJECTS 337
Data Sources 337
Automatically Connecting to Data 338
Connecting to the Data Source 338
Adding Data Controls to the Form 341
Automatically Created Objects 344
ftoc.indd xviiiftoc.indd xviii 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xix
CONTENTS
Other Data Objects 345
Data Overview 346
Connection Objects 347
Transaction Objects 350
Data Adapters 352
Command Objects 356
DataSet 358
DataTable 360
DataRow 363
DataColumn 365

DataRelation 366
Constraints 368
DataView 370
DataRowView 373
Simple Data Binding 373
CurrencyManager 374
Complex Data Binding 377
Summary 379
CHAPTER 20: LINQ 381
The Many Faces of LINQ 381
Introduction to LINQ 383
Basic LINQ Query Syntax 384
From 385
Where 386
Order By 386
Select 387
Using LINQ Results 389
Advanced LINQ Query Syntax 390
Join 390
Group By 391
Aggregate Functions 393
Set Operations 394
Limiting Results 394
LINQ Functions 395
LINQ Extension Methods 397
Method-Based Queries 397
Method-Based Queries with Lambda Functions 399
Extending LINQ 401
LINQ to Objects 403
LINQ to XML 404

XML Literals 404
ftoc.indd xixftoc.indd xix 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xx
CONTENTS
LINQ into XML 405
LINQ out of XML 406
LINQ to ADO.NET 409
LINQ to SQL and LINQ to Entities 409
LINQ to DataSet 410
PLINQ 413
Summary 414
CHAPTER 21: METROSTYLE APPLICATIONS 417
Building Metro-Style Applications 417
Starting a New Project 418
Special Image Files 419
Building MetroBones 420
Control Layout 421
XAML Code 421
Zooming in on the Controls 424
Visual Basic Code 424
Testing 428
Summary 429
PART III: OBJECTORIENTED PROGRAMMING
CHAPTER 22: OOP CONCEPTS 433
Introducing OOP 433
Classes 434
Encapsulation 436
Inheritance 437
Inheritance Hierarchies 438

Refi nement and Abstraction 438
“Has-a” and “Is-a” Relationships 441
Adding and Modifying Class Features 441
Interface Inheritance 443
Polymorphism 444
Method Overloading 445
Extension Methods 446
Summary 447
CHAPTER 23: CLASSES AND STRUCTURES 449
Packaging Data 449
Classes 450
ftoc.indd xxftoc.indd xx 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xxi
CONTENTS
Attribute_list 450
Partial 451
Accessibility 452
Shadows 453
Inheritance 454
Implements interface 456
Structures 456
Structures Cannot Inherit 457
Structures Are Value Types 457
Memory Required 457
Heap and Stack Performance 459
Object Assignment 459
Parameter Passing 460
Boxing and Unboxing 461
Class Instantiation Details 461

Structure Instantiation Details 464
Garbage Collection 466
Finalize 467
Dispose 469
Constants, Properties, and Methods 471
Events 473
Declaring Events 473
Raising Events 474
Catching Events 475
Shared Variables 477
Shared Methods 477
Summary 479
CHAPTER 24: NAMESPACES 481
Handling Name Confl icts 481
The Imports Statement 482
Automatic Imports 484
Namespace Aliases 486
Namespace Elements 486
The Root Namespace 487
Making Namespaces 487
Classes, Structures, and Modules 488
Resolving Namespaces 489
Summary 492
ftoc.indd xxiftoc.indd xxi 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xxii
CONTENTS
CHAPTER 25: COLLECTION CLASSES 493
Grouping Data 493
What Is a Collection? 494

Arrays 494
Array Dimensions 496
Lower Bounds 497
Resizing 497
Speed 498
Other Array Class Features 498
Collections 499
ArrayList 499
StringCollection 501
NameValueCollection 501
Dictionaries 503
ListDictionary 503
Hashtable 504
HybridDictionary 505
StringDictionary 505
SortedList 505
CollectionsUtil 505
Stacks and Queues 506
Stack 506
Queue 508
Generics 509
Collection Initializers 511
Iterators 512
Summary 513
CHAPTER 26: GENERICS 515
Class Creators 515
Advantages of Generics 516
Defi ning Generics 516
Generic Constructors 517
Multiple Types 518

Constrained Types 520
Instantiating Generic Classes 521
Imports Aliases 522
Derived Classes 523
Generic Collection Classes 523
Generic Methods 524
ftoc.indd xxiiftoc.indd xxii 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info
xxiii
CONTENTS
Generics and Extension Methods 524
Summary 526
PART IV: INTERACTING WITH THE ENVIRONMENT
CHAPTER 27: PRINTING 529
Printing Concepts 529
Basic Printing 530
Drawing Basics 534
Graphics Objects 534
Pens 536
Brushes 538
A Booklet Example 540
Summary 545
CHAPTER 28: CONFIGURATION AND RESOURCES 547
The Need for Confi guration 547
My 548
Me and My 549
My Sections 549
Environment 550
Setting Environment Variables 550
Using Environ 551

Using System.Environment 551
Registry 553
Native Visual Basic Registry Methods 554
My.Computer.Registry 556
Confi guration Files 559
Resource Files 562
Application Resources 562
Using Application Resources 563
Embedded Resources 564
Localization Resources 564
Application 566
Application Properties 566
Application Methods 567
Application Events 568
Summary 569
ftoc.indd xxiiiftoc.indd xxiii 18/07/12 11:22 AM18/07/12 11:22 AM
www.it-ebooks.info

×