About the Book
Go beyond knowing how Visual Basic works to
writing professional-level Microsoft .NET code.
Most books about Visual Basic® use
abstract snippets of code to illustrate the language’s syntax, data structures,
and controls. But even if you know the language, it’s sometimes difficult
to see how to put these elements together to write a complete program.
This practical handbook of software construction covers the vital details
about the latest version—Visual Basic .NET, with its integrated development
environment (IDE), complete support for XML, and ASP.NET Web-development
functionality, including Web Forms and Web Services. Whether you’re a beginner
or a self-taught programmer, a professional looking for a refresher in
coding techniques, or a programmer coming from another language, this is
the Visual Basic book for you. Expert instruction includes these topics:
• Visual Basic .NET overview, including
the Visual Studio® .NET IDE
• Object-oriented programming in Visual
Basic .NET
• Creating and deriving classes
• Data types and features
• Managing arrays and collections
• Handling errors and debugging programs
• Building and deploying assemblies
• File-system monitoring
• Data access and data binding with ADO.NET
• ASP.NET and XML Web services
Related Books
Microsoft® Visual Basic® Professional
6.0 Step by Step
Programming Microsoft® Visual Basic®
6.0
Developing Microsoft® .NET Controls
with Microsoft Visual Basic® .NET
Table of Contents
Acknowledgments
xv
Introduction xvii
1 Visual Basic .NET from the Ground
Up 1
What a Long, Strange Trip
It's Been 2
From
COM to .NET 4
The .NET
World 6
Why You Need to Learn Visual
Basic .NET 8
What Are the Pieces and
How Do They Fit Together? A .NET Framework Overview 9
Web Services
11
User
Interface 11
Data
and XML 12
Base
Class Library 12
Common
Language Runtime 13
Where
Do We Start to Access Functionality from Visual Basic .NET Source Code?
15
Visual Basic .NET Is Object
Oriented 16
A Brief Look at How the
Visual Basic .NET Language Works 18
How Is a Visual Basic .NET
Program Put Together? 20
Metadata—Data
About Data 20
The Just-In-Time
Compiler 21
Execution
of Visual Basic .NET Code 22
Assemble
the Troops 23
Configuring the Interactive
Development Environment 23
A First Look at the Visual
Basic .NET IDE 25
Some
Visual Basic .NET Code 27
Files
Created by the IDE for Our First .NET Program 33
Another
Word on Assemblies 38
A Closer Look at the Code
41
You Mean
I Get an Inheritance? 41
Starting
Up Our Form1 Class 42
Warning!
Don't Fiddle with the Designer's Code 46
The Big
Event 47
Nothing but .NET 48
2 Object-Oriented Programming in Visual
Basic .NET 49
An Object Lesson 49
Starting Out with Objects
50
A Class
Is Really Only a Blueprint 50
Let's
Talk Objects 51
Our Form
as an Object 52
Reading,
Writing, Invoking 54
Inheritance 56
Understanding
Namespaces 58
Inheriting
from System.Windows.Forms.Form: Forms and Controls 62
A Word
About Visual Basic .NET Controls 63
Check
Out the Code 65
The Code
Added for the Button 67
Enough Talk: Press F5 and
Run Your Program 69
The Doppelganger
Program: Creating Clones of the Form1 Class 70
Important
Object Concepts from the Doppelganger Program 71
Using the Class View to
Spy on Structure and Access Modifiers 76
More
About Access Types 78
Overloading Methods 79
Some
of the Overloaded Show Methods 81
Polymorphism 83
What's Controlling Our Form
When We Run It? 84
Try This
Out 84
Your First Real Visual Basic
.NET Program 86
Telling
the Application Object Which Form to Run 88
Let's
Add Some Controls 90
Examining
the Handiwork of the IDE-Generated Code 94
How Do
We Hardwire the Controls? 98
Can You
Name That Namespace? 98
Date and Time Arithmetic
99
Formatting the Date and
Time 101
Let's Run This Baby! 103
Conclusion 105
3 Writing Your First Class
109
Creating the Employee Class
110
Examining
the Class Code 113
Our Class's
Namespace 118
Declaring
Our Class 118
Using
Shared Variables 120
Class
Constructors 120
Overloading
Constructors 121
MyBase.New
122
Assigning
Values to Our Private Data Fields 123
Overriding
124
#Region
126
The Employee Class Properties
127
More About Inheritance
130
Virtual
Methods 134
Synchronizing the Class
View 134
Creating Instances of the
Employee Class 136
Conclusion 140
4 Visual Basic .NET Data Types and
Features 143
Getting to Know Data Types
143
Visual Basic .NET Data Types
144
Value
Types 145
Reference
Types 147
Data Type Features 148
The System.Object
Class 149
Strong
Typing 152
Type
Safety 152
Data
Widening 157
Garbage Collection: Getting
Rid of Our Objects 160
The Stack
and the Managed Heap 160
Conclusion 161
5 Examining the .NET Class Framework
Using Files and Strings 163
What Exactly Is the .NET
Framework? 164
Tapping
into the .NET Framework 165
It All
Starts with the System Namespace 165
Learning to Find and Use
What You Need 169
Searching
in Windows Class Viewer 170
Using
the Namespaces 171
Examining the File Class
171
Streams 173
What's
the Difference Between a File and a Stream? 174
Reading
and Writing Binary, Numeric, or Text Data 174
Using the File and StreamWriter
Classes in the .NET Framework 175
Reading
Our File 176
The FileInfo
Class 177
Creating
a New File 179
Enumerating
Directory Entries Using the Framework 180
Let's Talk Strings
183
What's
New in Strings? 184
Uninitialized
Strings 184
Working
with Strings 185
Copying
and Cloning a String 187
Conclusion 190
6 Arrays and Collections in Visual
Basic .NET 191
Building Your First Visual
Basic .NET Array 192
Array
Boundaries 194
Why Arrays
Are Based on the System.Array Class 198
What
If I Don't Know How Many Elements I Need Ahead of Time? 202
Arrays
Start at Zero in Visual Basic .NET 204
Initializing
the Array During Declaration 204
Arrays
Are Reference Types 205
Arrays in Action: A Roman
Numeral Calculator 207
Writing
the Code 208
Examining
the Code 209
Caching
Our Variables 210
Visual Basic .NET Collections
212
The ArrayList
Collection 213
Queues
216
Stacks
218
Eliza and the Beginning
of Artificial Intelligence 219
Eliza
in Action 220
Coding
Eliza 222
Topology
of Our Dialog.vb Code Module 224
Writing
the Dialog.vb Code Module 226
Examining
Our Code 234
Arrays
vs. Collections 235
The Entry
Point for Eliza 236
Is the
Patient Discussing the Good Doctor? 240
Can Eliza
Return a Quick Response? 241
Can Eliza
Translate the Patient's Response to Make It a Question? 243
Return
a Previous Patient Phrase 247
When
All Else Fails 250
Calling
the Module from the Form 251
Conclusion 253
7 Handling Errors and Debugging Programs
255
What Can Possibly Go Wrong?
255
Types of Visual Basic .NET
Errors 258
The Classic Visual Basic
Err Object Is Gone in Visual Basic .NET 259
Try, Catch, and Finally
259
Adding
Structured Error Handling 261
The Try.Catch
Block 262
Making
Our Simple Program Even More Bullet Proof 264
The Finally
Block 266
Setting a Breakpoint in
Your Code 267
Running the Program Using
the Debugger 268
Stepping
Through Our Code 270
Helpful
Debugging Windows 271
The Call Stack 276
The Debug and Trace Classes
278
Debug.WriteLine
278
Debug.Assert
279
Tracing
281
Adding
a Tracing Class to Our Code 282
Examining
the ErrorTrace.vb Code 284
Setting
the Trace Level 288
Adding
the Errors.vb Class to a Program 289
Adding Event Logging to
Your Programs 293
The Philosophy
of Logging Events to the Event Viewer 295
Adding
Event Logging to the ErrorTrace.vb Class 296
Using
Our New Event Logging Capability 300
Conclusion 301
8 Assemblies in Detail 303
The Right to Assemble
303
Private
Assemblies 304
Shared
Assemblies 305
The Other
Parts of an Assembly 308
Reflection: How to Go About
Examining Assemblies 309
The Assembly Spy Program
310
Building
the Assembly Spy Program 313
Let's Write Some Code
315
Examining
the Code 320
Self-Examination: Contemplating
Our Own Assembly 329
Code
Signing 330
Creating
a Strongly Named Assembly 330
The Global
Assembly Cache Revisited 333
Assembly
Versioning 335
New Variable Scoping in
Visual Basic .NET 338
Namespace
Scope 340
Determining
the Scope of a Variable 340
Conclusion 341
9 File System Monitoring
343
The File Sentinel Program
344
How the
File Sentinel Program Works 345
Starting
to Write the File Sentinel Program 346
Adding
the Sentinel Class to Our Program 349
Delegates
356
Handling
the Changed, Created, and Deleted Events 358
Handling
the Renamed and Error Events 360
Writing
to Our Log File 361
Wiring
Up the User Interface 362
Possible
Enhancements to the File Sentinel 370
Introduction to Windows
Services 372
The Life
and Death of a Service 372
Building
Our File Sentinel into a Windows Service 373
Adding
Our Sentinel Class to Our Service 374
Updating
the Service1.vb File 375
How Our
Service Works 377
Looking
at vbMonitorService in the Services Window 387
Debugging
a Windows Service 389
Conclusion 392
10 Data Access with ADO.NET
393
From ADO to ADO.NET
393
ADO.NET
from 50,000 Feet 394
Individual
Tables, Not the Join, Are in a DataSet 395
Comparing
Classic ADO and ADO.NET 397
A Closer Look at the Foundation
of ADO.NET: The DataSet Object 399
The DataTable
Object 400
The DataSet
Object and XML 401
DataView
Objects 402
Managed Providers in ADO.NET
402
A Common
Provider Model 404
Enough Talk, Let's Look
at Some Code 405
Connecting
to Our Data Source 406
Commands
to Manipulate Data from the Data Source 406
Creating
the DataReader Object 407
Putting
the Pieces of Our DataReader Together 408
Writing a Simple SQLClient
Class DataSet Program 410
Getting
Started 410
Adding
a DataAdapter Object to Our Program 414
Finishing
the User Interface 418
A Sneak
Preview of Our Data from the DataAdapter 419
XML Schema
for the Customers Table 421
Just
Add Code 422
Running
Our Program 423
Editing
Our Data 424
How the
Code Works 424
Updating
the Data Source 426
Conclusion 429
11 Data Sets in Detail
431
Looking Again at the ADO.NET
Object Model 431
Data Sets and XML
434
Building the Data Set and
XML Viewer Project 436
Adding
the Connection, Data Adapter, and DataSet Objects 438
Adding
Code to Our Program 439
How It
Works 440
Generating
XML from Our Data Set 441
Updating
the Data Source 441
ADO.NET and XML 443
Examining
Our Program's XML Output 443
The XML
Schema Output 444
Persisting Our XML Information
446
Testing
Our Persistence Code 447
Examining
the DiffGram 449
Leveraging Our XML File
for New Classes 452
The Xsd.exe
Program 452
Adding a Relationship to
Our Program 455
The Data
Sets and XML Program 456
Creating the Parent/Child
Relationship 457
Adding
a Relationship to Our Tables 458
Examining
DataSet Properties 461
Populating a Data Grid from
a Persisted XML File 462
Run the
Program 463
How the
Program Works 464
Hand Coding a Simple Program
465
Data Binding 467
Creating
the Program 467
Adding
the Code That Wires the Controls to the Data Set 469
Run the
Program 470
How It
Works 471
Updating
Our Data Grid 476
Conclusion 476
12 ADO.NET Data Binding
477
The BindingContext Object
477
The CurrencyManager Object
479
Record
Navigation 479
A Simple Example 479
Add the
Code 481
How the
Code Works 484
The DataTable, DataRow,
and DataColumn Objects 488
Examining
the DataTable Schema 488
Building
a Table Programmatically 490
How the
Code Works 491
Finding
Specific Records 493
Conclusion 495
13 ASP.NET and Web Services
497
A Look Back at ASP
497
Why ASP.NET? 498
Our First Web Form
500
New Server
Controls 503
The HTML
Presentation Template 505
Viewing
the Code-Behind File 508
Setting
the Properties on Our Web Page 509
Adding
the Calendar Control Code 510
Running
the Web Form 511
Examining
the HTML Sent to the Browser 512
Building a Loan Payment
Calculator 514
Building
Our Loan Application Project 517
Adding
Code to the Code-Behind Form 519
The Life
of a Web Form 520
How Our
Program Works 521
Taking
a Closer Look at Our Drop-Down List 523
Adding
the Payment Schedule Page 524
Adding
Our Class Code 526
How the
Calculator Works 529
Tracing
Our Program 532
Web Services: The New Marketplace
533
What
Are Web Services? 533
OK, Now
How Do We Communicate? 534
Finding
Out Who Is Offering What in the Global Marketplace 535
Where
Are Web Services Going? 537
Building a Web Service
538
Run the
Program 540
Consuming
the MagicEightBall Web Service 543
Building
Our Web Services Client Program 545
Adding
a Proxy Class to Our Program 546
Adding
Code to get Our Magic Eight Ball Answers 547
Conclusion 548
14 Visual Inheritance and Custom Controls
549
Visual Inheritance 549
Building
a Base Form 550
Adding
the Inherited Form 552
Creating a Custom Control
555
Changing
the Background Color of a Text Box 555
Building
Our Control 556
Adding
Code to Our Control 556
Adding
Our Custom Control to the Host Form 558
How it
Works 559
Putting it Together: What
We've Learned So Far 561
How Do
We Save the Notes? XML, Of Course 562
Building
the Sticky Notes Progam 564
Constructing
a Sticky Note 570
Adding
Code to the Sticky Note 571
How Does
it Work? 574
Adding
Even Handler Delegates 575
The serialize
Class in More Detail 577
When
the User Quits the Sticky Notes Program 582
How the
BaseNote Sticky Yellow Form Works 585
Deploying
Our Sticky Notes Program 588
Installing
Our Program on a Client Machine 592
Install
the Sticky Notes Program 593
Conclusion 594
APPENDIX: Some Helpful
ADO.NET Wizards 595
Using the Data Form Wizard
595
Run the
Program 600
Under
the Hood 601
Generating a Crystal Report
from a Data Source 603
Building
a Crystal Report 603
Getting
Ready to View Our Report 609
INDEX 613 |