The Book of Visual Studio .NET - A Visual Basic .NET Crash Course
The following is the first of three parts of a chapter on Visual Basic .NET from The Book of Visual Studio .NET, ISBN 1-886411-69-7 from No Starch Press written by Robert B. Dunaway. This part covers some of the differences between VB .NET from the previous version. The second part will present exception handling in Visual Basic .NET. The third part will present OOP concepts and VB .NET.
A V I S U A L B A S I C . N E T
C R A S H C O U R S E
Visual Basic (VB), for all intensive purposes, has arrived, and it's just as powerful and flexible as any other .NET language, although this may well be due more to the strength of the .NET Framework than to Visual Basic as a programming language. All versions of VB prior to Visual Basic .NET (let's refer to these versions as "classic" VB) were criticized as being non-object oriented programming languages not worthy of enterprise level or mission critical applications. However, while earlier versions lacked true full inheritance, they have been widely used to deliver mission critical applications successfully.
The fact is, a well-written Visual C++ application will nearly always out perform a Visual Basic application. But Visual Basic, while lacking in some flexibility and power, is easier to implement than Visual C++. Furthermore, because VB lacks flexibility and power, developers are less likely to create multithread problems or memory leaks at the cost of performance and stability.
This chapter provides an overview of many new Visual Basic .NET features and concepts. (For a detailed language reference, see the MSDN library online or The Book of VB .NET, .NET Insight for VB Developers.)
What's New in Visual Basic .NET?
The number of language enhancements in Visual Basic. NET nearly justifies the creation of a new language. Let's look at a few of the more visible changes.
Forcing the explicit declaration of all variables reduces potential bugs. Classic VB required an Option Explicit statement in the declaration section of code if we wanted the Visual Basic compiler to enforce variable declaration which held the potential for problems because variables were often misspelled. When using Option Explicit, an entirely new variable is created if not already declared. Unlike classic VB, Visual Basic .NET implements Option Explicit by default, preventing the accidental creation of new variables and protecting the use of declared ones.
Option Strict is similar to Option Explicit in that it tells the compiler to require a variable declaration and requires all data conversions to be explicit. In classic Visual Basic, implicit conversions are not possible when Option Strict is on. (This setting is off by default.)
Option Compare, as you might guess, determines how strings will be evaluated. The two possible parameter values are binary and text. Binary compares the literal binary values of the two values being compared. A binary compare would mean the upper- and lower- case values cannot be equal, in effect enforcing case-sensitive compares. Text allows the evaluation of two variables to be case - insensitive. Your application requirements will determine which Option Compare option you will use.
Option Base is a retired option of classic Visual Basic that allowed developers to determine whether or not arrays will be 0 or 1 based. Visual Basic .NET no longer recognizes this option and sets all arrays to base 0.
Variables in .NET come in two flavors: value types and reference types. All primitive data types with the exception of the string data type are value type variables. All classes including the string data type are reference types.
The most significant difference between the types is in how they are stored in memory. Value types are stored in a stack (which requires a smaller memory footprint), while reference types are stored in a heap.