WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

VB .NET Language in a Nutshell: What's New and Different in VB .NET
Pages: 1, 2, 3, 4, 5

Language Changes for VB .NET

In this section, we outline the changes made to the Visual Basic language from Version 6 to Visual Basic .NET. These language changes were made to bring VB under the umbrella of the .NET Framework and allow a Common Language Runtime for all languages in Visual Studio .NET. In some sense, the changes made to the VB language were to bring the language component of VB (as opposed to the IDE component) more in line with the C# language (which is a derivative of C and C++).



Since we assume in this chapter that you are familiar with VB 6, we will not necessarily discuss how VB 6 handles a given language feature, unless the contrast is specifically helpful. You can assume that if a VB .NET language feature is described in this chapter, there has been a change in its behavior since VB 6.

Data Types

There have been fundamental changes to data types in VB .NET, which we outline in this section. The most important change is that all of the languages under the .NET umbrella (VB, C#, and Managed C++) now implement a subset of a common set of data types, defined in the .NET Framework's Base Class Library (BCL). We say subset because VB .NET does not implement all of these data types. In any case, each data type in the BCL is implemented either as a class or as a structure (which is similar to a class) and, as such, has members. The VB .NET data types are wrappers for the corresponding BCL data type. While this need not concern the VB programmer, it can be used in some cases to expose a bit more functionality from a data type. For more on data types, see Chapter 2.

Now let us consider the specifics.

Strings

As you may know, in VB 6, strings were implemented as a data type known as the BSTR. A BSTR is a pointer to a character array that is preceded by a 4-byte Long specifying the length of the array. In VB .NET, strings are implemented as objects of the String class, which is part of the .NET Framework's System namespace.

One consequence of this reimplementation of strings is that VB .NET does not have fixed-length strings, as does VB 6. Thus, the following code is illegal:

Dim Name As String * 30

Note, though, that strings in VB .NET are immutable. That is, although you do not have to declare a string's length in advance, once a value is assigned to a string, its length cannot change. If you change that string, the .NET Common Language Runtime actually gives you a reference to a new String object. (For more on this, see Chapter 2.)

Integer/Long data type changes

VB .NET defines the following signed-integer data types:

Short
The 16-bit integer data type. It is the same as the Int16 data type in the Base Class Library.
Integer
The 32-bit integer data type. It is the same as the Int32 data type in the Base Class Library.
Long
The 64-bit integer data type. It is the same as the Int64 data type in the Base Class Library.

Thus, with respect to the changes from VB 6 to VB .NET, we can say:

  • The VB 6 Integer data type has become the VB .NET Short data type.
  • The VB 6 Long data type has become the VB .NET Integer data type.

Variant data type

VB .NET does not support the Variant data type. The Object data type is VB .NET's universal data type, meaning that it can hold data of any other data type. According to the documentation, all of the functionality of the Variant data type is supplied by the Object data type.

We cannot resist the temptation to add that there are several penalties associated with using a universal data type, including poor performance and poor program readability. Thus, while VB .NET still provides this opportunity through the Object data type, its use is not recommended whenever it can be avoided.

The VarType function, which was used in VB 6 to determine the type of data stored in a variant variable (that is, the variant's data subtype), now reports the data subtype of the Object type instead. In addition, the TypeName function, which can be used to return a string that indicates the data type of a variable of type Object, is still supported.

Other data type changes

Here are some additional changes in data types:

  • The Deftype statements (DefBool, DefByte, etc.), which were used to define the default data type for variables whose names began with particular letters of the alphabet, are not supported in VB .NET.
  • The Currency data type is not supported in VB .NET. However, in VB .NET, the Decimal data type can handle more digits on both sides of the decimal point, and so it's a superior replacement. In VB .NET, Decimal is a strong data type; in VB 6, it was a Variant subtype, and a variable could be cast as a Decimal only by calling the CDec conversion function.
  • In VB 6, a date is stored in a Double format using four bytes. In VB .NET, the Date data type is an 8-byte integer data type whose range of values is from January 1, 1 to December 31, 9999.

Variables and Their Declaration

The changes in variable declarations and related issues are described here.

Variable declaration

The syntax used to declare variables has changed for VB .NET, making it more flexible. Indeed, these are long awaited changes.

In VB .NET, when multiple variables are declared on the same line, if a variable is not declared with a type explicitly, then its type is that of the next variable with an explicit type declaration. Thus, in the line:

Dim x As Long, i, j, k As Integer, s As String

the variables i, j, and k have type Integer. (In VB 6, the variables i and j would have type Variant, and only the variable k would have type Integer.)

When declaring external procedures using the Declare statement, VB .NET does not support the As Any type declaration. All parameters must have a specific type declaration.

Variable initialization

VB .NET permits the initialization of variables in the same line as their declaration (at long last). Thus, we may write:

Dim x As Integer = 5

to declare an Integer variable and initialize its value to 5. Similarly, we can declare and initialize more than one variable on a single line:

Dim x As Integer = 6, y As Integer = 9

Variable scope changes

In VB 6, a variable that is declared anywhere in a procedure has procedure scope; that is, the variable is visible to all code in the procedure.

In VB .NET, if a variable is defined inside a code block (a set of statements that is terminated by an End..., Loop, or Next statement), then the variable has block-level scope; that is, it is visible only within that block.

For example, consider the following VB .NET code:

Sub Test(  )
   If x <> 0 Then
      Dim rec As Integer
      rec = 1/x
   End If
 
   MsgBox CStr(rec)
End Sub

In this code, the variable rec is not recognized outside the block in which it is defined, so the final statement will produce an error.

It is important to note that the lifetime of a local variable is always that of the entire procedure, even if the variable's scope is block-level. This implies that if a block is entered more than once, a block-level variable will retain its value from the previous time the code block was executed.

Arrays and array declarations

VB 6 permitted you to define the lower bound of a specific array, as well as the default lower bound of arrays whose lower bound was not explicitly specified. In VB .NET, the lower bound of every array dimension is 0 and cannot be changed. The following examples show how to declare a one-dimensional array, with or without an explicit size and with or without initialization:

' Implicit constructor: No initial size and no initialization
Dim Days(  ) As Integer
 
' Explicit constructor: No initial size and no initialization
Dim Days(  ) As Integer = New Integer(  ) {}
 
' Implicit constructor: Initial size but no initialization
Dim Days(6) As Integer
 
' Explicit constructor: Initial size but no initialization
Dim Days(  ) As Integer = New Integer(6) {}
 
' Implicit constructor: Initial size implied by initialization
Dim Days(  ) As Integer = {1, 2, 3, 4, 5, 6, 7}
 
' Explicit constructor, Initial size and initialization
Dim Days(  ) As Integer = New Integer(6) {1, 2, 3, 4, 5, 6, 7}

Note that in the declaration:

Dim ArrayName(X) As ArrayType

the number X is the upper bound of the array. Thus, the array has size X+1.

Multidimensional arrays are declared similarly. For instance, the following example declares and initializes a two-dimensional array:

Dim X(,) As Integer = {{1, 2, 3}, {4, 5, 6}}

and the following code displays the contents of the array:

Debug.Write(X(0, 0))
Debug.Write(X(0, 1))
Debug.Writeline(X(0, 2))
Debug.Write(X(1, 0))
Debug.Write(X(1, 1))
Debug.Write(X(1, 2))
 
123
456

In VB .NET, all arrays are dynamic; there is no such thing as a fixed-size array. The declared size should be thought of simply as the initial size of the array, which is subject to change using the ReDim statement. Note, however, that the number of dimensions of an array cannot be changed.

Moreover, unlike VB 6, the ReDim statement cannot be used for array declaration, but only for array resizing. All arrays must be declared initially using a Dim (or equivalent) statement.

Structure/user-defined type declarations

In VB 6, a structure or user-defined type is declared using the Type...End Type structure.

In VB .NET, the Type statement is not supported. Structures are declared using the Structure...EndStructure construct. Also, each member of the structure must be assigned an access modifier, which can be Public, Protected, Friend, ProtectedFriend, or Private. (The Dim keyword is equivalent to Public in this context.)

For instance, the VB 6 user-defined type:

Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

is defined in VB .NET as:

Structure RECT
   Public Left As Long
   Public Top As Long
   Public Right As Long
   Public Bottom As Long
End Structure

Actually, the VB .NET Structure type is far more reaching than its VB 6 user-defined type predecessor. Indeed, structures have many properties in common with classes; for instance, structures can have members (properties and methods). We discuss structures in detail in Chapter 2.

Pages: 1, 2, 3, 4, 5

Next Pagearrow