Writing this book was great, albeit tiring fun. The unique format of the "cookbook" is surprisingly rewarding, as it gives the author an unusual chance to create chapters filled with dense, practical examples that specifically deal with the issues raised by professional programmers. I look forward to finding out what recipes are the most usefuland what interesting techniques or tricks I might have overlooked.
I owe a heartfelt thanks to many individuals at Microsoft Press, including Danielle Voeller, who sold me on the idea of a cookbook, and Dick Brown, who guided it through the editorial process. Marc Young and Mark Bukovec provided the tech review for this book (along with many invaluable suggestions), and Lisa Pawlewicz added polish with her expert copyediting. Im also indebted to the principal compositor Dan Latimer and electronic artist Joel Panchot, as well as numerous others at Microsoft Press who have worked tirelessly behind the scenes to ensure that indexing, figures, and page setting were successful. In addition, Id like to extend a special thanks to those individuals who contributed tips, insight, or tools that I used in creating the recipes in this book, including Lutz Roeder, Francesco Balena, Mike Krueger, Lance Olson, and many more. Any remaining mistakes or omissions are purely my own.
Finally, I would like to thank my parents (all four of them) and my loving wife.
Introduction
The Microsoft Visual Basic .NET language now spans more programming tools, concepts, and application programming interfaces (APIs) than ever before. With the Microsoft .NET Framework, your Visual Basic code can go to work in a Microsoft Windows service, a Web application, an XML Web service, a Windows client, or a remote component. It can use robust multithreading, manipulate relational data and XML, harness COM+ services, and more. With all this functionality comes a priceeven the most experienced developer can have trouble isolating a useful feature buried somewhere in the enormous .NET class library!
The Microsoft Visual Basic .NET Programmers Cookbook is designed to share some of the best practices, tips, and undocumented secrets that help programmers master all aspects of .NET. This book doesnt intend to replace the many excellent tutorials that describe .NET basics and explain the foundational concepts for programming various types of applications. Instead, this book aims to fill the knowledge gaps of a professional programmer. In other words, you shouldnt turn to this book to learn how to create your first multithreaded program. However, when you need a reference that can give you an at-a-glance look at several different asynchronous programming patterns and provide you with a recipe for deriving your own custom thread class, this book will be invaluable.
The best way to think of this book is as a cross between an "FAQ on steroids" and a library of templates that show best practices. It would be impossible to list all the useful snippets of code Ive come across as a .NET programmer (and even if I did, the resulting book would be too large and disorganized to be much help to anyone). Instead, this book includes recipes that respond to the questions developers ask again and again on message boards, discussion lists, at conferences, in my .NET courses, and in direct-to-author e-mails. One of the reasons that a book like this works so well is that developers do run into the same problems time and time again, and the right solutions are often universal.
This book not only focuses on how to do things, but also how to do them right . For example, its easy enough to create a custom Exception class, but developers wont necessarily know the recommended constructors they should include, or the steps they should take to make the exception serializable so that it can be thrown across application domains in a remoting scenario. Youll find a similar theme when using threads, implementing common design patterns in .NET code, or creating custom objects that support the standard interfaces for copying, cloning, and comparing.
Another difference with this book and many other .NET titles is that its relentlessly practical. Many books about .NET cover only what the .NET platform encompasses. They might describe its limitations, but they rarely go beyond them. This book, on the other hand, covers everything developers need to get their work accomplished, even if it stretches into an area where the .NET class library hasnt caught up yet. Here are some examples:
The Win32 API . As comprehensive as the .NET Framework is, it doesnt duplicate the entire Windows API. Some reasons you might turn to the API include playing a WAV file, logging in as a new user, getting Web connectivity information, and shutting down Windows programmatically.
Windows Management Instrumentation (WMI) . WMI is a framework you can use to retrieve a wide array of hardware-specific and operating systemspecific information. Well use WMI to find out about currently installed printers, and retrieve lists of Windows users and groups for the current computer.
JavaScript . In Microsoft ASP.NET Web applications, all code executes on the server. If you want to set control focus on the client, show a client-side message box, or just tailor the appearance of the rendered page, you need to enhance your pages with a twist of JavaScript.
Legacy COM Components . The .NET Framework supplants the Component Object Model (COM), but there are still a number of tasks that can be greatly simplified with legacy COM components. One example is the Windows Script Host, which allows you to create shortcuts and send keystrokes to other applications. Well use other examples when showing a Web page, using a masked text box, playing video, sending e-mail messages with Messaging Application Programming Interface (MAPI), and automating Microsoft Office applications.
Your Own Custom Components . Some of the missing pieces of .NET can be easily filled in with your own custom components. For example, this book includes completely developed classes for accessing a File Transfer Protocol (FTP) site, contacting a Post Office Protocol (POP) mail server, reading MP3 headers, creating a salted password hash for storage in a database, and working with vectors, fractions, and complex numbers.
Third-Party Components . The .NET component market is alive and well, with many professional components for sale that can save hours of programming effort. We wont cover any of these in this book, but we will feature a few components that are open source or completely free. This includes components for reading ZIP files, writing PDF documents, and showing menus in ASP.NET.
Feedback
This book is largely driven by the questions and interests of the programming community. So, if you have an unanswered question or found that a recipe didnt completely answer a question, feel free to e-mail me at . Reader feedback will be used to enhance the recipe set in future editions of this book.
Code Samples
The recipes in this book are designed to illustrate a particular concept by isolating only the essential details. Many of the code samples use simple Console applications, and often the complete code is provided in the recipe. Other times, only code snippets will be shown. Mundane details, like the designer code Microsoft Visual Studio .NET adds automatically to any Windows application, are not shown.