This book is for experienced object-oriented developers. In this overview, we will take a look at some of the major language differences between Visual Basic 2008 (VB 2008), C# 3.0, and Visual Basic 6.0 (VB6). Functionally, VB 2008 and C# 3.0 are nearly identical, and you can use either language to create stable, high-performance applications in the .NET environment. You'll see the biggest differences in the syntax, which is completely different between the two languages. VB 2008 and VB6 are also vastly different from each other, and we will look at some of the overreaching differences between the two languages. Next, we will review a simple VB 2008 program to get an idea of the programmatic structure in .NET 3.5 and wrap up with a summary of what's new for current VB programmers in this latest and greatest version, VB 2008.
Differences Between VB 2008, C# 3.0, and VB6
This new version of VB has been specifically designed to target the new programming model provided by .NET 3.5. Both C# 3.0 and VB 2008 are designed to write programs that work with the .NET runtime. Whereas C# 3.0 was designed with C and C++ programmers in mind, VB 2008 is designed to be more accessible to the large base of existing VB programmers. The new language targets the .NET 3.5 programming model and is derived from previous versions of VB, but you'll find that it's quite different. The language changes are due to the fact that in order to support the framework, VB 2008 must provide more object-oriented features as well as type safety.
.NET Runtime
To understand VB development in the .NET environment, you first need to understand some components of the .NET environment and how they interact. This section summarizes how VB programs are compiled and run in .NET. The execution engine of .NET is known as the common language runtime (CLR). The CLR is primarily responsible for loading and executing code, as well as memory management, security, and handling of types.
At the top level is the VB language itself, or any language that targets the CLR, that is used to create code. The VB compiler takes the written code and generates intermediate language (IL). For example, a DLL or EXE contains IL that is understood by the CLR. Any code written to run in the CLR is known as managed code, because it runs under the control of the CLR. Managed code is an IL because it is halfway between the high-level language (VB) and the
lowest-level language (assembly/machine code).
At run time, the CLR compiles the IL into native code on the fly by using the just-in-time (JIT) compiler. The JIT compiler creates native code that is CPU-specific, so you could take the IL for a program and compile it on computers with different architectures. JIT compiling comes with its pros and cons. It may seem that an obvious disadvantage is the inefficiency of compiling code at run time. However, the JIT compiler doesn't convert all the IL into native
code; rather, it converts only the code that's executing into native code to be run. At the same time, it creates stubs to any methods not executing so that when those methods are called, the JIT compiler will compile and execute the necessary code.
An advantage of JIT compiling is that the working set of the application is reduced because the memory footprint of intermediate code is smaller. During the execution of the application, only the needed code is JIT-compiled. Unused code, such as printing code if the user never prints a document, is never JIT-compiled. Moreover, the CLR can optimize the program's execution on the fly at run time. For example, on the Windows platform, the CLR may determine a way to reduce page faults in the memory manager by rearranging compiled code in memory, and it could do all this at run time. That said, there are times when JIT compilation can be a performance drawback. In this case, you can use native image generation (NGen) to precompile IL on the machine where it's running.
The CLR replaces the traditional VB runtime and also eliminates the use of COM, DCOM, MTS, or COM+. VB applications now run in the context of the CLR, so there's no more need for the host of distributed technologies that were once so prevalent. Of course, you can still access COM components if you need to through the interop layer provided by .NET.
Note If you wish, you could actually code a program in raw IL while building it with the Microsoft Intermediate Language (MSIL) Assembler. Of course, that would be a very inefficient way to write programs, but the fact that you could do it demonstrates the cross-platform capabilities of .NET. You can convert any language that supports .NET into IL, and the CLR can understand IL from any language. From there, you can JIT-compiled the IL into native code for the particular CPU architecture on which it's running.
VB 2008 and C# 3.0
VB 2008 and C# 3.0 are nearly identical in what you can accomplish with them; you can use either language to access all the classes and functions provided by the .NET Framework. Essentially, you can do everything in VB 2008 that you can do in C# 3.0, although one language may provide a
more streamlined approach than the other, depending on what you're trying to accomplish.
When discussing VB 2008 and C# 3.0, it's easier to talk about their differences than their similarities. In the latest release, both languages add new language extensions, including Language-Integrated Query (LINQ), query comprehensions, anonymous types, lambda expressions, and extension methods. Later chapters cover each of these topics extensively.
The only real differences are that currently C# 3.0 provides the ability to write unsafe code, and VB 2008 provides late binding. Unsafe code in C# 3.0 is that which uses pointers to manage and access memory directly. You might need to use unsafe code for performance reasons or to write low-level Win32 API calls, to the ReadFile function, for example. Unsafe code may be warranted at times, but it isn't recommended because it cannot be verified to be safe and creates objects in memory that cannot be removed by garbage collection. Late binding was kept in the VB language to maintain compatibility with previous versions. Late binding allows you to create a variable as a type of Object and then later assign it to a variable either implicitly by assigning it to an object instance or using the CreateObject function. Early binding is preferred over late binding because late binding incurs a performance hit, and the compiler can't report errors at compilation time, which means you'll receive them at run time instead.
VB 2008 and VB6
The Visual Basic language, beginning with VB .NET 2002, was completely overhauled to support the CLR, so it bears only a passing resemblance to VB6. This results in a steep learning curve for many developers, but the trade-off is a whole new programming model that, when applied correctly, provides a better development environment and better software. In addition to a whole new compilation model, the CLR provides improved memory management, an
object-oriented environment, and type safety. One of the biggest changes to the VB language is that it is now truly object-oriented. This means that each and every object (including data types) derive from System.Object. Instead of the VB runtime and the Win32 API, you now have the entire Base Class Library (BCL) of objects to work with. The challenge for programmers new to .NET is navigating this vast library and knowing where to find the classes they need. Becoming familiar with the .NET Framework will make you a better and faster programmer. The BCL is a subset of the .NET Framework and provides types that can be utilized by any code written to run in the CLR. The BCL includes the System.Collections, System.Diagnostics, System.IO,System.Registry, System.Globalization, System.Reflection, System.Text, and System.Drawing namespaces, to name a few.