The following sections detail a number of changes that have occurred to the Visual Basic language since version 6.0. For a more complete reference to the Visual Basic language changes, look for the topic "Language Changes in Visual Basic" in the Visual Basic .NET Help system, which can be found in the Help index under "Visual Basic/changes to." Array Bounds When you declared an array in VB6, the default behavior was for the lowest-numbered element of the array to be element 0. In other words, declaring an array with the line Dim MyArray(3) As Integer created an array containing four elements: MyArray(0), MyArray(1), MyArray(2), and MyArray(3). However, you could use the statement Option Base to cause arrays to begin with element 1 instead of element 0; if Option Base 1were used, Dim MyArray(3) As Integer would lead to three elements, not four. In Visual Basic .NET, the Option Base statement is no longer supported; all arrays begin with element 0. Changing the Size of an Array In VB6, you could specify the upper and lower limits of an array with a statement like Dim Sales(0 to 4) As Single; the size of such a fixed array could not be changed later with the ReDim statement. Visual Basic .NET does not support fixed-size arrays. Any array can be resized with the ReDim statement. For example, if you have declared an array named Sales that can contain four elements, and it needs to be expanded to allow for up to ten elements, the following line of code will increase its upper bound: ReDim Sales(10) Use of the ReDim Statement Unlike VB6, Visual Basic .NET does not allow you to use ReDim in the initial declaration of an array. A variable array must be declared with a Dim statement (or equivalent, such as a Public statement) before using ReDim on that array. Fixed-Length Strings Visual Basic .NET nolonger supports fixed-length strings. Declaring a VB6-style fixed-length string with a statement like Dim sLastName As String * 20 is no longer allowed. All strings are now declared as variable-length strings (Dim sLastName As String). In this case, the sLastName variable can contain a string of any length. Changes to Integer Data Types Visual Basic .NET has modified the Integer data types to more closely match the data types supported by the CLS. The following mini-table summarizes the old and new Integer types: VB6 Type | VB .NET Type | Range of Values |
---|
Integer | Short | 32,768 to 32,768 | Long | Integer | 2,147,483,648 to 2,147,483,647 | (None) | Long | 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Variant Data Type In VB6, the universal data type (which could store data of any type) was Variant. In Visual Basic .NET, Object is now the universal data type. Currency Data Type Visual Basic .NET no longer supports the Currency data type. The new Decimal data type, which is part of the CLS, is recommended for storage of money amounts. The Decimal data type provides greater precision on both sides of the decimal point than the old Currency data type; in fact, up to 28 total places of precision are possible. Date Data Type In VB6, the Date data type stored a date using a Double (four-byte) type. Visual Basic .NET supports the CLS DateTime data type, which stores dates as an eight-byte Integer. Property Procedures When declaring PROPERTY procedures for user-defined classes in VB6, you created separate Property Get, Property Let, and Property Set procedures. Visual Basic .NET introduces a more streamlined single Property procedure, which contains Get and Set portions that do the same job as their respective VB6 counterparts. See Chapter 16, "Creating Your Own Windows Controls," for more information. Type Statements Replaced by the Structure Construct In VB6, you could create a user-defined type with a Type...End Type construct. This construct is no longer supported. Instead, in Visual Basic .NET, you use the Structure...End Structure construct to define structures and user-defined types. Declaring Multiple Variables In VB6, if you declared multiple variables on the same line, you had to declare a type for each variable; otherwise, untyped variables defaulted to Variant type. In other words, the statement Dim a, b, c As Integer would create two Variants named a and b and a single Integer named c. In Visual Basic .NET, you can have multiple declarations on the same line without repeating the type. That is, Dim a, b, c As Integer will create three Integer variables named a, b, and c. You can still declare variables of two different types on the same line. Dim a, b As Integer, c as String would give you two Integer variables named aand b and a single String variable named c. Block-Level Variable Scope Visual Basic .NET introduces a new block-level variable scope. That is, if you want to have a variable available only within a block-type construct, such as a For...Next loop or an If...End If block, you can declare that variable inside the block, and the variable will exist only within that block. For example, the variable sMsg declared inside the following If...End If block would only be available within the block: If x > 120 Then Dim sMsg As String sMsg = "Please enter a smaller value." MessageBox.Show(sMsg) End If However, the following code is invalid, as it tries to utilize the sMsg variable outside the If...End If block: If x > 120 Then Dim sMsg As String sMsg = "Please enter a smaller value." End If MessageBox.Show(sMsg) Initializing Variables when Declared In Visual Basic .NET, you can now set initial values for variables as you declare them. For example, the following variable declaration will create a variable named sLastName and set its initial value to Fortner, which had to be done on a separate line in VB6: Dim sLastName As String = "Fortner" Changes to While...Wend The VB6 While…Wend construct, which loops through code statements as long as the test condition is true, still exists in Visual Basic .NET, but the Wend keyword has been replaced by End While. Wend is no longer supported. Passing Arguments to Sub Procedures In VB6, you could pass arguments to a Sub procedure without parentheses if you did not use the Call keyword, as in ProcessEmployee "Eric Burton", 99, although parentheses were always required when calling a Function. Visual Basic .NET always requires the use of parentheses, as in ProcessEmployee("Eric Burton", 99). Returning Values from Function Procedures In VB6, you returned values from a Function procedure by setting the name of the procedure equal to the return value, as in the following example: Function AddNums(ByVal a As Single, ByVal b As Single) Dim x As Single x = a + b AddNums = x End Function While this technique still works, the preferred method in Visual Basic .NET is to use the Return statement to set the return value of the function, simultaneously returning control to the calling code: Function AddNums(ByVal a As Single, ByVal b As Single) Dim x As Single x = a + b Return x End Function Parameter Passing Is Now ByVal Previously, when passing parameters to procedures, they were assumed to be passed by reference (ByRef) unless you included the ByVal keyword. In Visual Basic .NET, parameters are now passed by value (ByVal) by default. This helps protect the parameters from being changed by the called procedures. Of course, if you want a procedure to be able to change the value of a passed parameter, you can use the ByRef keyword to explicitly pass parameters by reference. Changes to Project Files When you saved a project in VB6, each form was stored in a file with a .frm extension, classes were stored in .cls files, the overall project was stored as a .vbp file, and groups (multiple projects) were stored in .grp files. In Visual Basic .NET, forms (as well as other classes) are stored in files with an extension of .vb. The filename extension for project files is .vbproj. Solutions (containers for related projects) are stored in .sln files. In addition to these basic project components, Visual Studio .NET works with several other file types as well. See the Help system topic "File Types and File Extensions in Visual Studio. NET" for a more thorough listing of supported file types. |