VIII
Preface
xli
1
Introduction to .NET and Visual C++
®.NET
1
1.1 Introduction 2
1.2 History of the Internet and World Wide Web 2
1.3 World Wide Web Consortium (W3C) 4
1.4 Extensible Markup Language (XML) 4
1.5 Key Software Trend: Object Technology 5
1.6 Introduction to Microsoft .NET 7
1.7 Visual C++ .NET 9
1.8 .NET Framework and the Common Language Runtime 11
1.9 Tour of the Book 13
1.10 Summary 21
1.11 Internet and World Wide Web Resources 23
2
Visual Studio
®.NET IDE and Visual C++ .NET Programming
2.1 Introduction 26
2.2 Visual Studio .NET Integrated Development Environment (IDE) Overview 26
2.3 Menu Bar and Toolbar 30
2.4 Visual Studio .NET Windows 32
2.4.1 SolutionExplorer 32
2.4.2 Properties Window 33
2.5 Using Help 33
2.6 Creating a Simple Console Application 35
2.7 Simple Program: Printing a Line of Text 38
2.8 Arithmetic 43
2.9 Decision Making: Equality and Relational Operators 44
2.10 Summary 50
3
Control Structures
52
3.1 Introduction 53
3.2 Control Structures 53
3.3 if Selection Structure 55
3.4 if/else Selection Structure 56
3.5 while Repetition Structure 57
3.6 Assignment Operators 58
3.7 Increment and Decrement Operators 59
3.8 for Repetition Structure 61
3.9 Example: Using the for Structure to Compute Compound Interest 63 3.10 switch Multiple-Selection Structure 66 3.11 do/while Repetition Structure 70
3.12 Statements break and continue 71
3.13 Logical and Conditional Operators 73
IX
4
Functions and Arrays
80
4.1 Introduction 81
4.2 Functions and Methods in Managed Extensions for C++ 81
4.3 Function Definitions 82
4.4 Argument Promotion 86
4.5 Managed Extensions for C++ Namespaces 89
4.6 Value Types and Reference Types 90
4.7 Passing Arguments: Pass-by-Value vs. Pass-by-Reference 91
4.8 Scope Rules 93
4.9 Recursion 96
4.10 Function Overloading 99
4.11 Arrays 101
4.12 Declaring and Allocating Arrays 102
4.13 Arrays and Functions 105
4.14 Passing Arrays by Value and by Reference 106
4.15 Multiple-Subscripted Arrays 111
4.16 Summary 116
5
Object-Based Programming
118
5.1 Introduction 119
5.2 Implementing a Time Abstract Data Type with a Class 120
5.3 Class Scope 129
5.4 Controlling Access to Members 130
5.5 Initializing Class Objects: Constructors 132
5.6 Using Overloaded Constructors 132
5.7 Properties 138
5.8 Composition: Objects Pointers as Instance Variables of Other Classes 145
5.9 Using the this Pointer 150
5.10 Garbage Collection 153
5.11 static Class Members 153
5.12 const Keyword and Read-Only Properties 158
5.13 Indexed Properties 162
5.14 Data Abstraction and Information Hiding 167
5.15 Software Reusability 169
5.16 Namespaces and Assemblies 169
5.17 ClassView 175
5.18 Summary 176
6
Object-Oriented Programming: Inheritance
179
6.1 Introduction 180
6.2 Base Classes and Derived Classes 181
6.3 protected Members 184
6.4 Relationship Between Base Classes and Derived Classes 184
6.5 Example: Three-Level Inheritance Hierarchy 208
6.6 Constructors and Destructors in Derived Classes 212
X
6.8 Summary 219
7
Object-Oriented Programming: Polymorphism
221
7.1 Introduction 222
7.2 Derived-Class-Object to Base-Class-Object Conversion 222
7.3 Type Fields and switch Statements 230
7.4 Polymorphism Examples 231
7.5 Abstract Classes 232
7.6 Case Study: Inheriting Interface and Implementation 234
7.7 __sealed Classes and Methods 244
7.8 Case Study: Payroll System Using Polymorphism 245
7.9 Case Study: Creating and Using Interfaces 257
7.10 Delegates 271
7.11 Operator Overloading 277
7.12 Summary 282
8
Exception Handling
283
8.1 Introduction 284
8.2 Exception Handling Overview 284
8.3 Example: DivideByZeroException 288
8.4 .NET Exception Hierarchy 291
8.5 __finally Block 293
8.6 Exception Properties 301
8.7 Programmer-Defined Exception Classes 306
8.8 Summary 310
9
Graphical User Interface Concepts: Part 1
313
9.1 Introduction 314
9.2 Windows Forms 316
9.3 Event-Handling Model 317
9.3.1 Basic Event Handling 318
9.4 Control Properties and Layout 323
9.5 Labels, TextBoxes and Buttons 325
9.6 GroupBoxes and Panels 331
9.7 CheckBoxes and RadioButtons 336
9.8 PictureBoxes 350
9.9 Mouse Event Handling 354
9.10 Keyboard Event Handling 358
9.11 Summary 363
10
Graphical User Interface Concepts: Part 2
365
10.1 Introduction 366
10.2 Menus 366
10.3 LinkLabels 379
10.4 ListBoxes and CheckedListBoxes 385
XI 10.4.2 CheckedListBoxes 392 10.5 ComboBoxes 396 10.6 TreeViews 402 10.7 ListViews 409 10.8 Tab Control 417
10.9 Multiple-Document-Interface (MDI) Windows 427
10.10 Visual Inheritance 438
10.11 User-Defined Controls 444
10.12 Summary 449
11
Multithreading
451
11.1 Introduction 452
11.2 Thread States: Life Cycle of a Thread 453
11.3 Thread Priorities and Thread Scheduling 456
11.4 Thread Synchronization and Class Monitor 461
11.5 Producer/Consumer Relationship without Thread Synchronization 463 11.6 Producer/Consumer Relationship with Thread Synchronization 471
11.7 Producer/Consumer Relationship: Circular Buffer 480
11.8 Summary 493
12
Strings, Characters and Regular Expressions
495
12.1 Introduction 496
12.2 Fundamentals of Characters and Strings 496
12.3 String Constructors 498
12.4 StringChars Property, Length Property and CopyTo Method 499
12.5 Comparing Strings 501
12.6 String Method GetHashCode 505
12.7 Locating Characters and Substrings in Strings 506
12.8 Extracting Substrings from Strings 510
12.9 Concatenating Strings 511
12.10 Miscellaneous String Methods 512
12.11 Class StringBuilder 514
12.12 StringBuilderLength and Capacity Properties, and EnsureCapacity Method516
12.13 StringBuilderAppend and AppendFormat Methods 518
12.14 StringBuilderInsert, Remove and Replace Methods 521
12.15 Char Methods 524
12.16 Card Shuffling and Dealing Simulation 529
12.17 Regular Expressions and Class Regex 536
12.18 Summary 551
13
Graphics and Multimedia
553
13.1 Introduction 554
13.2 Graphics Contexts and Graphics Objects 556
13.3 Color Control 558
XII
13.5 Drawing Lines, Rectangles and Ovals 577
13.6 Drawing Arcs 581
13.7 Drawing Polygons and Polylines 585
13.8 Advanced Graphics Capabilities 592
13.9 Introduction to Multimedia 599
13.10 Loading, Displaying and Scaling Images 600
13.11 Animating a Series of Images 604
13.12 Windows Media Player 621
13.13 Microsoft Agent 627
13.14 Summary 646
14
Files and Streams
647
14.1 Introduction 648
14.2 Data Hierarchy 648
14.3 Files and Streams 650
14.4 Classes File and Directory 652
14.5 Creating a Sequential-Access File 666
14.6 Reading Data from a Sequential-Access File 685
14.7 Random-Access Files 700
14.8 Creating a Random-Access File 705
14.9 Writing Data “Randomly” to a Random-Access File 709
14.10 Reading Data Sequentially from a Random-Access File 716
14.11 Case Study: A Transaction-Processing Program 723
14.12 Summary 751
15
Extensible Markup Language (XML)
753
15.1 Introduction 754
15.2 XML Documents 754
15.3 XML Namespaces 759
15.4 Document Object Model (DOM) 762
15.5 Document Type Definitions (DTDs), Schemas and Validation 788
15.5.1 Document Type Definitions 789
15.5.2 Microsoft XML Schemas 794
15.5.3 W3C XML Schema 796
15.5.4 Schema Validation in Visual C++ .NET 798
15.6 Extensible Stylesheet Language and XslTransform 804
15.7 Summary 813
15.8 Internet and Web Resources 815
16
Database, SQL and ADO .NET
817
16.1 Introduction 818
16.2 Relational Database Model 819
16.3 Relational Database Overview: Books Database 820
16.4 Structured Query Language (SQL) 827
16.4.1 Basic SELECT Query 828
XIII
16.4.3 ORDERBY Clause 831
16.4.4 Merging Data from Multiple Tables: INNERJOIN 835 16.4.5 Joining Data from Tables Authors, AuthorISBN, Titles and
Publishers837
16.4.6 INSERT Statement 840
16.4.7 UPDATE Statement 841
16.4.8 DELETE Statement 843
16.5 ADO .NET Object Model 845
16.6 Programming with ADO .NET: Extracting Information from a Database 846 16.6.1 Connecting to and Querying an Access Data Source 846
16.6.2 Querying the Books Database 850
16.7 Programming with ADO.NET: Modifying a Database 854
16.8 Reading and Writing XML Files 870
16.9 Summary 875
16.10 Internet and Web Resources 877
17
Web Services
879
17.1 Introduction 880
17.2 HTTP Request Types 881
17.3 Multi-Tier Architecture 882
17.4 Accessing Web Servers 884
17.5 Simple HTTP Transaction 884
17.6 ASP (Active Server Pages) .NET 887
17.7 .NET Web Services Basics 888
17.8 Simple Object Access Protocol (SOAP) and Web Services 895
17.9 Publishing and Consuming Web Services 896
17.10 Session Tracking 915
17.10.1 Cookies 916
17.10.2 Session Tracking with HttpSessionState 917
17.11 Session Tracking in Web Services 918
17.12 Case Study: Temperature-Information Application 935
17.13 User-Defined Types in Web Services 946
17.14 Global XML Web Services Architecture (GXA) 960
17.14.1 WS-Inspection 961
17.14.2 WS-Routing 962
17.14.3 WS-Referral 964
17.14.4 WS-Security 965
17.15 Summary 967
18
Networking: Streams-Based Sockets and Datagrams 971
18.1 Introduction 972
18.2 Establishing a Simple Server (Using Stream Sockets) 973
18.3 Establishing a Simple Client (Using Stream Sockets) 975
18.4 Client/Server Interaction with Stream-Socket Connections 976 18.5 Connectionless Client/Server Interaction with Datagrams 989 18.6 Client/Server Tic-Tac-Toe Using a Multithreaded Server 997
XIV
18.7 Summary 1017
19
Data Structures and Collections
1019
19.1 Introduction 1020 19.2 Self-Referential Classes 1020 19.3 Linked Lists 1022 19.4 Stacks 1036 19.5 Queues 1042 19.6 Trees 1045
19.6.1 Binary Search Tree of Integer Values 1046
19.6.2 Binary Search Tree of IComparable Objects 1054
19.7 Collection Classes 1063 19.7.1 Class Array 1063 19.7.2 Class ArrayList 1067 19.7.3 Class Stack 1077 19.7.4 Class Hashtable 1083 19.8 Summary 1093
20
Accessibility
1095
20.1 Introduction 109620.2 Regulations and Resources 1097
20.3 Web Accessibility Initiative 1099
20.4 Providing Alternatives for Images 1100
20.5 Maximizing Readability by Focusing on Structure 1101
20.6 Accessibility in Visual Studio .NET 1102
20.6.1 Enlarging Toolbar Icons 1102
20.6.2 Enlarging the Text 1103
20.6.3 Modifying the Keyboard 1105
20.6.4 Rearranging Windows 1106
20.7 Accessibility in Visual C++ .NET 1108
20.8 Accessibility in XHTML Tables 1117
20.9 Accessibility in XHTML Frames 1121
20.10 Accessibility in XML 1122
20.11 Using Voice Synthesis and Recognition with VoiceXML™ 1122
20.12 CallXML™ 1129
20.13 JAWS® for Windows 1136
20.14 Other Accessibility Tools 1136
20.15 Accessibility in Microsoft® Windows® XP 1138
20.15.1 Tools for People with Visual Impairments 1140
20.15.2 Tools for People with Hearing Impairments 1142
20.15.3 Tools for Users Who Have Difficulty Using the Keyboard and Mouse 1143
20.15.4 Microsoft Narrator 1149
20.15.5 Microsoft On-Screen Keyboard 1151
20.15.6 Accessibility Features in Microsoft Internet Explorer 6 1152
XV
20.17 Internet and Web Resources 1156
21
Introduction to Unmanaged Code in Visual C++ .NET 1159
21.1 Introduction 1160
21.2 Unmanaged Code 1160
21.2.1 Choosing Between Unmanaged and Managed Code 1161
21.2.2 Future of Unmanaged Code 1161
21.3 Changes in Visual C++ .NET 1162
21.3.1 Unified Event Model 1162
21.3.2 Changes to Object-Oriented Capabilities 1169
21.3.3 Minor Modifications and Changes to Unmanaged Code 1177
21.4 Additions to Visual C++ .NET 1179
21.4.1 Runtime checks 1179
21.4.2 Attributed Programming 1185
21.4.3 ATL Server 1185
21.4.4 .NET Interoperability 1185
21.5 Summary 1185
22
Attributed Programming in ATL/COM
1189
22.1 Introduction 1190
22.2 Attributes, COM and ATL 1190
22.2.1 Introduction to COM 1190
22.2.2 Introduction to ATL 1192
22.2.3 Simplifying ATL with Attributes 1193
22.3 Creating an Attributed COM DLL 1195
22.4 COM Event-Handling Attributes 1212
22.5 Case Study: BookContentsScroller ATL ActiveX Control 1221
22.6 Summary 1248
22.7 Internet and Web Resources 1249
23
ATL Server Web Applications
1251
23.1 Introduction 1252
23.2 ATL Server Architecture 1253
23.3 Creating and Running a Simple ATL Server Example 1253
23.4 AdRotator Example 1264
23.5 Session Tracking 1268
23.5.1 Cookies 1268
23.5.2 Session Tracking with a Memory-Backed Session State 1277
23.6 Case Study: Online Guest Book 1287
23.7 Case Study: Connecting to a Database in ATL Server 1294
23.8 ATL Server Web Services 1309
23.8.1 Introduction to ATL Server Web-Services Architecture 1309
23.8.2 Creating an ATL Server Web Service 1310
23.8.3 Case Study: HugeIntegerService Web Service 1314
23.8.4 Case Study: Unmanaged Web-Service Client 1323
XVI
24
Managed and Unmanaged Interoperability
1333
24.1 Introduction 1334
24.2 It Just Works (IJW) 1334
24.2.1 Managed vs. Unmanaged Code Revisited 1335
24.2.2 Type Marshaling 1342
24.3 Managed and Unmanaged Memory 1347
24.4 Wrappers 1356
24.4.1 Wrapping Managed Objects 1356
24.4.2 Wrapping Unmanaged Objects 1359
24.5 Platform Invoke 1365
24.5.1 DllImport Attribute 1366
24.5.2 Callbacks and Structures 1373
24.6 Summary 1381
25
COM Interoperability Services
1383
25.1 Introduction 1384
25.2 COM as .NET Components 1384
25.2.1 Type Libraries and Assemblies 1385
25.2.2 The Runtime Callable Wrapper 1389
25.2.3 Early Binding 1390
25.2.4 Late Binding 1392
25.3 Handling COM Connection Points 1394
25.4 ActiveX Controls in Windows Forms 1400
25.5 .NET Components as COM 1406
25.5.1 COM Callable Wrapper 1406
25.5.2 Type-Library Generation and Registration 1407
25.5.3 Case Study: Using ManagedContentsScroller as COM 1409
25.6 Summary 1425
A
Operator Precedence Chart
1427
B
Number Systems
1431
B.1 Introduction 1432
B.2 Abbreviating Binary Numbers as Octal Numbers and Hexadecimal Numbers 1435 B.3 Converting Octal Numbers and Hexadecimal Numbers to Binary Numbers 1437 B.4 Converting from Binary, Octal or Hexadecimal to Decimal 1437 B.5 Converting from Decimal to Binary, Octal or Hexadecimal 1438
B.6 Negative Binary Numbers: Twos-Complement Notation 1439
B.7 Summary 1440
C
ASCII Character Set
1443
D
Unicode
® 1445D.1 Introduction 1446
XVII
D.3 Characters and Glyphs 1448
D.4 Advantages and Disadvantages of Unicode 1449
D.5 Unicode Consortium’s Web Site 1449
D.6 Using Unicode 1450 D.7 Character Ranges 1455 D.8 Summary 1456
E
Introduction to XHTML: Part 1
1459
E.1 Introduction 1460 E.2 Editing XHTML 1460E.3 First XHTML Example 1461
E.4 W3C XHTML Validation Service 1464
E.5 Headers 1465
E.6 Linking 1467
E.7 Images 1471
E.8 Special Characters and More Line Breaks 1475
E.9 Unordered Lists 1477
E.10 Nested and Ordered Lists 1478
E.11 Summary 1481
E.12 Internet and Web Resources 1482
F
Introduction to XHTML: Part 2
1485
F.1 Introduction 1486
F.2 Basic XHTML Tables 1486
F.3 Intermediate XHTML Tables and Formatting 1489
F.4 Basic XHTML Forms 1491
F.5 More Complex XHTML Forms 1494
F.6 Internal Linking 1502
F.7 Creating and Using Image Maps 1505
F.8 meta Elements 1507
F.9 frameset Element 1508
F.10 Nested framesets 1512
F.11 Summary 1514
F.12 Internet and Web Resources 1516
G
XHTML Special Characters
1517
H
XHTML Colors
1519
I
Bit Manipulation
1523
I.1 Introduction 1524
I.2 Bit Manipulation and the Bitwise Operators 1524
I.3 Class BitArray 1544