Introduction and Welcome

The initial release of the .NET platform (circa 2001) caused quite a stir within the Visual Basic programming community. One the one hand, many die-hard VB6 developers were up in arms at the major differences between VB6 and Visual Basic .NET (VB .NET). Individuals in this group were a bit stunned to see that VB .NET was not in fact "VB7" (i.e., the same syntax and programming constructs as VB6 with some new features thrown in for good measure), but something altogether different.

The truth of the matter is that VB .NET had little to do with VB6, and might best be regarded as a new language in the BASIC family. This cold hard fact caused some individuals to recoil to such a degree that they coined terms such as "VB .NOT" or "Visual Fred" to express their displeasure. In fact, there are even websites ( and petitions dedicated to criticizing Microsoft's decision to abandon VB6 in favor of this new creature known as VB .NET.

Beyond the major syntactical changes introduced with VB .NET, several VB6-isms were nowhere to be found under the .NET platform, which only added to the confusion. The core .NET programming models for data access, form development, and website construction are entirely different from their COM-based counterparts.

As time has progressed, and the .NET platform has become a mainstream programming model, it seems that even the most die-hard VB6 developer has come to see the writing on the wall: VB6 is quickly becoming a legacy programming tool. Even Microsoft itself has made it clear that support for VB6 will be phased out over time. For better or for worse, the hand of change has been forced upon us.

Note With the release of .NET 2.0 (circa 2005), VB .NET was renamed to "Visual Basic 2005." As of .NET 3.5, Microsoft's BASIC language has been renamed yet again, this time to "Visual Basic 2008" (yes, the VB rename-game is maddening). Throughout this text, when you see the term Visual Basic, VB, or Visual Basic 2008, do know I am referring to the BASIC language that we find within the .NET platform. When I am referring to the COM-centric BASIC language, I'll use the terms Visual Basic 6.0 or simply VB6.

On the other end of the spectrum, there were many VB6 developers who were excited by the myriad new language features and openly embraced the necessary learning curve. Members of this group were ready to dive into the details of object-oriented programming (OOP), multithreaded application development, and the wealth of functionality found within the .NET base class libraries. These individuals quickly realized that in many (if not a majority of) cases, existing VB6 code could remain VB6 code, while new development could take place using the .NET platform and the latest iteration of the Visual Basic language.

Strangely enough, there is also a third group of individuals, formed with the release of Visual Basic .NET. Given that VB .NET was in fact a brand new OOP language, many developers who would have never considered learning a BASIC-centric language (typically C++, Java, and C# programmers) were now much more open to the idea of exploring a language devoid of semicolons and curly brackets.

In any case, regardless of which group you identify with, I do welcome you to this book. The overall approach I will be taking is to treat VB 2008 as a unique member of the BASIC family. As you read over the many chapters that follow, you will be exposed to the syntax and semantics of

VB 2008. Here you will find a thorough grounding in OOP, coverage of all of the new VB 2008 language features (such as object initialization syntax, anonymous types, extension methods, and Language Integrated Query [LINQ]), and guidance for working within the Visual Studio 2008 integrated development environment.

As well, this text will dive into each of the major .NET code libraries you will make use of as you build .NET applications. You will be exposed to each of the .NET desktop programming frameworks (Windows Forms and Windows Presentation Foundation), database programming with ADO.NET, web development with ASP.NET, as well as a number of other critical .NET topics such as assembly configuration, Windows Communication Foundation, Windows Workflow Foundation, and file IO operations.

Technology authors write for a demanding group of people (I should know—I'm one of them). You know that building software solutions using any platform is extremely detailed and is very specific to your department, company, client base, and subject matter. Perhaps you work in the electronic publishing industry, develop systems for the state or local government, or work at NASA or a branch of the military. Speaking for myself, I have developed children's educational software, various n-tier systems, and numerous projects within the medical and legal industries. The chances are almost 100 percent that the code you write at your place of employment has little to do with the code I write at mine (unless we happened to work together previously!).

Therefore, in this book, I have deliberately chosen to avoid creating examples that tie the example code to a specific industry or vein of programming. Given this, I choose to explain VB 2008, OOP, the CLR, and the .NET 3.5 base class libraries using industry-agnostic examples. Rather than having every blessed example fill a grid with data, calculate payroll, or whatnot, I'll stick to subject matter we can all relate to: automobiles (with some geometric structures and employees thrown in for good measure). And that's where you come in.

My job is to explain the VB 2008 programming language and the core aspects of the .NET platform the best I possibly can. To this end, I will do everything I can to equip you with the tools and strategies you need to continue your studies at this book's conclusion.

Your job is to take this information and apply it to your specific programming assignments. I obviously understand that your projects most likely don't revolve around automobiles with pet names, but that's what applied knowledge is all about! Rest assured, once you understand the concepts presented within this text, you will be in a perfect position to build .NET solutions that map to your own unique programming environment.

Who Should Read This Book?

I do not expect that you have any current experience with BASIC-centric languages or the Microsoft .NET platform (however, if this is the case, all the better). I am assuming that you are either a professional software engineer or a student of computer science. Given this, please know that this book may not be a tight fit for individuals who are brand-new to software development, as we will be exploring many lower-level/advanced topics and will not be spending all of our time binding data to grids (at least not until Chapter 22) or examining every single option of the Visual Studio 2008 menu system.

While this book will dive into some more advanced topics, this is not to say the material covered here is impractical! This book focuses on the details you must understand to be a proficient Visual Basic 2008 developer. While some of this information can be challenging (for example, understanding the role of delegates and lambda expressions), I hope you'll find the text is written in a friendly and approachable vibe.

My assumption is that you are the sort of developer who wishes to understand the inner workings of VB 2008, and are not content with authoring code by simply "dragging and dropping." While this book will most certainly examine how Visual Studio 2008 can be used to reduce the amount of code you must author by hand, I'll typically only illustrate the use of integrated wizards once you have seen how to author the code yourself. This will make it easy for you to modify the IDE-generated code to your liking.

An Overview of This Book

Pro VB 2008 and the .NET3.5 Platform is logically divided into eight distinct parts, each of which contains some number of chapters that are focused on a given technology set and/or specific task. To set the stage, here is a part-by-part and chapter-by-chapter breakdown of the book you are holding in your hands.

Part 1: Introducing Visual Basic 2008 and the .NET Platform

The purpose of Part 1 is to acclimate you to the core aspects of the .NET platform, the .NET type system, and various development tools used during the construction of .NET applications. Along the way, you will also check out some basic details of the VB 2008 programming language.

Chapter 1: The Philosophy of .NET

This first chapter functions as the backbone for the remainder of the text. We begin by examining the world of traditional Windows development and uncovering the shortcomings with the previous state of affairs. The primary goal of this chapter, however, is to acquaint you with a number of .NET building blocks, such as the common language runtime (CLR), Common Type System (CTS), Common Language Specification (CLS), and base class libraries. Also, you will take an initial look at the VB 2008 programming language and the .NET assembly format, and you'll examine the platform-independent nature of the .NET platform and the role of the Common Language Infrastructure (CLI).

Chapter 2: Building Visual Basic 2008 Applications

The goal of this chapter is to introduce you to the process of compiling VB 2008 source code files using various tools and techniques. First, you will learn how to make use of the command-line compiler (vbc.exe) and VB 2008 response files. Over the remainder of the chapter, you will examine numerous IDEs, including SharpDevelop, Visual Basic 2008 Express, and (of course) Visual Studio 2008. As well, you will be exposed to a number of open source tools that many .NET developers have in their back pocket.

Part 2: Core VB Programming Constructs

This part explores the core aspects of the VB 2008 programming language such as intrinsic data types, decision and iteration constructs, constructing (and overloading) methods, as well as manipulating arrays, strings, enumerations, and modules. Next, you will dive into the details of object-oriented programming (OOP) as seen through the eyes of VB. As well, you will learn about the role of structured exception handling and how the CLR handles memory management details.

Chapter 3:VB 2008 Programming Constructs, Part I

This chapter begins by examining the role of the VB 2008 module type and the related topic of an executable's entry point—the Main() method. You will also come to understand the intrinsic data types of VB 2008 (and their CTS equivalents), implicit and explicit casting operations, iteration and decision constructs, and the construction of valid code statements.

Chapter 4:VB 2008 Programming Constructs, Part II

Here you will complete your examination of basic coding constructs. The major thrust of this chapter is to dive into the details of building subroutines and functions using the syntax of VB 2008. Along the way, you will get to know the roles of the ByVal, ByRef, and ParamArray keywords and understand the topic of method overloading. This chapter also examines how to build and manipulate arrays, enums, and structures, and the underlying classes that lurk in the background (System.Array, System.Enum, and System.ValueType).

Chapter 5: Designing Encapsulated Class Types

This chapter will dive into the first "pillar of OOP," encapsulation services. Not only will you learn the basics of class construction (constructors, shared members, and property syntax), but you will also investigate several auxiliary class design techniques such as the role of the Partial keyword and XML code documentation syntax.

Chapter 6: Understanding Inheritance and Polymorphism

The role of Chapter 6 is to examine the details of how VB 2008 accounts for the remaining "pillars" of OOP: inheritance and polymorphism. Here you will learn how to build families of related classes using inheritance, virtual methods, abstract methods (and classes!), as well as various casting operations. This chapter will also explain the role of the ultimate base class in the .NET libraries: System.Object.

Chapter 7: Understanding Structured Exception Handling

The point of this chapter is to discuss how to handle runtime anomalies in your code base through the use of structured exception handling. Not only will you learn about the VB 2008 keywords that allow you to handle such problems (Try, Catch, Throw, and Finally), but you will also come to understand the distinction between application-level and system-level exceptions. In addition, this chapter examines various tools within Visual Studio 2008 that allow you to debug the exceptions that have escaped your view.

Chapter 8: Understanding Object Lifetime

This chapter examines how the CLR manages memory using the .NET garbage collector. Here you will come to understand the role of application roots, object generations, and the System.GC type. Once you understand the basics, the remainder of this chapter covers the topics of building "disposable objects" (via the IDisposable interface) and how to interact with the finalization process (via the System.Object.Finalize() method).

Part 3: Advanced VB Programming Constructs

This part furthers your understanding of OOP using VB 2008. Here you will learn the role of interface types, delegates, and events, and several advanced topics such as operator overloading and generics. As well, this section dives into the details of the new .NET 3.5 language features, including your first look at LINQ.

Chapter 9: Working with Interface Types

The material in this chapter builds upon your understanding of object-based development by covering the topic of interface-based programming. Here you will learn how to define types that support multiple behaviors, how to discover these behaviors at runtime, and how to selectively hide particular behaviors from an object level. To showcase the usefulness of interface types, you will learn how interfaces can be used to build a custom event architecture.

Chapter 10: Collections, Generics, and Nullable Data Types

This chapter begins by examining the collection types of the System.Collections namespace, which has been part of the .NET platform since its initial release. However, since the release of .NET 2.0, the VB programming language offers support for generics. As you will see, generic programming greatly enhances application performance and type safety. Not only will you explore various generic types within the System.Collections.Generic namespace, but you will also learn how to build your own generic methods and types (with and without constraints).

Chapter 11: Delegates, Events, and Lambdas

The purpose of Chapter 11 is to demystify the delegate type. Simply put, a .NET delegate is an object that "points" to other methods in your application. Using this pattern, you are able to build systems that allow multiple objects to engage in a two-way conversation. After you have examined the use of .NET delegates, you will then be introduced to the VB 2008 Event, RaiseEvent, Handles, and Custom keywords, which are used to simplify the manipulation of programming with delegates in the raw. Finally, you will come to understand the role of lambda expressions and the VB 2008 lambda operator.

Chapter 12: Operator Overloading and Custom Conversion Routines

This chapter deepens your understanding of the VB 2008 programming language by introducing a number of advanced programming techniques. Here, you will find a detailed examination of value types and reference types. Next, you will learn how to overload operators and create custom conversion routines (both implicit and explicit). We wrap up by contrasting the use of CType(), DirectCast(), and TryCast() for explicit casting operations.

Chapter 13: VB 2008-Specific Language Features

With the release of .NET 3.5, the VB language has been enhanced to support a great number of new programming constructs, many of which are used to enable the LINQ API (which you will begin to examine in Chapter 14). Here, you will learn the role of implicit typing of local variables, extension methods, anonymous types, and object initialization syntax.

Chapter 14:An Introduction to LINQ

This chapter will begin your examination of LINQ, which could easily be considered the most intriguing aspect of .NET 3.5. As you will see in this chapter, LINQ allows you to build strongly typed query expressions, which can be applied to a number of LINQ targets to manipulate "data" in the broadest sense of the word. Here, you will learn about LINQ to Objects, which allows you to apply LINQ expressions to containers of data (arrays, collections, custom types). This information will serve you well when we examine how to apply LINQ expressions to relational databases (via LINQ to ADO) and XML documents (a la LINQ to XML) later in Chapter 24.

Part 4: Programming with .NET Assemblies

Part 4 dives into the details of the .NET assembly format. Not only will you learn how to deploy and configure .NET code libraries, but you will also understand the internal composition of a .NET binary image. This section of the text also explains the role of .NET attributes and the construction of multithreaded applications as well as accessing legacy COM applications using interop assemblies.

Chapter 15: Introducing .NET Assemblies

From a very high level, assembly is the term used to describe a managed *.dll or *.exe file. However, the true story of .NET assemblies is far richer than that. Here you will learn the distinction between single-file and multifile assemblies, and how to build and deploy each entity. You'll examine how private and shared assemblies may be configured using XML-based *.config files and publisher policy assemblies. You will also investigate the role of the .NET Framework configuration utility.

Chapter 16: Type Reflection, Late Binding, and Attribute-Based Programming

Chapter 16 continues our examination of .NET assemblies by checking out the process of runtime type discovery via the System.Reflection namespace. Using these types, you are able to build applications that can read an assembly's metadata on the fly. You will learn how to dynamically activate and manipulate types at runtime using late binding. The final topic of this chapter explores the role of .NET attributes (both standard and custom). To illustrate the usefulness of each of these topics, the chapter concludes with the construction of an extendable Windows Forms application.

Chapter 17: Processes, AppDomains, and Object Contexts

Now that you have a solid understanding of assemblies, this chapter dives deeper into the composition of a loaded .NET executable. The goal of this chapter is to illustrate the relationship between processes, application domains, and contextual boundaries. These topics provide the proper foundation for the topic of the following chapter, where we examine the construction of multithreaded applications.

Chapter 18: Building Multithreaded Applications

This chapter examines how to build multithreaded applications and illustrates a number of techniques you can use to author thread-safe code. The chapter opens by revisiting the .NET delegate type in order to understand a delegate's intrinsic support for asynchronous method invocations. Next, you will investigate the types within the System.Threading namespace. You will look at numerous types (Thread, ThreadStart, etc.) that allow you to easily create additional threads of execution. We wrap up by examining the BackgroundWorker type, which greatly simplifies the creation of threads from within a desktop user interface.

Chapter 19: .NET Interoperability Assemblies

The last chapter in this part will examine a unique type of .NET assembly termed an interop assembly. These binary images are used to allow .NET applications to make use of classic COM types. Once you dive into the details of how .NET applications can consume COM servers, you will then learn the functional opposite: COM applications consuming .NET objects. Once you have completed this chapter, you will have a solid understanding of the interoperability layer.

Part 5: Introducing the .NET Base Class Libraries

By this point in the text, you have a very solid handle of the VB 2008 language and the details of the .NET type system and assembly format. Part 5 leverages your newfound knowledge by exploring a number of commonly used services found within the base class libraries, including file IO and database access using ADO.NET. This part also covers the construction of distributed applications using Windows Communication Foundation (WCF) and workflow-enabled applications that make use of the Windows Workflow Foundation (WF) API.

Chapter 20: File and Directory Manipulation

As you can gather from its name, the System.IO namespace allows you to interact with a machine's file and directory structure. Over the course of this chapter, you will learn how to programmatically create (and destroy) a directory system as well as move data into and out of various streams (file based, string based, memory based, etc.).

Chapter 21: Introducing Object Serialization

This chapter examines the object serialization services of the .NET platform. Simply put, serialization allows you to persist the state of an object (or a set of related objects) into a stream for later use. Deserialization (as you might expect) is the process of plucking an object from the stream into memory for consumption by your application. Once you understand the basics, you will then learn how to customize the serialization process via the ISerializable interface and a number of .NET attributes.

Chapter 22: ADO.NET Part I: The Connected Layer

In this first of two database-centric chapters, you will learn about the ADO.NET programming API. Specifically, this chapter will introduce the role of .NET data providers and how to communicate with a relational database using the connected layer of ADO.NET, represented by connection objects, command objects, transaction objects, and data reader objects. Be aware that this chapter will also walk you through the creation of a custom database and a data access library that will be used throughout the remainder of this text.

Chapter 23: ADO.NET Part II: The Disconnected Layer

This chapter continues your study of database manipulation by examining the disconnected layer ofADO.NET. Here, you will learn the role of the DataSet type, data adapter objects, and numerous tools of Visual Studio 2008 that can greatly simplify the creation of data-driven applications. Along the way, you will learn how to bind DataTable objects to user interface elements, such as the GridView type of the Windows Forms API.

Chapter 24: Programming with the LINQ APIs

Chapter 14 introduced you to the LINQ programming model, specifically LINQ to Objects. Here, you will deepen your understanding of Language Integrated Query by examining how to apply LINQ queries to relational databases, DataSet objects, and XML documents. Along the way, you will learn the role of data context objects, the sqlmetal.exe utility, and various LINQ-specific aspects of Visual Studio 2008.

Chapter 25: Introducing Windows Communication Foundation

.NET 3.0 introduced a brand-new API, WCF, that allows you to build distributed applications, regardless of their underlying plumbing, in a symmetrical manner. This chapter will expose you to the construction of WCF services, hosts, and clients. As you will see, WCF services are extremely flexible, in that clients and hosts can leverage XML-based configuration files to declaratively specify addresses, bindings, and contracts.

Chapter 26: Introducing Windows Workflow Foundation

In addition to WCF, .NET 3.0 also introduced an API, WF, that allows you to define, execute, and monitor workflows to model complex business processes. Here, you will learn the overall purpose of Windows Workflow Foundation, as well as the role of activities, workflow designers, the workflow runtime engine, and the creation of workflow-enabled code libraries.

Part 6: Desktop Applications with Windows Forms

This section of the text examines how to build graphical desktop applications using the Windows Forms API. As you may know, Windows Forms is the original desktop GUI framework that has been part of the .NET base class libraries since version 1.0. While it is true that .NET 3.0 shipped a new GUI framework (Windows Presentation Foundation), Windows Forms is still a key part of .NET development and will most likely be your UI toolkit of choice for many traditional business applications.

Chapter 27: Introducing Windows Forms

This chapter begins your examination of the System.Windows.Forms namespace. Here you will learn the details of building traditional desktop GUI applications that support menu systems, toolbars, and status bars. As you would hope, various design-time aspects of Visual Studio 2008 will be examined.

Chapter 28: Rendering Graphical Data with GDI+

This chapter covers how to dynamically render graphical data in the Windows Forms environment. In addition to discussing how to manipulate fonts, colors, geometric images, and image files, this chapter examines hit testing and GUI-based drag-and-drop techniques. You will learn about the Windows Forms resource format, which allows you to embed graphics image files, string data, and other aspects of a desktop application into the executable itself.

Chapter 29: Programming with Windows Forms Controls

This final Windows Forms chapter will examine numerous GUI widgets that ship with the .NET Framework 3.5. Not only will you learn how to program against various Windows Forms controls, but you will also learn about dialog box development and Form inheritance. As well, this chapter examines how to build custom Windows Forms controls that integrate into the IDE.

Part 7: Desktop Applications with WPF

This section of the text focuses on a brand-new desktop programming model named Windows Presentation Foundation (WPF). As you will see, WPF is a "supercharged" UI toolkit that allows you to build highly interactive and media-rich desktop programs. Here you will understand the role of

WPF, the use of an XML-based grammar named XAML, and several WPF features such as animation, graphical rendering, and data binding.

Chapter 30: Introducing Windows Presentation Foundation and XAML

.NET 3.0 introduced a brand-new GUI toolkit termed WPF. Essentially, WPF allows you to build extremely interactive and media-rich front ends for desktop applications (and indirectly, web applications). Unlike Windows Forms, this supercharged UI framework integrates a number of key services (2D and 3D graphics, animations, rich documents, etc.) into a single unified object model. In this chapter, you will begin your examination of WPF and the Extendable Application Markup Language (XAML). Here, you will learn how to build WPF programs XAML-free, using nothing but XAML, and a combination of each. We wrap up by building a custom XAML viewer, which will be used during the remainder of the WPF-centric chapters.

Chapter 31: Programming with WPF Controls

In this chapter, you will learn how to work with the WPF control content model as well as a number of related control-centric topics such as dependency properties and routed events. As you would hope, this chapter provides coverage of working with a number of WPF controls; however, more interestingly, this chapter will explain the use of layout managers, control commands, and the WPF data binding model.

Chapter 32: WPF 2D Graphical Rendering, Resources, and Themes

The final chapter of this part will wrap up your examination of WPF by examining three seemingly independent topics. However, as you will see, WPF's graphical rendering services typically require you to define custom resources. Using these resources, you are able to generate custom WPF animations, and using graphics, resources, and animations, you are able to build custom themes for a WPF application. To pull all of these topics together, this chapter wraps up by illustrating how to apply custom graphical themes at runtime.

Part 8: Building Web Applications with ASP.NET

The final part of this text is devoted to the construction of web applications using ASP.NET. As you will see, ASP.NET was intentionally designed to model the creation of desktop user interfaces by layering on top of standard HTTP request/response an event-driven, object-oriented framework.

Chapter 33: Building ASP.NET Web Pages

This chapter begins your study of web technologies supported under the .NET platform using ASP.NET. As you will see, server-side scripting code is now replaced with "real" object-oriented languages (such as VB 2008, C#, and the like). This chapter will introduce you to key ASP.NET topics such as working with (or without) code-behind files, the ASP.NET 3.5 directory structure, and the role of the web.config file.

Chapter 34: ASP.NET Web Controls, Themes, and Master Pages

This chapter will dive into the details of the ASP.NET web controls. Once you understand the basic functionality of these web widgets, you will then build a simple but illustrative website making use of various .NET 3.5 features (master pages, *.sitemap files, themes, and skins). As well, this chapter will examine the use of the validator controls and the enhanced data binding engine.

Chapter 35: ASP.NET State Management Techniques

This chapter extends your current understanding of ASP.NET by examining various ways to handle state management under .NET. Like classic ASP, ASP.NET allows you to easily create cookies, as well as application-level and session-level variables. Once you have looked at the numerous ways to handle state with ASP.NET, you will then come to learn the role of the System.HttpApplication base class (lurking within the Global.asax file). We wrap up with an examination of the ASP.NET profile management API.

Obtaining This Book's Source Code

All of the code examples contained within this book (minus small code snippets here and there) are available for free and immediate download from the Source Code area of the Apress website. Simply navigate to, select the Source Code/Download link, and look up this title by name. Once you are on the "homepage" for Pro VB 2008 and the .NET3.5 Platform, you may download a self-extracting *.zip file. After you unzip the contents, you will find that the code has been logically divided by chapter.

Do be aware that Source Code notes like the following in the chapters are your cue that the example under discussion may be loaded into Visual Studio 2008 for further examination and modification:

Source Code This is a source code note referring you to a specific directory!

To do so, simply open the *.sln file found in the correct subdirectory. If you are not making use of Visual Studio 2008 (see Chapter 2 for additional IDEs), you can manually load the provided source code files into your development tool of choice.

Obtaining Updates for This Book

As you read through this text, you may find an occasional grammatical or code error (although I sure hope not). If this is the case, my apologies. Being human, I am sure that a glitch or two may be present, despite my best efforts. If this is the case, you can obtain the current errata list from the Apress website (located once again on the "homepage" for this book) as well as information on how to notify me of any errors you might find.

Contacting Me

If you have any questions regarding this book's source code, are in need of clarification for a given example, or simply wish to offer your thoughts regarding the .NET platform, feel free to drop me a line at the following e-mail address (to ensure your messages don't end up in my junk mail folder, please include "VB TE" in the Subject line somewhere): [email protected]

Please understand that I will do my best to get back to you in a timely fashion; however, like yourself, I get busy from time to time. If I don't respond within a week or two, do know I am not trying to be a jerk or don't care to talk to you. I'm just busy (or, if I'm lucky, on vacation somewhere).

So, then! Thanks for buying this text (or at least looking at it in the bookstore while you try to decide if you will buy it). I hope you enjoy reading this book and putting your newfound knowledge to good use.

Was this article helpful?

0 0

Post a comment