0% found this document useful (0 votes)
45 views

Programming Microsoft Visual Basic NET Version 2003 Francesco Balena instant download

The document provides information about the book 'Programming Microsoft Visual Basic .NET Version 2003' by Francesco Balena, including its ISBN, publication details, and a brief overview of its contents. It also includes links to download the book and other related resources. The book covers fundamental concepts of Microsoft Visual Basic .NET and is published by Microsoft Press.

Uploaded by

cladconatsev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Programming Microsoft Visual Basic NET Version 2003 Francesco Balena instant download

The document provides information about the book 'Programming Microsoft Visual Basic .NET Version 2003' by Francesco Balena, including its ISBN, publication details, and a brief overview of its contents. It also includes links to download the book and other related resources. The book covers fundamental concepts of Microsoft Visual Basic .NET and is published by Microsoft Press.

Uploaded by

cladconatsev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Programming Microsoft Visual Basic NET Version

2003 Francesco Balena download

https://ebookultra.com/download/programming-microsoft-visual-
basic-net-version-2003-francesco-balena/

Explore and download more ebooks or textbooks


at ebookultra.com
Here are some recommended products for you. Click the link to
download, or explore more at ebookultra.com

Programming Microsoft SQL Server 2000 with Microsoft


Visual Basic NET 1st edition Edition Rick Dobson

https://ebookultra.com/download/programming-microsoft-sql-
server-2000-with-microsoft-visual-basic-net-1st-edition-edition-rick-
dobson/

Sams Teach Yourself Microsoft Visual Basic NET 2003 in 21


Days 2nd Edition Steven Holzner

https://ebookultra.com/download/sams-teach-yourself-microsoft-visual-
basic-net-2003-in-21-days-2nd-edition-steven-holzner/

Sams Teach Yourself Microsoft Visual Basic NET 2003 in 24


Hours Complete Starter Kit 2nd Edition James Foxall

https://ebookultra.com/download/sams-teach-yourself-microsoft-visual-
basic-net-2003-in-24-hours-complete-starter-kit-2nd-edition-james-
foxall/

Programming with Microsoft Visual Basic 2012 6th Edition


Diane Zak

https://ebookultra.com/download/programming-with-microsoft-visual-
basic-2012-6th-edition-diane-zak/
Programming the Web with Visual Basic NET 1st Edition Lynn
Torkelson

https://ebookultra.com/download/programming-the-web-with-visual-basic-
net-1st-edition-lynn-torkelson/

Practical Database Programming with Visual Basic NET


Second Edition Ying Bai(Auth.)

https://ebookultra.com/download/practical-database-programming-with-
visual-basic-net-second-edition-ying-baiauth/

An Introduction to Object Oriented Programming with Visual


Basic NET 1st Edition Dan Clark

https://ebookultra.com/download/an-introduction-to-object-oriented-
programming-with-visual-basic-net-1st-edition-dan-clark/

Mastering Microsoft Visual Basic 2008 Evangelos Petroutsos

https://ebookultra.com/download/mastering-microsoft-visual-
basic-2008-evangelos-petroutsos/

Microsoft Visual Basic Programs to Accompany Programming


Logic and Design 3rd Edition Jo Ann Smith

https://ebookultra.com/download/microsoft-visual-basic-programs-to-
accompany-programming-logic-and-design-3rd-edition-jo-ann-smith/
Programming Microsoft Visual Basic NET Version 2003
Francesco Balena Digital Instant Download
Author(s): Francesco Balena
ISBN(s): 9780735620599, 0735620598
Edition: Hardcover/CD-ROM combo
File Details: PDF, 10.99 MB
Year: 2004
Language: english
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2004 by Francesco Balena
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by
any means without the written permission of the publisher.
Library of Congress Cataloging-in-Publication Data
Balena, Francesco, 1960-
Programming Microsoft Visual Basic .NET version 2003 / Francesco Balena.
p. cm.
Includes index.
ISBN 0-7356-2059-8
1. Microsoft Visual BASIC. 2. BASIC (Computer program lanuage) 3. Microsoft
.NET. I. Title.

QA76.73.B3B348 2003
005.2'768--dc22 2003058666
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 8 7 6 5 4 3
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further information
about international editions, contact your local Microsoft Corporation office or contact Microsoft Press
International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments
to mspinput@microsoft.com.
Active Directory, ActiveX, Authenticode, ClearType, DirectX, IntelliMouse, IntelliSense, JScript,
Microsoft, Microsoft Press, MS-DOS, MSDN, Outlook, PowerPoint, Visual Basic, Visual C++, Visual
C#, Visual J#, Visual Studio, Win32, Windows, and Windows NT are either registered trademarks or
trademarks of Microsoft Corporation in the United States and/or other countries. Other product and
company names mentioned herein may be the trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places,
and events depicted herein are fictitious. No association with any real company, organization, product,
domain name, e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided
without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its
resellers or distributors will be held liable for any damages caused or alleged to be caused either directly
or indirectly by this book.
Acquisitions Editor: Danielle Bird Voeller
Project Editor: Kathleen Atkins
Technical Editor: Jeff Webb
Indexer: Virginia Bess
Body Part No. X10-21789
To Adriana, the love of my life
Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

Part I The Basics


1 Introducing Microsoft .NET 2
.NET as a Better Programming Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Leveling Windows Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
.NET as a Better COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
All .NET Languages Are Born Equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Web Forms, the Successor to Active Server Pages. . . . . . . . . . . . . . . . . . . . . 5
Web Services, the Programmable Internet . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Microsoft .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
.NET Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
.NET Languages and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Just-in-Time (JIT) Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Working with Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The .NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2 Modules and Variables 25


Modules and Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Assignments and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

3 Control Flow and Error Handling 65


Execution Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Conditional and Loop Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

v
vi Contents

Commands, Functions, and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74


String Constants and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Interaction Commands and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Other Commands, Functions, and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . 78
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
The Exception Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The Try…Catch…Finally Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The Throw Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Custom Exception Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Exceptions in Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Performance Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The Trace and Debug Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Part II Object-Oriented Programming


4 Class Fundamentals 98
Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Overloading and Coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Ambiguous Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Read-Only and Write-Only Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Properties with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Default Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Constructors with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Overloaded Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Constructors and Read-Only Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Declaring and Raising an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Trapping Events with WithEvents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Trapping Events with AddHandler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Module Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Trapping Events from Arrays and Collections . . . . . . . . . . . . . . . . . . . . . . . 120
Guidelines for Event Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Shared Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Shared Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Contents vii

Shared Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124


Shared Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

5 Inheritance 129
Inheritance Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Extending the Derived Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Using the Derived Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Polymorphic Behavior. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Overriding Members in the Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Override Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
The MyBase Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Constructors in Derived Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
The MyClass Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Member Shadowing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Redefining Shared Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Sealed and Virtual Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
The NotInheritable Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
The MustInherit Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
The MustOverride Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Public, Private, and Friend Scope Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . 149
The Protected Scope Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
The Protected Friend Scope Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Using Scope Qualifiers with Constructors . . . . . . . . . . . . . . . . . . . . . . . . . 151
Redefining Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Object Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
COM and the Reference Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
The Finalize Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
The Dispose Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Weak Object References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

6 Interfaces, Delegates, and Attributes 171


Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Implementing the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Accessing the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Interfaces and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Using .NET Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The IComparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The IComparer Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
The ICloneable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
viii Contents

Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Invoking Static Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Invoking Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Other Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Callback Procedures and Code Reuse. . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Multicast Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Delegates and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Attribute Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
The Conditional Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
The Obsolete Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The DebuggerStepThrough Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

Part III Programming the .NET Framework


7 .NET Framework Basic Types 200
The System.Object Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Public and Protected Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Value Types and Reference Types (Revisited) . . . . . . . . . . . . . . . . . . . . . . . 203
Boxing and Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
The String Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
String Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Shared Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
The CultureInfo Auxiliary Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The Encoding Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Formatting Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Formatting Date Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
The Char Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
The StringBuilder Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Parsing Strings into Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The Convert Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Random Number Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
The DateTime Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Adding and Subtracting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Formatting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Parsing Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Working with Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
The Guid Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Contents ix

Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Displaying and Parsing Enum Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Other Enum Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Bit-Coded Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

8 Arrays, Lists, and Collections 239


The Array Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Sorting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Clearing, Copying, and Moving Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Searching Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The System.Collections Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The ICollection, IList, and IDictionary Interfaces. . . . . . . . . . . . . . . . . . . . . 248
The BitArray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
The BitVector32 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
The Queue Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
The Hashtable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
The SortedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Custom Collection and Dictionary Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
The CollectionBase Abstract Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
The ReadOnlyCollectionBase Abstract Class . . . . . . . . . . . . . . . . . . . . . . . 268
The DictionaryBase Abstract Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
The NameObjectCollectionBase Abstract Class . . . . . . . . . . . . . . . . . . . . . 270
The IEnumerable and IEnumerator Interfaces . . . . . . . . . . . . . . . . . . . . . . 271

9 Files, Directories, and Streams 275


The Directory and File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
The DirectoryInfo and FileInfo Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
The Path Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
The Stream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Stream Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Stream Readers and Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Reading and Writing Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Reading and Writing Memory Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Reading and Writing Strings in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

10 Object Serialization 293


Basic Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Binary Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
x Contents

SOAP Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295


The Serializable and NonSerialized Attributes . . . . . . . . . . . . . . . . . . . . . . 296
Object Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Deep Object Cloning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Custom Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The IDeserializationCallback Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The ISerializable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
A Custom Serialization Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
The FormatterServices Helper Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

11 Regular Expressions 307


Regular Expression Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
The Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
The Regular Expression Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Regular Expression Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Regular Expression Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
The Regex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
The MatchCollection and Match Classes . . . . . . . . . . . . . . . . . . . . . . . . . 322
The Group Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

12 Threading 326
Threading Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
When to Use Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Creating Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Working with Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Thread Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Debugging Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Threads and Unhandled Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Storing and Sharing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Local, Class, and Static Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
The ThreadStatic Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Passing Data to and from a Thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Thread Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The SyncLock Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Synchronized Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The Monitor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
The Interlocked Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
The Mutex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
The ReaderWriterLock Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
The ManualResetEvent and AutoResetEvent Classes . . . . . . . . . . . . . . . . . 350
Using the Thread Pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
The ThreadPool Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Contents xi

The Timer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353


Asynchronous Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Asynchronous Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

13 Components and Assemblies 360


Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Creating a .NET Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Creating Hostable Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Working with Multiple-Project Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
The Configuration Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Private and Shared Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Strong Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Installing in the GAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Using the DEVPATH Environment Variable . . . . . . . . . . . . . . . . . . . . . . . . . 373
The Binding Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Version Policy in Application Configuration Files . . . . . . . . . . . . . . . . . . . . . 374
Previously Loaded Assemblies and GAC Searches . . . . . . . . . . . . . . . . . . . 376
Codebase Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Probing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
The Assembly Binding Log Viewer Utility (FUSLOGVW) . . . . . . . . . . . . . . . . 379
Configuration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Setting the Runtime Version. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Dynamic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
The .NET Framework Configuration Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Assembly Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

14 Reflection 390
Working with Assemblies and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
The Assembly Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
The AssemblyName Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
The Module Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Working with Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Getting a Type Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Retrieving Type Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Enumerating Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Retrieving Member Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Enumerating Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Invoking Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Creating an Object Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Dynamic Registration of Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . 406
xii Contents

Tracing the Stack Frame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407


Attributes and Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Building a Custom Attribute Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Discovering Attributes at Run Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
On-the-Fly Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

Part IV Win32 Applications


15 Windows Forms Applications 418
Form Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
The Form Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
The Windows Forms Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Common Tasks for Forms and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
The Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Common Form Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
MDI Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Form Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Advantages of Form Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
An Example of Form Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Trapping and Overriding Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Advanced Form Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Window Subclassing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Visual Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Localized Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Dynamic Control Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Form as Child Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

16 Windows Forms Controls 486


Overview of Windows Forms Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
The TextBox Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
The Label Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
The LinkLabel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
The CheckBox, RadioButton, and Button Controls . . . . . . . . . . . . . . . . . . . 489
The ListBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
The CheckedListBox Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
The ComboBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Provider Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
The Splitter Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Common Dialog Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
The ImageList Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
The TreeView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Contents xiii

The ListView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512


The PropertyGrid Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Other Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Binding to an Array of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
ADO.NET Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
The DataGrid Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Other Useful Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
The Clipboard Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
The Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
The Cursor Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
The SendKeys Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
The Help Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

17 GDI+ 551
2-D Vector Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
The Graphics Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Lines, Rectangles, Polygons, Ellipses, and Arcs. . . . . . . . . . . . . . . . . . . . . 553
Cardinal and Bézier Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
The Pen Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Filled Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Brush Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Alpha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Imaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Loading and Saving Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Displaying an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Flipping, Rotating, and Skewing an Image . . . . . . . . . . . . . . . . . . . . . . . . . 573
Transparent and Semitransparent Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . 577
Icons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Metafiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Typography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Font Families . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Aligned Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Textual Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Anti-Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
xiv Contents

18 Custom Windows Forms Controls 589


Inheriting from an Existing Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Creating the Control Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Creating the Client Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Adding the Validation Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Testing the Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Design-Time Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Improving the Custom Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Working with Other Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Adding Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Working with Icons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Working with Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Creating Composed Multiple Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Creating the UserControl Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Adding Properties, Methods, and Events . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Shadowing and Overriding UserControl Properties . . . . . . . . . . . . . . . . . . . 606
Adding Resize Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Creating a Control from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
The ControlPaint Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
The ISupportInitialize Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Multithreaded Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Extender Provider Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Custom Property Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Custom Control Designers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Data-Binding Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Design-Time and Run-Time Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Hosting Custom Controls in Internet Explorer . . . . . . . . . . . . . . . . . . . . . . 638

19 Advanced Win32 Techniques 641


The Environment Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
The Registry and RegistryKey Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Reading Registry Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
The SystemEvents Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
The FileSystemWatcher Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Initializing a FileSystemWatcher Component . . . . . . . . . . . . . . . . . . . . . . . 648
Getting Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Troubleshooting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
The Process Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
Contents xv

Running and Stopping a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653


Querying a Process for Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Waiting for a Process to End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
The PerformanceCounter Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Introduction to Performance Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Reading Performance Counter Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Working with Custom Performance Counters . . . . . . . . . . . . . . . . . . . . . . . 670
The EventLog Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Introduction to Event Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Reading Event Log Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
Writing Event Log Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679

20 Windows Services 683


Windows Service Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Creating the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Setting Service Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Browsing and Fixing the Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
Overriding ServiceBase Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Implementing the Encryption Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Installing the Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Setting the Service’s Main Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Setting the Service’s Security Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Running InstallUtil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Starting and Stopping the Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
More Service Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Custom Event Log Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Reacting to Shutdown and Power Events . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Managing Custom Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Passing and Receiving Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Debugging a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
The ServiceController Component. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Listing Installed Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Querying a ServiceController Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Managing a Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698

Part V Database Applications


21 ADO.NET in Connected Mode 702
The Transition to ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Introducing ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
.NET Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
The ADO.NET Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Random documents with unrelated
content Scribd suggests to you:
Savignone branch, received some feuds as a reward for not having
shared in the conspiracy of his relatives. The castle of Castelano was
ceded to the Duke of Parma. Agostino Landi retained the burgh of
Valditaro. This Landi had promised to assassinate Pierluigi Farnese
whom Doria had condemned to death for his secret intrigues with
Gianluigi. It is worth our while to clear up the history of this part of
Andrea’s vengeance.
The cities of Parma and Piacenza, having been detached from the
duchy of Milan and put into the hands of the Holy See, were ceded
by Paul III. to his natural son Pier Luigi Farnese who had been
legitimated in 1501 by Julius II. To secure his son in this new duchy,
the Pope supported Charles in the German war and in his expedition
to Tunis, where, aided by Doria the emperor restored the inhuman
Muley-Hassan to the throne which he mounted by the assassination
of his twenty-two brothers. The alliance of Farnese with the empire
was cemented by the marriage of Pierluigi’s son, Ottavio, with
Margaret a natural daughter of Cæsar and widow of Alessandro de
Medici. Francis Sforza died and the duchy of Milan reverted to the
empire giving rise to a war with France. The Pope thought to gain
profit for Pier Luigi out of this contest for the duchy by securing him
the investiture, and Cæsar, at the conference of Busseto, promised
to grant the pontiff’s request. The emperor did not keep his pledge
and the Pope began to abandon the imperial cause. He reproached
Charles with the fact that certain prelates devoted to the empire had
proposed in the council of Trent innovations on the rights of the
Papal See, and expressed his discontent with the mild, treatment of
the partisans of Luther in Germany. He went further and began to
intrigue, in 1547, for a league with France against Charles.
Francis I. at the moment when he was most zealously engaged in
uniting England, Germany and Italy against Spain was stricken by
death at Rambouillet after a twenty years’ conflict with the
increasing power of Charles Fifth. The emperor now saw himself
without a rival and hastened to take advantage of the occasion. He
renewed hostilities against the Duke of Saxony, though his army had
been thinned by the withdrawal of the Papal troops. It is not our
purpose to recount the story of this Germanic war. Charles
conducted it to a successful termination because the affairs of Italy
no longer distracted his attention. But his victories over the league
of Smacalda increased the suspicions and fears of Paul III. who saw
that if Charles was successful in Germany he would be master at the
council of Trent. It was no secret that the emperor designed to take
that occasion for avenging himself on the Pope for sympathy with
the Fieschi and France. The Roman court was too jealous of its
prerogatives not to be alarmed at the prospect of having its power
limited by an ambitious monarch favourably disposed towards the
policy of the German reformers. It was thought necessary to remove
the seat of the council to some city nearer to Rome and more under
Papal influence, where Charles could not intrigue nor display his
arms with so much effect.
Fortune favoured the Pope. Some of the assembled prelates fell
sick and the physicians, especially Fracastoro who was employed by
Rome for the business, reported that a fierce contagion had broken
out in the city. Many of the prelates abandoned Trent in great haste
and the council was removed to Bologna. The cardinals and bishops
of the imperial faction remained in Trent by express order of Charles.
The remainder, thirty-four in number, accompanied the Papal
legates. There were mutual recriminations and the very council
assembled to destroy scism was menaced with a scism in its own
bosom.
Cæsar made angry appeals and intrigued adroitly to secure the
reassembling of the Synod in Trent. The Pope refused, and Charles
avenged himself by that decree of Interim, in which he declared that
until the council should be reconvoked in Trent every one was at
liberty to think as he pleased in matters of religion. The decree
occasioned great scandal in the church.
“It was believed,” says Varchi, “that the emperor wished to
restore the Papacy to the simplicity and poverty of times when
prelates did not meddle with temporal government but contented
themselves with their spiritual functions. The gross abuses and vile
practices of the Roman court had awakened in many an ardent
desire for such a reform.” This gave bitterness to the enmity
between the Pope and Charles. The pontiff directed his hostilities
especially against the two imperial ministers in Italy, Anotonio Leyva
and Andrea Doria. On the death of the first, the whole weight of
Papal displeasure fell on the head of the latter, who earlier in life had
received from Rome a consecrated sword and hat for his victories
over the Turks. We have elsewhere shown how the opposition of
Doria to the growth of the Farnese family and his other acts hostile
to Paul III. had led the latter to favour the Fieschi conspiracy against
Doria and Spain. Some deny that Paul favoured the conspirators and
adduced the testimony of Don Appollonio Filareto, secretary to Pier
Luigi Farnese. This secretary, though confined for three years as a
prisoner in Milan and put to torture, steadfastly denied that the
French knew of the plans of Fieschi. But this is contradicted both by
the current opinion of that time and by authentic and credible
documents extant. Charles was so certain of the complicity of the
Pope with Fieschi, that when Paul sent Camillo Orsino to Madrid to
complain to the emperor of the murder of his son Pier Luigi and ask
the restitution of Piacenza to the Apostolic See, he boldly charged
the pontiff with this crime.
As soon as Andrea learned through the ministers of Cæsar that
Paul had been concerned in the Fieschi movement, and that Pier
Luigi had given material aid to Gianluigi he was inflamed with an
ardent desire to punish old and new treacheries by a signal act of
vengeance. From that hour, Farnese was condemned to the fate of
the Fieschi. Moreover, in gratifying his own passion for revenge,
Andrea was furthering the schemes of Charles. He launched himself
into the matter with the ardour of youth.
The news that Charles was suffering from a mortal sickness filled
Doria with apprehension of wide-spread conspiracy against Spain in
case of the emperor’s death. Pier Luigi, in fact, as soon as he
received the same intelligence, began to raise troops, fortify castles
and enlist able commanders among whom were Bartolomeo
Villachiara, Sforza Santa Fiore, Sforza Pallavicino and Alessandro
Tommasoni da Terni. He collected arms everywhere. We find in old
documents that he bought at one time four thousand arquebuses,
for a gold crown each, from the celebrated Venturino del Chino,
armourer of Gordone in Valtrompia. Bonfadio tells us that these
military preparations awakened grave suspicions in the neighbouring
cities of the empire who feared that these arms were to be used
against themselves. The fear of revolution was widely diffused. Doria
could not be an idle witness of this drawing of swords in places so
near, especially after the share of Farnese in the Fieschi plot. He had
then two motives for prompt action; to secure the safety of the
empire and to avenge the blood of Gianettino.
Pier Luigi has been traduced by the malice of writers in the
Spanish interest. It is true that Cellini declares him avaricious, and
many historians affirm that he was intemperate and a votary of
licentious pleasures. Even Aretino admonished him to husband more
carefully the strength of his manhood. But the fable of Varchi that he
ravished Cosimo Gheri, bishop of Fano, though repeated in our days
has no longer any supporters. It is now beyond question that the
story began with Pier Paolo Vergerio, a malignant slanderer of
Farnese. The slander was refuted at the time by Bishop Della Casa in
the time of Vergerio, and later by Ammiani, Poggiali, Morandi,
Cardinal Quirino and Apostolo Zeno, not to mention many others.
Pier Luigi was great by rank and by nature. He restrained the
arrogance of his nobles and had studied much to elevate his people
to an equality with their lords. He was supported in these plans by
the distinguished literary men who served as his secretaries; Claudio
Tolomei, Giovanni Battista Pico, David Spilimbergo, Gandolfo Porrino,
Giovanni Paccini, Gottifredi, Rainerio, Zuccardi, Tebalducci, Apollonio
and Caro. The last after the death of his master was pursued by
assassins and with great difficulty saved his life by fleeing into the
province of Cremona.
This open friendship of Farnese for the people, at a time when
the lords were everywhere practising great severity, added to the
hatred of the imperial agents and whetted their desire for
vengeance. There was still another cause of quarrel. The port of the
Po at Piacenza had been ceded by Paul III. to the divine Bonarotti
(taking away certain rights upon it from the Pusterla and Trivulzio)
and Bonarotti had rented it to Francesco Durante, and the nobles
taking the sides of the defrauded parties resolved to wreak their
vengeance on the pontiff’s son. A conspiracy was formed at the head
of which were Giovanni Anguissola, Camillo and Gerolamo Pallavicini
and Giovanni Confaloniere. But the soul of the plot was count
Agostino Landi, the same person who informed the government at
Lucca of the conspiracy of Pietro Fatinelli, and thus betrayed him to
death.
Andrea opened his heart to Landi and showed him the golden
promises of Cæsar. Casoni relates this and he founded it upon
irrefragible proofs which he had in his hands. He adds that the
prince pledged to Landi the hand of the sister of Gianettino for his
son with a wealthy dowry. This marriage afterwards took place. It
was important that, after the assassination of the duke, the duchy of
Piacenza should revert to the empire, and to secure this result Doria
intrigued with Gerolamo Pallavicino, Marquis of Cortemaggiore and
Busseto, whose mother and wife had been held in captivity by
Farnese and who was therefore anxious to punish the affront. The
conspirators in Piacenza at first really intended to establish a popular
government; but Doria adroitly induced them to communicate with
Gonzaga. It was not difficult then to secure the subjection of
Piacenza to the empire.
A warm animosity burned between Gonzaga and the duke on
account of the priorship of Barletta which Gonzaga had obtained for
his son to the exclusion of Horace Farnese. Gonzaga made many
attempts upon the life of Pier Luigi. Annibal Caro, who in July, 1547
was sent by the latter to Milan informed his master of these plots;
but the duke had no presentiment of his imminent peril. The efforts
of Gonzaga, however, all failed, and with the knowledge of Charles,
he sent captain Federico Gazzino to order the conspirators to
proceed with their work.
On the tenth of December 1547 Giovanni Anguissola went to the
castle which Farnese had erected to command the city and
demanded instant speech of the duke on matters of pressing
urgency. Having entered, Anguissola and his friend Giovanni
Valentino threw themselves upon the duke and killed him with stabs
in his face and breast. On leaving the apartment, the assassin killed
a priest and a servant who were rushing in to ascertain the occasion
of the duke’s cries, struck down a German lancer who threw himself
before him and ran to rejoin his fellow conspirators, who, led by
Confaloniere immediately overpowered the garrison of the citadel.
Others, headed by Landi and the Pallavicini brothers, attacked and
soon captured the castle with but little loss of life. Some mercenaries
fleeing from the citadel spread a report that the Spaniards had
attacked the castle; and the plebians, to whom the very name
Spaniards was odious, rose in arms, gathered around Tommasoni da
Terni, captain of the city militia, and marched to the citadel to
recover it by storm.
The battle could not have been long or doubtful; for only thirty-
seven conspirators were in possession of the fortress. But they
invented an expedient which served them in the stead of force. They
hung the corpse of the duke to the wall and afterwards threw it into
the moat. The sight destroyed the hopes of the people. The
conspirators found means to increase the number of their adherents
and to occupy the city. Captain Ruschino arrived before the gates,
according to a previous understanding, at the head of a considerable
body of infantry and shortly after the castellan of Cremona arrived
with reinforcements. These were followed by Gonzaga himself who
took possession in the name of Cæsar. The vengeance of Doria was
complete.
The Venitians were greatly grieved by these events; indeed, all
the governments in Italy which were unfriendly to the Spanish
power were alarmed at its success. The nobles of Piacenza regretted
too late that they had changed masters without gaining their
liberties. Gonzaga had promised to destroy the citadel, but he
increased its strength and it remained for three centuries.
Piacenza was never restored to the Farnese in spite of that
spirited discourse which Casa wrote to Cæsar and which we find in
his works. The Pope in full concistory asked an account from the
emperor of the assassination of his son and the seizure of Piacenza,
and demanded the punishment of Gonzaga. But the emperor
pleased with his success, paid no attention either to the threats of
the Pope or the appeals of his son-in-law and Margaret. Gonzaga
was not even content with Piacenza but attempted to grasp Parma
also. He moved an army against it, but the valour of Camillo Orsino
rendered his efforts fruitless. To secure his grandson against Spanish
treachery, Paul kept him near his own person in Rome, until Ottavio,
weary of living in privacy put himself into the power of the ministers
of Charles and returned to Parma. The old pontiff, pricked to the
heart by the death of his son and the fruitlessness of his appeals to
other governments against Spain, soon ended his days in bitterness
and sorrow (1549).
Though the assassins of Farnese obtained rewards from the
emperor they were long the objects of atrocious persecutions from
Rome. Anguissola was created governor of Como; but he sought
refuge from many assassins who dodged his steps in the Pliniana
villa which he had constructed. Beleseur, French ambassador, having
encountered him in the Grisons tried to pierce him in the very palace
of the bishop with the dagger of papal vengance. A certain Rinaldo
Rondinello, of the mountains of Cesena, long followed him in the
mantle of a friar; and when this assassin was punished, many others
rose up to take his place, until Anguissola seeing himself the object
of universal scorn and the mark of every stiletto terminated his
miserable life in sorrow and remorse. Gerolamo Pallavicini who with
his brother Alessando and others was an accomplice in that crime
was making the campaign in Flanders in 1552, in company with his
relatives. Eight masked men one day assailed him, killed all his
relatives and left him stretched upon the earth with five severe
wounds. However, he recovered and retired to his castle of
Castiglione di Lodi, which he had obtained from the Fieschi. He
made a vow to marry the first woman whom he should meet. Fate
was propitious and Gerolamina Virotelli, the daughter of a
mountaineer and a woman of more than womanly prudence, made
the evening of his life cheerful. Count Landi died in remorse and
bequeathed a rich legacy to the heir of the murdered Farnese
Gonzaga, too, died miserably. Some assassins, Corsican soldiers of
Ottavio Farnese, several times attempted to kill him; but it was
reserved for the Genoese to avenge on him the death of the Fieschi
and Farnese, and his other crimes. Tommaso Marini and Ottobuono
Giustiniani obtained a decree from Charles, that Gonzaga be
subjected to an examination for the robberies with which he was
charged. The emperor acquitted him, but removed him from the
governorship of Milan and the disgrace so wounded him that he died
of his grief.
These acts of vengeance were followed by others of a fierce
character. In these, Andrea Doria was the instructor. At the death of
Pier Luigi nothing remained for him but to punish the Pope for his
complicity with the Count of Lavagna; but the elevation of Paul and
the sanctity of his office put him out of the reach of personal
violence. Other arms than daggers must be employed, and fortune
put them into the hands of Doria. We must here premise that after
the death of Gianluigi, the Pope, to suppress the rumour that he was
accessory to the conspiracy, sent Andrea a brief, condoling with him
for the death of Gianettino. The fierce Genoese, who well knew the
arts of Roman wolves, swallowed his resentiment and was silent
until the time arrived to settle his account with the successor of St.
Peter. As soon as he learned through Cristoforo Lercaro Di Salvo,
captain of Chiavari, that Pier Luigi was dead, he took that same
brief, changed only the names and sent it back to the author as his
letter of condolence for the death of the pontiff’s son. The injury was
great; but the punishment was terrible.
These punishments and assassinations did not restore order and
confidence. The blood which had been spilled fertilized the soil for a
new harvest of disaster and suffering.
CHAPTER XIII.

THE NOBLES AND THE PLEBEIANS.

Intrigues of Figuerroa and the nobility—The law of Garibetto—New efforts of Spain


to give Genoa the character of a Duchy—The firmness of the senate and
Andrea foils the scheme of Don Filippo—The reception of the Spaniards by
Doria and by the people—Sad story of a daughter of the Calvi—Don
Bernardino Mendozza and his relations with Prince Doria—Baneful influence of
the Spanish occupation.

Charles V. had long cherished the design of rendering the entire


Peninsula subject to his authority. He was master of the Sicilies and
the Milanese and controlled Tuscany through the servility of Cosimo;
and if he were able to complete the conquest of Genoa, it would be
easy to expel the French army from Piedmont where Henry II. was
preparing to renew the war in Italy. It is true that the emperor
through the senate and Doria actually directed Genoese affairs; but
dependence on the will and favour of individuals did not seem to
Charles either a dignified or durable means of power. The conspiracy
of Fieschi had been crushed; but it had left discontents behind it and
a new outbreak was possible at every hour. Besides, Charles thirsted
to be complete master of a city which was in his view, and in fact,
the connecting link between the kingdoms of Spain and his Lombard
provinces.
Figuerroa, knowing the wishes of his master, opened his views to
the old nobles who were his intimates and drew them over to his
wishes. He terrified them by setting forth the prospect of new
conspiracies and the popular affection for Gianluigi which was still
strong in the city. He told them that Andrea was too decrepit to
combat these approaching perils and that prudence counselled
adequate provisions to suppress revolt. Figuerroa found in the minds
of the old nobles, morbidly sensitive to the least breath of popular
commotion, complacent acquiescence, and he induced some of the
faction of San Luca to address a petition to the emperor in Germany,
in which they exaggerated the Fieschi movement, showed the
uncertain faith of many of the Italian princes and the danger of
general revolt and concluded by requesting that the security of
Genoa be provided for by a Spanish garrison and a more stable form
of government.
The emperor answered the appeal by sending Nicolò Perenoto,
lord of Granveille and imperial councillor, with some engineers, to
construct a fortress on the hill of Pietra Minuta as a rein on the
Genoese populace. This fortification garrisoned by a strong Spanish
force would have secured the imperial power and stifled all attempts
at revolution. But Andrea, who wished to rule Genoa himself,
vehemently opposed the erection of a fortress to be occupied by
imperial troops. The prince desired to be the sole imperial
representative in Genoa and to keep the Spanish crown in a state of
dependence upon his loyalty. He therefore resisted the innovation
with all his power, and boldly told Granveille that he must lay aside
the project. When the imperial minister informed him of the petition
sent by the Genoese nobility to the emperor, the old man called to
him the persons chiefly concerned in that business, reproached them
spiritedly for the weakness they had shown in falling into an imperial
trap, and induced them to recant their approval of this scheme of
national humiliation.
But Granveille still hoped to win Doria’s consent to the wishes of
the emperor, and he frequently sent his engineers to Pietra Minuta
for the purpose of defining the position of the new citadel. The
people saw these surveys, and they one day broke into tumult,
rushed to the place and would have killed Granveille and his
engineers if the senate had not forseen the danger and stationed
troops so as to prevent access to the hill. The emperor was now
convinced that he could only carry out his plans by an open war
both with Andrea and the people; and he therefore wrote to the
prince that he would renounce a project which seemed so distasteful
to his admiral.
Doria on his side pledged himself to reform the government and
give it such a direction as to put it out of the power of a few persons
to reëstablish the popular constitution. He accordingly instituted the
provision called Garibetto which entirely excluded popular families
from political power and gave rise to many civil disorders and finally
to intestine war. It completed the alienation of the masses from the
nobility and destroyed the vital force of the Republic. But the
plebeians, the more they were depressed, burned the more for
liberty. The spirit of revolution sometimes slumbered but was never
entirely extinguished. The opposition of Doria and the threatening
attitude of the populace deterred the Spaniards and the greater part
of the old nobles from carrying out their scheme of building a
fortress to overawe the people. But though Charles bent to the will
of our people in that project, he secured through the prince a more
oligarchic form of government and removed the new nobles from
power. This success and the increasing subservience of Doria
inspired Charles with new hope that he might get Genoa entirely in
his power as a first step to the complete control of the Peninsula. He
renewed his efforts with more shrewdness and contrived a scheme
for taking the populace by surprise and lulling to sleep the vigilance
of the old admiral.
A conference was held in Piacenza by the Duke of Alba, Gonzaga,
an envoy of Cosimo, and Tomaso de’ Marini a Genoese knight. It was
agreed that when Doria had sailed to Spain, to escort the Archduke
Maximilian, Gonzaga should enter the city with a large body of
imperial troops and Cosimo should support the movement with some
regiments of infantry. The pretext for this military concentration was
afforded by the fact that the Prince Don Phillip, called into Germany
by his father, would return with Doria to Genoa and Cosimo and
Gonzaga would go thither to pay him homage.
Having made these arrangements, the Duke of Alba sailed with
Doria for Spain (July, 1548) in order to prepare other parts of the
conspiracy. But the Genoese fortunately received information of the
plot. The Pope, who, since the death of his son, distrusted the
emperor more than ever, having heard of the conference in
Piacenza, instructed Carlo Orsino, governor of Piacenza, to ascertain
what had been done by the conspirators. Orsino laboured so well
that he penetrated the mystery. Some incautious words of Gonzaga
put him on the scent of the movement and enabled him to inform
the Pope of the nature of the emperor’s plans. Paul communicated
this intelligence to Leonardo Strata, a Genoese noble living in Rome,
and Strata immediately wrote to the senate. The scheme was so
bold and unexpected that the senators were at first disposed to
distrust the report. But their doubts were soon removed. Gonzaga
soon after sent a messenger to notify the government that Don
Phillip would soon arrive in Genoa, and to ask quarters in the city for
two thousand cavalry and as many arquebusiers. At the same time,
Cosimo wrote asking permission to pay homage to the prince in
Genoa and to bring as an escort, to protect him against the plots of
Genoese exiles, two regiments of cavalry and two of infantry. Andrea
also wrote from Rosas (October 19th, 1548) a letter to the Doge,
which, as an eloquent proof of his servility to Spain, we give entire:

“I send with this galley Don Michele de Velasco and with him
three quarter-masters whom His Highness the prince desires to have
forwarded in advance of himself, for reasons which you will more
fully learn from his ambassador, Figuerroa. Their mission as you will
learn is to prepare lodgings for this court. It seems expedient for me
to write you these few words, as a citizen, praying you to give me
pleasure by issuing orders that these quarter-masters be allowed to
accompany Don Michele, and assigning them without delay all the
lodgings which may be necessary.
“Receive them with such marks of esteem as you are accustomed
to give when the honour of princes and the glory of the city are
concerned, in order that His Majesty and this Illustrious Prince, his
son, may know that, not only in this, but in matters of much greater
moment, you are delighted to render him service. For, besides the
general repute which your excellencies will gain by such a course of
conduct, the favour of His Majesty and His Highness will be much
greater towards you, and their love for the Republic will be increased
so that they will the more cheerfully aid her in the hour of need, as
hitherto. Your Excellencies should remember that we have no other
light or support but the great goodness of His Majesty which permits
us to live within his kingdoms without any sense of subjection, and
that for this reason alone the whole city ought to do spontaneously
whatever is required in these circumstances, and all the more that in
these matters which require small sacrifices we shall gain large
favour and induce His Majesty to grant us privileges of greater
importance. I know well that our citizens will interpose obstacles as
they are accustomed to do in such emergencies; but your
Excellencies, knowing the convenience and importance of the matter,
will strive to remove all difficulties, compel all to preserve order and
obedience and punish whoever makes opposition in such a way as to
render them a warning and example to all the rest. I have nothing
more to add on this subject; for I am sure that you, as wise men,
will carefully reflect on the duty we owe the emperor, and voluntarily
and cheerfully give those orders that are required; the more that the
stay of the prince will be only for a few days, and small as the favour
will be, His Majesty will reckon it a great one and always remember
your good will and that of the city towards Himself. His Highness will
also be gratified for your prompt good service and all his suite will
leave you greatly pleased by your hospitality. M. Domenico Doria,
the bearer of this letter, will speak more fully of this concernment to
your Excellencies, to whom I commend me with affectionate
solicitude.”
These simultaneous requests removed the doubts of the
senators. They showed an admirable firmness in refusing quarters
for the soldiers of Gonzaga and Medici. Gonzaga renewed his
request and the senate replied that if he appeared at the gates with
more than twenty horses he would find them shut in his face. He
came with three hundred infantry and two companies of cavalry, but
he was obliged to quarter himself outside of the walls, in Sestri.
Cosimo, seeing the firmness of the senate, relinquished the design
of coming. But no one dared resist Doria, and his Spaniards were
received in the city.
While these events were transpiring Don Phillip sailed out of
Spain with a fleet of fifty-eight galleys, of which nineteen belonged
to Prince Doria and six to Antonio Doria, two to the prince of Monaco
and two to Visconte Cicala. There were forty other vessels of which
six were Genoese. Don Phillip took passage on board the admiral’s
galley, a vessel wonderful for her size, construction and equipment.
The designs of the embellishments were made by Pierino del Vaga,
and executed by Carota and Tasso, Florentine artists. The standards
were painted by Vaga. The gilding, the satins and the rich brocades
rendered the vessel a marvel of beauty. The young prince,
astonished by this magnificence, was prodigal of honours and marks
of affection to Andrea, hoping to captivate the old man and secure
his coöperation in the plot against the Republic. As they neared our
coasts, Phillip inquired of the admiral where he would be quartered
in Genoa. The admiral responded that he hoped to have that honour
for his palace in Fassiolo, where the emperor had been his guest.
The young Prince showed dissatisfaction at the response and
rejoined that he wished to reside in the Ducal palace. “That,” replied
Andrea “Is not in my power. Your Highness may ask it of the senate,
though I am of opinion that those who live there will not willingly
evacuate it.” These frank words enraged Phillip, and his wrath was
yet more inflamed immediately after by letters of Gonzaga which
reported that their plan could not be put into execution. The young
prince broke out into angry imprecations; but his preceptor, the
Duke of Alba conjured him to conceal his displeasure lest the
suspicions of the Genoese should be increased, and Phillip
constrained himself to a complacent reception of the messengers of
the Republic.
He landed at Savona and was entertained by Benedetta Spinola,
a beautiful and courteous widow. After a brief stay he proceeded to
Genoa. The princess Peretta received him in the Doria palace with
the highest honour. The Doge and the senators, the Genoese
cardinals Doria and Cybo, Lord Bishop Matera, envoy of the Pope,
and the ministers of other nations went to pay him homage.
We shall not dwell on the sumptuous reception of Phillip by the
nobility, or the splendour which Doria displayed with his open court
and princely banquets for the Spanish barons. The luxury of the
decorations, the richness of the furniture, the splendour of the
carpets and service of every kind and the wealth sunk in the
banquets of that palace were then the marvel of Italy. Don Phillip
and his suite were filled with admiration by the magnificence of their
reception.
The Genoese populace did not participate in these festivities.
They could ill brook these servile attentions towards those who were
conspiring, not merely to deprive them of political power, but to take
away the independence of the Republic; and, looking on with ill-
concealed rage, they were more than once on the brink of
revolution. On the 3rd of December at midnight, the people rose at
the cry of “Ammazza, Ammazza”—kill them, kill them—and rushed to
attack fifty of the Bisogni who were in a tavern of the mole; and
they would have despatched the Spaniards, if Colonel Spinola had
not arrived on the ground with a strong body of infantry in time to
quell the tumult. But the rage of the populace continued. Don Phillip
had requested the city police to arrest a certain Don Antonio d’Arze,
a Spaniard guilty of homicide. After the arrest, he sent eighty
Spanish arquebusiers to conduct the criminal from the prison on
board a galley. Near the Ducal palace, this body of Spaniards met
the city guard. The Bisogni had their matches lit, and the guard,
believing that the imperial troops came to assault the palace,
prepared to make a desperate resistance, and in fact drove the
Spaniards back by force. Many of the latter were wounded and some
lost their lives. In a twinkling, the rumour ran that the Spaniards had
attacked the Ducal palace; the people collected in crowds and would
have put the Spaniards to the edge of the sword if the Doge and two
governors of the palace had not mingled in the crowd and soothed
the irritation. Prince Doria himself was carried in a palanquin through
the most populous quarters, and besought the people to lay aside
their hostile intentions. The populace was held in subjection by force
and supplications; but the Spaniards lost no time in returning on
board their ships, and Don Phillip departed dissimulating his
animosity against the city.
We must here speak of an incident which occurred while Don
Phillip was the guest of the city; though Bandello places it some
years earlier.
In one of the many descents of the Turkish corsair upon the
Riviera, they had captured a Genoese girl about ten years of age,
belonging, says the chronicle, to the illustrious family of the Calvi.
Being of remarkable beauty she was sold by the pirates at a high
price to a merchant who carried her into Spain. Here she grew more
beautiful with years and inspired a son of the Duke of Alba with an
ardent passion which he found means to satisfy. When Don Phillip
came into Italy, the young man was obliged to accompany the
cortège; but not wishing to leave the young woman, he took her on
board one of the vessels and brought her to Genoa. Annina had
never forgotten her parents and her native city; and as soon as she
landed, she induced her pages by rich presents to find her lodgings
on the piazza Maruffi, near the palace of Stefano Fieschi and in the
residence of the Calvi. Annina entered her father’s house with joy,
and, seizing a moment when her lover was occupied with Don
Phillip, she dismissed her domestics and revealed herself to her
parents. The embracings, the tears, the transports of tenderness,
cannot be described. But the noble girl broke off these
demonstrations of affection. “It is time that I think of my liberation.
Though loaded with ornaments, I have been hitherto only a slave,
and I owe it to my dignity and my blood to atone in the shadow of
the altar for my dishonourable though forced manner of life. Take
me to a convent before my master learns that I belong to you, and
put me in a cell where none may ever hear my name pronounced.”
Her parents approved her choice and at once sent her to a
monastery near the city, where she was received under another
name. She had scarcely departed when the knight came to find his
mistress, and, inquiring for her, he read in the silence of the pages
that she had fled. He was at the first moment about to wreak his
anger on these servants; but he restrained himself and demanded of
the Calvi the restoration of the girl. An angry contention arose which
raised a tumult in that part of the city. In a few moments the piazza
was full of men of both nations. Among the first to enter the house
of Calvi to succour the Genoese was Giovanni Lavagna, allied by
blood to the Fieschi. He was one of the most reckless warriors of his
time. Encountering the Spanish knight at the head of the staircase
surrounded by armed men and threatening the bystanders, he
demanded the cause of his discourteous manners. Alba replied:—
“It does not concern thee, white moor and traitor that thou art!”
Lavagna was not accustomed to receive abuse with patience, and
he angrily retorted:—
“Moorish Jew, thou liest in the throat!” and drawing his sword,
threw himself upon the Spaniard. The fight was of brief duration.
Despite the assistance of his companions, the knight was pierced to
the heart. The Spaniards descended into the piazza and came to
blows with the populace, who killed some and put the others to
flight. Lavagna fearing the vengeance of Phillip took refuge in the
province of Piacenza.
Don Phillip did not relinquish the hope of reducing Genoa to the
condition of a province, and he was encouraged by Gonzaga,
Figuerroa and the Duke of Alba. The plan of the new fortress was
again taken up. The partisans of Spain reasoned that the popular
hostility to Spain constantly threatened the city with revolution and
that so stubborn a people needed a strong rein. It was reasonable
enough they said that Doria, when he was in the full vigour of life,
should have opposed the erection of the citadel, but now when he
was old and infirm almost to decrepitude he ought no longer to
resist the will of Cæsar.
Charles sent to Genoa a certain Sigismondi Fransino with
instructions to confer with Doria and Centurione and endeavour to
gain their consent to the fortification. Some engineers also came
secretly, for the purpose of selecting the most convenient site. They
renounced the plan of fortifying Pietra Minuta and recommended
that the fortress of Castelletto should be restored. Doria hearing of
this new plan and wishing to finish once for all with these projects
for the humiliation of Genoa, sent Adamo Centurione into Flanders
to confer with Cæsar and convince him that there was imminent
peril of losing the Republic altogether unless these schemes were
renounced. Charles made the most formal pledges that he would put
a stop to the intrigue and never again raise the question. The advice
of Don Bernardino Mendozza probably had more weight with Charles
than the remonstrances of Centurione. Mendozza was a man of
infinite cunning and dexterity in politics. He pointed out to his
sovereign the excessive devotion of the Genoese to the acquisition
of wealth, and advised him to employ every artifice to get their
money into the imperial treasury in the form of loans secured upon
lands, privileges, feuds and jurisdictions in Sicily, Naples and Spain.
“Thus,” said the adroit politician, “you will bind the Genoese to the
fortunes of your kingdom by a voluntary chain; since when their
riches are in your hands they will be naturally inclined to increase
and maintain your power. This hold upon their affections will be
worth more than any fortress.”
This shrewd advice was followed; every inducement was held out
to the wealthy nobles to place their money in the hands of the
emperor, with such securities and guarantees as would infallibly
induce other citizens to follow the example and bind themselves with
their fortunes to Spain. By this expedient Charles seemed to leave
the Genoese their independence, but he really made them tributary
to his crown, Phillip II. pursued this policy with even greater
assiduity and it became hereditary in the Spanish princes. It was in
fact for two centuries the political science by which the court of
Spain regulated the affairs of Italy; and the people found themselves
insensibly bound, without their own action, to the interests and
policy of that crown. It must be said that some give a different
version of the affair of the citadel. Writers of weight tell us that,
even in this, Doria was subservient to Charles; but we cannot believe
it possible. His steadfast resistance to that scheme is more
consistent with the greatness and fame of the illustrious admiral;
and, though he was a vehement partisan of the imperial cause, he
could not have wished to become, like Cosimo, its slave. When the
Medici gave up to imperial troops the fortresses of Florence and
Leghorn, he found himself in the hands of a master, and never
digested the retort of Venice, who refused to treat with him
“because he was, in his own house, the servant of another man.”
We think the truth to be that when Doria saw the unanimity of
the people in opposing the erection of a citadel, he wisely resolved
to support his fellow-citizens, and the people are entitled to the chief
praise for the failure of that scheme. They were not yet corrupted by
the servility of the nobility, and might have renewed the examples of
their ancient valour and prevented the foreign power from striking
root in the Republic. They lost no opportunity of manifesting their
profound dislike of Spain, as Doge Lercaro himself testifies. When
Charles gave to Cosimo the government of Piombino, then in the
hands of the Appiani, the Genoese rose up in arms and demanded of
the senate that galleys be despatched to Elba to expel the
Florentines and Spaniards. This time, too, it was Doria who held
back the arms of the people.
It is easy to see that the new ties between Genoa and Spain
were the principal occasion of our decline. Doria, by breaking the
French alliance and persecuting the men of Barbary (instead of
courting their alliance after the example of Venice) hastened our fall.
Our commerce gradually declined. French and Barbary fleets roved
over our seas and destroyed our marine. The city was put to great
straits, and longed in vain for the only remedy for its maladies, the
alliance of France to open up the commerce of the East. Fieschi,
who had courted these benefits, was remembered the more sadly as
disasters multiplied upon the Republic.
The government comprehended that some important and
energetic measures must be taken to restore our fortunes; and, after
mature reflection, the senate resolved to attempt the recovery of our
Eastern trade. The only remnant of our extensive possessions in the
Levant was the island of Scio, which was still held by the family of
the Giustiniani. In 1558, Giovanni Di Franchi and Nicolò Grillo were
sent to Constantinople, with eight vessels bearing costly presents for
the Sultan and his principal ministers, to ask a renewal of trade and
treaties of amity and commerce such as the Porte maintained with
the Venitians.
The Porte was disposed to accept our trade and friendship, but
the king of France raised objections which destroyed the hopes of
Genoa. He showed the Porte that the Genoese were the fast allies of
Spain, and could not remain neutral between Spaniards and Turks;
that all the maritime enterprises of Charles to the damage of the
Turks had been conducted with Genoese fleets; that Doria the
greatest of the enemies of Turkey and the admiral of Spain, lived in
Genoa and ruled it at his caprice; that, in fine, the Porte could not
safely listen to the proposals of the Genoese unless they declared
themselves enemies of Spain. These arguments changed the
purpose of Soliman, and he sent the Ligurian ambassadors home
without giving them audience. The Republic lost hope of reacquiring
that commerce with the East which had once enabled it to triumph
over Pisa and Venice.
Such were the consequences of our fatal bondage to the empire.
The people, guided by infallible instincts, showed in this matter more
wisdom than their rulers. If we had shaken off the imperial
embraces, we might have obtained from the Turks all those
privileges which the Venitians had acquired a few years before; nor
should we have had rivals to contest our gains. The French were
falling into civil commotions which turned their attention from
commercial enterprises. The English seldom showed themselves in
our seas. The Dutch had not yet thrown off the yoke at which they
were fretting, and the Venitians soon after, becoming as inimical as
the Spaniards to the Turkish power, were excluded from Eastern
markets. The Levant, still rich in silk fabrics, might have been a
fountain of vast wealth for Genoese merchants.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like