CodeNotes for VB.NET illustrates the major changes in Visual Basic with the new .NET edition, emphasizing the Common Language Runtime (CLR), syntax changes, Windows Forms, assemblies, new object oriented programming features, threading, and a survey of new .NET technologies such as ADO.NET, ASP.NET, and SOAP. This book will help any level of VB developer understand the power of VB.NET and learn the necessary techniques to transition from VB6 to VB.NET.
This edition of CodeNotes includes:
-A global overview of a technology and explanation of what problems it can be used to solve
-"How and Why" sections that provide hints, tricks, workarounds, and tips on what should be taken advantage of or avoided
-Instructions and classroom-style tutorials throughout from expert trainers and software developers
Visit www.codenotes.com for updates, source code templates, access to message boards, and discussion of specific problems with CodeNotes authors and other developers.
Every CodeNotes title is written and reviewed by a team of commercial software developers and technology experts. See "About the Authors" at the beginning of the book for more information.
About the Author
Read an Excerpt
VB.NET AND THE .NET FRAMEWORK
Visual Basic is a creature that must evolve. Since its inception 10 years ago, each new version of the product has boasted additional capabilities to adapt to new technologies and paradigms. Visual Basic 5, for example, allowed developers to create ActiveX controls when Microsoft was attempting to foster the ActiveX proprietary technology. Visual Basic 6 introduced the Web Class Designer that greatly simplified the deployment of web applications using Dynamic HTML. VB.NET continues the evolutionary trend, but on a much grander scale. It is fair to say that VB.NET (what was once thought of as VB7) represents the most fundamental change in the Visual Basic language ever. Several aspects of the product that have remained consistent since VB’s original release are different under VB.NET:
• The Visual Basic IDE is now integrated with VS.NET, Microsoft’s next version of Visual Studio. This means that VB now shares its design environment with other Microsoft languages such as Visual C++.
• The Visual Basic Forms engine that drove form design in previous versions of VB has been replaced with Windows Forms, the new way to design desktop GUIs. Although Windows Forms still employs the same intuitive approach to application design (whereby one paints applications by dragging and dropping controls onto a form), it exposes many details abstracted in previous versions of VB.
• VB.NET has its own command-line compiler (called vbc.exe), allowing you to compile applications outside the Visual Basic environment. You can, for example, write VB source code using a text editor such as Notepad and then compile the source file from the command prompt.
In addition to these noteworthy changes, the language’s syntax has been modified significantly. Array bounds, parameter passing, and control properties, for example, all behave differently under VB.NET. New features such as C++ try/catch error handling routines, multithreading, and object-oriented concepts such as inheritance have been added to the language. Microsoft has also removed numerous elements from the VB syntax. Among others, Variant, GoSub, IsNull, and IsMissing have all been retired.
Although each new version of a product raises compatibility issues, the magnitude of changes in VB.NET makes migration a prime concern for many developers. In Chapter 3, we investigate VB.NET’s syntax changes in detail, as well as the Upgrade Wizard, which is designed to assist developers porting VB6 code to VB.NET.
The .NET Framework
To understand VB.NET, you must understand its new ecological niche—the .NET Framework. Simply put, .NET is Microsoft’s new strategy for the development and deployment of software. Depending on your interests and development background, you may already have a number of ideas about what exactly .NET is. As we will see in these CodeNotes,
• .NET fundamentally changes the way applications execute under the Windows operating system.
• With .NET, Microsoft is, in effect, abandoning its traditional stance, one that favors compiled components, and is embracing interpreted technology (similar in many ways to the Java paradigm).
• In addition to radically overhauling Visual Basic, .NET also brings about significant changes to Visual C++ and introduces a new language called C# (pronounced “C-sharp”).
• .NET is built from the ground up with the Internet in mind, embracing open Internet standards such as XML and HTTP. XML is also used throughout the framework as both a messaging instrument and for configuration files.
These are all noteworthy features of .NET, or more accurately the .NET Framework, which consists of the platform and tools needed to develop and deploy .NET applications.
A UNIVERSAL RUNTIME
To effectively write programs in VB.NET, you must understand two .NET entities: the Common Language Runtime (CLR) and the Base Class Libraries (BCL). If you have worked with Visual Basic 6 before, you have in fact already used lesser versions of both entities. To understand how, let us consider the VB6 Runtime file (MSVBVM60.DLL) that must accompany every VB6 application you deploy.
Although distributing the 1.4-megabyte runtime file with your VB6 applications is annoying, it is essential for two reasons:
1. The VB Runtime provides services such as automatic memory management, string allocation/deallocation, and saftey checking on code. If, for example, your VB code tries to reference an array outside its boundaries, the VB Runtime steps in and informs you that an illegal operation is being performed. This is in contrast to languages such as C and C++, where memory access is not monitored, resulting in potentially catastrophic failures if code misbehaves. And, unlike C and C++, the VB Runtime will automatically clean up after you, destroying objects and variables once they go out of scope.
2. The VB Runtime also houses intrinsic VB functions such as MsgBox, Ubound, and InStr. For years, VB developers have routinely used such functions for string manipulation, type conversion, and user input. The code for these functions is contained in the VB Runtime. When you call Mid() to extract a substring from a string, you are really calling a function in the VB Runtime that performs the work. Without the VB Runtime, a VB application would be reduced to a meaningless collection of function calls.
From these two observations, it can be said that a Visual Basic 6 application runs within the VB Runtime. In fact, the name of the runtime file is indicative of this very purpose: MSVBVM stands for MicroSoft Visual Basic Virtual Machine. With VB.NET, the VB Runtime has grown into two separate and larger entities: the Common Language Runtime and the Base Class Libraries.
The Common Language Runtime
The Common Language Runtime (CLR) is the execution environment for all programs in the .NET Framework. The CLR is similar to a Java Virtual Machine (VM), which interprets bytecode and executes it on the fly. Instead of interpreting Java bytecode, however, the CLR reads Microsoft Intermediate Language (IL) code that is produced by the VB.NET compiler. Like the VB Runtime, the CLR scrutinizes code before it executes to ensure that it stays within its designated boundaries. The CLR also performs a sophisticated type of memory management called garbage collection, which we examine in Chapter 6.
Thus, applications written in VB.NET no longer run within the VB Runtime but execute inside the CLR. Code that runs inside the CLR is referred to as managed code. Code that executes outside its boundaries (such as that produced by VB6), is called unmanaged code. As its name suggests, the Common Language Runtime offers its services not only to VB.NET, but to any compiler capable of producing IL code. Think of the CLR as an improved and enlarged version of the VB Runtime, which is now accessible from any .NET language.
The Base Class Libraries
Built-in VB functions such as LBound and UCase are now contained in the Base Class Libraries (BCL), which are prewritten services that clients can use. It helps if you think of LBound and UCase not as VB-specific functions, but as services that were offered to your program by the VB Runtime. In this sense, the Base Class Libraries contain services that form the building blocks for applications in the .NET Framework. We will see how to call these functions in the upcoming example.
In addition to containing functions that were previously intrinsic members of the VB syntax, the BCL contains hundreds of other classes and functions for operations such as File IO, Remote Messaging, and other technologies you may have used in the past (ADO, for example).
The Base Class Libraries (which are automatically copied to your system when you install the .NET Framework, as shown in Chapter 2), exist as language-independent IL code. This means that they are accessible not only from VB.NET, but from any .NET language. Although we will call the Base Class Libraries from VB.NET throughout these CodeNotes, realize that we could do the same with C# or C++. Readers familiar with Java can think of the BCL as analogous to the Java Class Libraries.
MOVING TO VB.NET
The challenges for VB developers migrating to the .NET Framework are fourfold:
1. You must familiarize yourself with the BCL to use functions that were previously intrinsic members of the VB language. Initially, this can be frustrating, as many functions and classes in the BCL behave differently than their VB counterparts. Nevertheless, understanding the BCL is worthwhile, as it contains hundreds of services that previous versions of VB did not expose (classes for threading, linked lists, sophisticated graphics, etc.).
2. You must account for the numerous syntax changes in VB.NET. For example, the default way in which procedure parameters are passed is by value (ByVal), not by reference (ByRef).
3. You must learn Windows Forms, the new way to design desktop GUIs in the .NET Framework.
4. You must concern yourself with details that previous versions of Visual Basic usually abstracted (concepts such as threading and component versioning).