Changes from Previous Versions of VB

I l @ ve RuBoard

Although the advancements to the language make the language more powerful, they do come at a price. Developers now need to design their applications with these new features in mind.

General Changes

The sections that follow present the changes to declarations, variable assignments, and scope. You can save time in writing code and debugging if you are aware of these changes.

Declarations

In previous versions of Visual Basic, multiple variable declarations are permitted within a single statement; however, if the data type of each variable is not specified, the variable defaults to a Variant :

 Dim a As Integer, b As Integer  ' a is an Integer, b is an Integer Dim x, y as Integer             ' x is a Variant, y is an Integer 

In VB.NET, repeating the data type name in the declaration is no longer necessary:

 Dim x, y as Integer  ' Both x and y are Integers 
Variable Assignments

In addition to the enhancements made to multiple variable declarations as just outlined, variable assignment has been enhanced to permit variable declaration and assignment within a single statement:

 Dim y as Integer = 3 
Scope

Variable scope has been tweaked in VB.NET as well. In VB 6.0, variables declared within a procedure had procedure scope regardless of whether the variable was declared within a block. Consider the following:

  For x = 100 To 1 Step -1     ' y has procedure scope even though it was declared inside the block.     Dim y As Integer       y = y + x Next z = y    ' y is available outside the block 

Note that y is available outside the block it was declared in. In VB.NET, y is unavailable at procedure scope.

Procedure Changes

The sections that follow present the changes to these areas: parentheses, Static , optional arguments, ByRef and ByVal , and ParamArrays ByVal .

Use of Parentheses

One of the biggest headaches (in my opinion anyway) addressed in VB.NET is the use of parentheses when calling procedures. If you recall, in Visual Basic 6.0, when making Function calls, parentheses were required around argument lists. However, when calling a Sub , parentheses were forbidden unless the Call statement was used, in which case they were required:

  Call SetCustomerLastName("Smith") SetCustomerLastName "Smith" 

You'll be glad to learn that parentheses are required around any non-empty argument list. When making calls with empty argument lists, parentheses are optional.

Static Not Supported for Procedures

Previous versions of Visual Basic allowed the shorthand practice of marking a procedure with the Static modifier. This allowed all local variables in the procedure to maintain state between calls to the procedure. VB.NET does not support this shortcut, instead requiring the developer to mark every local variable as Static as required.

Optional Arguments

Another change to procedures in VB.NET is in the way optional parameters are handled. In VB 6.0, default values were not required for optional parameters. Developers were able to detect missing parameters via the IsMissing function. In VB.NET, all optional parameters require default values. Therefore, IsMissing is no longer needed and no longer supported.

ByRef versus ByVal

Passing parameters ByRef allows the procedure to modify the value of the parameter in the calling program, potentially leading to unexpected results. In Visual Basic 6.0, parameters that were not explicitly marked ByVal defaulted to ByRef . In VB.NET, all parameters are passed ByVal unless marked ByRef . This change should be inconsequential to the studious developer who marks his parameters either ByVal or ByRef explicitly anyway to avoid confusion.

ParamArrays ByVal

In VB 6.0, procedures could accept a variable number of arguments by specifying the ParamArray keyword on the last argument for the procedure. This array was always of type Variant and passed ByRef because you could not specify ByVal .

In VB.NET, a ParamArray is always passed ByVal and all arguments in the array must be of the type specified for the ParamArray .

Return Statement

In Visual Basic 6.0, values were returned from functions simply by setting the name of the function to the value to be returned:

 Function TestFunction As Integer     TestFunction = 1 End Function 

Many developers found this syntax confusing, so Microsoft has included the new Return statement to provide an alternate means of returning a value from a function:

 Function TestFunction As Integer     Return 1 End Function 

Property Changes

The sections that follow discuss the changes in the handling of properties. The areas affected are the default properties, property declarations, and ByRef arguments.

Default Properties

Visual Basic 6.0 supported default properties for objects, which often led to shorter though more confusing code. Consider the following example:

 rs.Fields.Item("FirstName").Value = "Jack" 

In VB 6.0, because the Fields collection is the default property on the Recordset object, and because the Value property is the default property for the Item property of the collection, this assignment is functionally equivalent to the following:

 rs("FirstName") = "Jack" 

To help reduce confusion, but at the expense of shorter code, VB.NET supports only parameterized default properties:

 rs.Fields("FirstName").Value = "Jack"  '  valid because Item is parameterized 
Property Declarations

One of the more confusing aspects of previous versions of Visual Basic, especially for programmers making the jump to VB from other languages, was property declaration syntax. VB 6.0 included both Property Get and Property Set statements. Further, either Property Let or Property Set had to be used depending upon whether an object reference or default property was to be assigned. VB.NET introduces a unified property declaration syntax, which includes both a Get and a Set clause:

  Dim m_PhoneNumber As String Property PhoneNumber As Integer     Get        PhoneNumber = FormatPhone(m_PhoneNumber)     End Get     Set  ' The implicit variable Value represents value being set        m_PhoneNumber = Strip(Value)     End Set End Property  ' PhoneNumber 

As a consequence of this new unified syntax, Property Let is no longer supported.

Properties ByRef

As covered previously, VB.NET now supports only parameterized default properties. Arguments for parameterized default properties must be specified ByVal because ByRef arguments are no longer supported for parameterized queries.

Changes to Arrays

Two array- related statements were impacted by the changes in VB.NET: Size and LBound . It is important to note these changes or else your program may yield unexpected results.

Size

Visual Basic 6.0 allowed the creation of fixed-size arrays with the following syntax:

  Dim MyArray(0 to 4) As Integer 

An array created in this fashion could not be resized with the ReDim statement. VB.NET no longer supports fixed-size arrays such as this. The preceding array can be created in VB.NET with any of the following statements:

 Dim MyArray(5) As Integer Dim MyArray() As New Integer(5) { } Dim MyArray() As Integer { 0, 1, 2, 3, 4} 

Multidimensional arrays are supported and created in the following manner:

 Dim MyArray( , , ) As Integer 

Note that although arrays can no longer be fixed-size, multidimensional arrays are fixed-dimension. Although the individual dimensions can be resized with the ReDim statements, the number of dimensions cannot change.

LBound

In Visual Basic 6.0, the default lower bound for an array was 0. This could be changed to a default lower bound of 1 by using the Option Base statement. In VB.NET, however, all arrays have a lower bound of 0 and the Option Base statement is not supported.

Data Type Changes

The Variant type is now a reserved keyword, and all types derive from the Object type . Short , Long , and Integer types have also been modified in VB.NET.

Object Versus Variant

In Visual Basic 6.0, the Variant type served as the universal data type. This type supported any kind of data the developer wanted to put in a variable of this type. VB.NET introduces a universal type system in that all types derive from the Object type. The Variant type is no longer supported; however, it is still a reserved keyword.

Short , Integer , and Long

Fundamental changes have been made to the treatment of integers in VB.NET. In previous versions of Visual Basic, the Integer type was 16 bits, the Long type was 32 bits, and the language did not support a 64-bit integer type. In VB.NET this changes with the introduction of the Short type for 16-bit values, Integer now handles 32-bit values, and now VB supports 64-bit values with the Long type. These types correspond to the CLR types of System.Int16 , System.Int32 , and System.Int64 respectively.

No Fixed-Length Strings

Visual Basic 6.0 supported the concept of fixed-length strings with the following syntax:

  Dim MyString As String * 25 'Defines a fixed-length string 

In VB.NET, fixed-length strings are no longer supported. The length of the string assigned to it determines the size of the string variable.

No Currency Data Type

In an effort to handle more places on both sides of the decimal point when dealing with monetary values, the Currency data type in Visual Basic 6.0 has been retired in favor of the new Decimal data type, which maps to the System.Decimal type in the CLR.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net