Table of Contents
| Ch. 1 | What is Microsoft.NET? | 1 |
| Ch. 2 | Introducing Visual Basic 2005 and Visual Studio 2005 | 19 |
| Ch. 3 | Variables and type | 51 |
| Ch. 4 | Object syntax introduction | 83 |
| Ch. 5 | Inheritance and interfaces | 137 |
| Ch. 6 | The common language runtime | 197 |
| Ch. 7 | Applying objects and components | 223 |
| Ch. 8 | Generics | 259 |
| Ch. 9 | Namespaces | 283 |
| Ch. 10 | Exception handling and debugging | 311 |
| Ch. 11 | Data access with ADO.NET 2.0 | 341 |
| Ch. 12 | Using XML in Visual Basic 2005 | 389 |
| Ch. 13 | Security in the .NET framework 2.0 | 439 |
| Ch. 14 | Windows forms | 497 |
| Ch. 15 | Windows forms advanced features | 551 |
| Ch. 16 | Building Web applications | 583 |
| Ch. 17 | ASP.NET 2.0 advanced features | 627 |
| Ch. 18 | Assemblies | 671 |
| Ch. 19 | Deployment | 691 |
| Ch. 20 | Working with classic COM and interfaces | 747 |
| Ch. 21 | Enterprise services | 773 |
| Ch. 22 | Threading | 803 |
| Ch. 23 | XML Web services | 835 |
| Ch. 24 | Remoting | 871 |
| Ch. 25 | Windows services | 899 |
| Ch. 26 | Network programming | 931 |
| Ch. 27 | Visual Basic and the Internet | 965 |
Read a Sample Chapter
Professional VB 2005
By Bill Evjen John Wiley & Sons
ISBN: 0-7645-7536-8
Chapter One
What Is Microsoft .NET?
New technologies force change, nowhere more so than in computers and software. Occasionally, a new technology is so innovative that it forces us to challenge our most fundamental assumptions. In the computing industry, the latest such technology is the Internet. It has forced us to rethink how software should be created, deployed, and used.
However, that process takes time. Usually, when a powerful new technology comes along, it is first simply strapped onto existing platforms. So it has been for the Internet. Before the advent of Microsoft .NET, developers used older platforms with new Internet capabilities "strapped on." The resulting systems worked, but they were expensive and difficult to produce, hard to use, and difficult to maintain.
Realizing this several years ago, Microsoft decided it was time to design a new platform from the ground up specifically for the post-Internet world. The result is called .NET. It represents a turning point in the world of Windows software for Microsoft platforms. Microsoft has staked their future on .NET and publicly stated that henceforth almost all their research and development will be done on this platform. It is expected that, eventually, almost all Microsoft products will be ported to the .NET platform. (However, the name ".NET" will evolve, as you will see at the end of the chapter.)
Microsoft is now at version 2.0 of Microsoft .NET, and the development environment associated with this version is called Visual Studio 2005. The version of Visual Basic in this version is, thus, called Visual Basic 2005, and that's what this book is all about.
What Is .NET?
Microsoft's .NET initiative is broad-based and very ambitious. It includes the .NET Framework, which encompasses the languages and execution platform, plus extensive class libraries, providing rich built-in functionality. Besides the core .NET Framework, the .NET initiative includes protocols (such as the Simple Object Access Protocol, commonly known as SOAP) to provide a new level of software integration over the Internet, via a standard known as Web Services.
Although Web Services are important (and are discussed in detail in Chapter 23), the foundation of all .NET-based systems is the .NET Framework. This chapter will look at the .NET Framework from the viewpoint of a Visual Basic developer. Unless you are quite familiar with the Framework already, you should consider this introduction an essential first step in assimilating the information about Visual Basic .NET that will be presented in the rest of this book.
The first released product based on the .NET Framework was Visual Studio .NET 2002, which was publicly launched in February 2002, and included version 1.0 of the .NET Framework. Visual Studio .NET 2003 was introduced a year later and included version 1.1 of the .NET Framework. As mentioned, the current version is Visual Studio 2005. (Note that the ".NET" part of the name has been dropped for this version.)
This book assumes that you are using VS.NET 2005. Some of the examples will work transparently with VS.NET 2002 and VS.NET 2003, but you should not count on this, because the difference between 2.0 and the earlier versions is significant.
A Broad and Deep Platform for the Future
Calling the .NET Framework a platform doesn't begin to describe how broad and deep it is. It encompasses a virtual machine that abstracts away much of the Windows API from development. It includes a class library with more functionality than any yet created. It makes available a development environment that spans multiple languages, and it exposes an architecture that makes multiple language integration simple and straightforward.
At first glance, some aspects of .NET appear similar to previous architectures, such as UCSD Pascal and Java. No doubt some of the ideas for .NET were inspired by these past efforts, but there are also many brand new architectural ideas in .NET. Overall, the result is a radically new approach to software development.
The vision of Microsoft .NET is globally distributed systems, using XML as the universal glue to allow functions running on different computers across an organization or across the world to come together in a single application. In this vision, systems from servers to wireless palmtops, with everything in between, will share the same general platform, with versions of .NET available for all of them, and with each of them able to integrate transparently with the others.
This does not leave out classic applications as you have always known them, though. Microsoft .NET also aims to make traditional business applications much easier to develop and deploy. Some of the technologies of the .NET Framework, such as Windows Forms, demonstrate that Microsoft has not forgotten the traditional business developer. In fact, such developers will find it possible to Internet- enable their applications more easily than with any previous platform.
What's Wrong with DNA and COM?
The pre-.NET technologies used for development on Microsoft platforms encompassed the COM (Component Object Model) standard for creation of components, and the DNA model for multitier software architectures. As these technologies were extended into larger, more enterprise-level settings, and as integration with the Internet began to be important, several major drawbacks became apparent. These included:
Difficulty in integrating Internet technologies: Hard to produce Internet-based user interfaces No standard way for systems and processes to communicate over the Internet Expensive, difficult, and undependable deployment Poor cross-language integration Weaknesses in the most popular Microsoft tool-Visual Basic: Lack of full object orientation, which made it impossible to produce frameworks in Visual Basic One threading model that did not work in some contexts Poor integration with the Internet Other weaknesses such as poor error-handling capabilities
It is important to note that all pre-.NET platforms, such as Java, also have some of these drawbacks, as well as unique ones of their own. The drawbacks related to the Internet are particularly ubiquitous.
Microsoft .NET was created with the Internet in mind. It was also designed specifically to overcome the limitations of COM and products such as Visual Basic 6 and Active Server Pages. As a result, all of the preceding limitations have been eliminated or significantly reduced in Microsoft .NET and Visual Studio 2005.
An Overview of the .NET Framework
First and foremost, .NET is a framework that covers all the layers of software development above the operating system level. It provides the richest level of integration among presentation technologies, component technologies, and data technologies ever seen on a Microsoft, or perhaps any, platform. Second, the entire architecture has been created to make it as easy to develop Internet applications as it is to develop for the desktop.
The .NET Framework actually "wraps" the operating system, insulating software developed with .NET from most operating system specifics such as file handling and memory allocation. This prepares for a possible future in which the software developed for .NET is portable to a wide variety of hardware and operating system foundations.
VS.NET supports Windows 2003, Windows XP, and all versions of Windows 2000. Programs created for .NET can also run under Windows NT, Windows 98, and Windows Me, though VS.NET does not run on these systems. Note that in some cases certain service packs are required to run .NET.
The major components of the Microsoft .NET Framework are shown in Figure 1-1.
The framework starts all the way down at the memory management and component loading level and goes all the way up to multiple ways of rendering user and program interfaces. In between, there are layers that provide just about any system-level capability that a developer would need.
At the base is the common language runtime, often abbreviated to CLR. This is the heart of the .NET Framework-it is the engine that drives key functionality. It includes, for example, a common system of datatypes. These common types, plus a standard interface convention, make cross-language inheritance possible. In addition to allocation and management of memory, the CLR also does reference tracking for objects and handles garbage collection.
The middle layer includes the next generation of standard system Services such as classes that manage data and Extensible Markup Language (XML). These services are brought under control of the Framework, making them universally available and making their usage consistent across languages.
The top layer includes user and program interfaces. Windows Forms is a new and more advanced way to do standard Win32 screens (often referred to as "'smart clients"). Web Forms provides a new Web-based user interface. Perhaps the most revolutionary is Web Services, which provide a mechanism for programs to communicate over the Internet, using SOAP. Web Services provide an analog of COM and DCOM for object brokering and interfacing, but based on Internet technologies so that allowance is made even for integration to non-Microsoft platforms. Web Forms and Web Services, which constitute the Internet interface portion of .NET, are implemented by a part of the .NET Framework referred to as ASP.NET.
All of these capabilities are available to any language that is based on the .NET platform, including, of course, VB.NET.
The Common Language Runtime
We are all familiar with runtimes-they go back further than DOS languages. However, the common language runtime (CLR) is as advanced over traditional runtimes as a machine gun is over a musket. Figure 1-2 shows a quick diagrammatic summary of the major pieces of the CLR.
That small part in the middle of Figure 1-2 called Execution support contains most of the capabilities normally associated with a language runtime (such as the VBRUNxxx.DLL runtime used with Visual Basic). The rest is new, at least for Microsoft platforms.
Key Design Goals
The design of the CLR is based on the following primary goals:
Simpler, faster development Automatic handling of system-level tasks such as memory management and process communication Excellent tool support Simpler, safer deployment Scalability
Notice that many of these design goals directly address the limitations of COM/DNA. Let's look at some of these in detail.
Simpler, Faster Development
A broad, consistent framework allows developers to write less code, and reuse code more. Using less code is possible because the system provides a rich set of underlying functionality. Programs in .NET access this functionality in a standard, consistent way, requiring less "hardwiring" and customization logic to interface with the functionality than is typically needed today.
Programming is also simpler in .NET because of the standardization of datatypes and interface conventions. As will be discussed later, .NET makes knowledge of the intricacies of COM much less important.
The net result is that programs written in VB.NET that take proper advantage of the full capabilities of the .NET Framework typically have significantly less code than equivalent programs written in earlier versions of Visual Basic. Less code means faster development, fewer bugs, and easier maintenance.
Excellent Tool Support
Although much of what the CLR does is similar to operating system functionality, it is very much designed to support development languages. It furnishes a rich set of object models that are useful to tools like designers, wizards, debuggers, and profilers, and since the object models are at the runtime level, such tools can be designed to work across all languages that use the CLR. It is expected that third parties will produce a host of such tools.
Simpler, Safer Deployment
It is hard for an experienced Windows component developer to see how anything can work without registration, GUIDs, and the like, but the CLR does. Applications produced in the .NET Framework can be designed to install with a simple XCOPY. That's right-just copy the files onto the disk and run the application (as long as the .NET Framework was previously installed, which is discussed in more detail in the following sections). This hasn't been seen in the Microsoft world since the days of DOS (and some of us really miss it).
This works because compilers in the .NET Framework embed identifiers (in the form of metadata, to be discussed later) into compiled modules, and the CLR manages those identifiers automatically. The identifiers provide all the information needed to load and run modules, and to locate related modules.
As a great by-product, the CLR can manage multiple versions of the same component (even a shared component) and have them run side by side. The identifiers tell the CLR which version is needed for a particular compiled module, because such information is captured at compile time. The runtime policy can be set in a module to use the exact version of a component that was available at compile time, to use the latest compatible version, or to specify an exact version. The bottom line is that .NET is intended to eradicate DLL hell once and for all.
This has implications that might not be apparent at first. For example, if a program needed to run directly from a CD or a shared network drive (without first running an installation program), that was not feasible in Visual Basic after version 3. That capability reappears with VB.NET. This dramatically reduces the cost of deployment in many common scenarios.
Another significant deployment benefit in .NET is that applications only need to install their own core logic. An application produced in .NET does not need to install a runtime, for example, or modules for ADO or XML. Such base functionality is part of the .NET Framework, which is installed separately and only once for each system. The .NET Framework will eventually be included with the operating system and probably with various applications. Those four-disk installs for a VB "Hello world" program are a thing of the past.
.NET programs can also be deployed across the Internet. Version 2.0 of the .NET Framework includes a new technology specifically for that purpose called ClickOnce. This is a new capability in .NET, supplementing the older "no touch deployment." You can read about ClickOnce in Chapter 19.
The .NET Framework, which includes the CLR and the Framework base classes, is required on every machine where you want to run .NET applications and code. For Windows 2003 and above, the .NET Framework is installed automatically as part of the operating system. For older operating systems, or to install a newer version of the .NET Framework, the .NET Framework is a separate installation. Deployment of .NET applications is discussed in Chapter 19.
Scalability
Since most of the system-level execution functions are concentrated in the CLR, they can be optimized and architected to allow a wide range of scalability for applications produced in the .NET Framework. As with most of the other advantages of the CLR, this one comes to all applications with little or no effort.
Memory and process management is one area where scalability can be built in. The memory management in the CLR is self-configuring and tunes itself automatically. Garbage collection (reclaiming memory that is no longer being actively used) is highly optimized, and the CLR supports many of the component management capabilities of MTS/COM+ (such as object pooling). The result is that components can run faster and, thus, support more users.
This has some interesting side effects. For example, the performance and scalability differences among languages become smaller. All languages compile to a standard bytecode called Microsoft Intermediate Language (MSIL), often referred to simply as IL, and there is a discussion later on how the CLR executes IL. With all languages compiling down to similar bytecode, it becomes unnecessary in most cases to look to other languages when performance is an issue. The difference in performance among .NET languages is minor-Visual Basic, for example, gives about the same performance as any of the other .NET languages.
Versions of the CLR are available on a wide range of devices. The vision is for .NET to be running at all levels, from smart palmtop devices all the way up to Web farms. The same development tools work across the entire range-news that will be appreciated by those who have tried to use older Windows CE development kits.
Metadata
The .NET Framework needs lots of information about an application to carry out several automatic functions. The design of .NET requires applications to carry that information within them. That is, applications are self-describing. The collected information that describes an application is called metadata.
(Continues...)
Excerpted from Professional VB 2005 by Bill Evjen Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.