Understanding Language and Syntax Changes
To migrate Visual Basic 6.0 applications to Visual Basic .NET, it is essential to understand the differences between Visual Basic 6.0 and Visual Basic .NET, discussed in the following subsection.
Visual Basic 6.0 languages support a data type called as variant. The variant is a flexible data type; it can accommodate all other types. If a variable is not declared with any type, it is regarded as a variant type variable. Visual Basic bases the type of the variant variable on its contents. For example, if the variant variable contains a string, Visual Basic handles the variable as a string type variable. If it contains an integer, it is handled as an integer type variable. Consider that there are multiple variables declared on one line as shown in the following code snippet.
Dim Counter1, Counter2, Counter3 As Integer
Here only the variable Counter3 is declared to be of type Integer. The other two variables Counter1 and Counter2 are of type variant.
In Visual Basic .NET, the variant data type is not supported. All the variables of variant data type get converted to object type in the Visual Basic .NET code. Object is a generic class in the Microsoft .NET Framework class library from which all other classes are derived.
In Visual Basic 6.0, the currency data type is used for performing financial calculations. currency types are not supported in Visual Basic .NET. During the upgrade process, the upgrade wizard will change all currency data types to decimal type. However, to smooth the migration process all currency variables in Visual Basic 6.0 applications can be converted to double type before upgrading to Visual Basic .NET, as the following code snippet demonstrates:
Dim Sal_Amount As Currency
In this case, it is recommended to change the above variable declaration to double type:
Dim Sal_Amount As Double
If the Visual Basic 6.0 application uses the FormatCurrency function, the application is upgraded and works well in Visual Basic .NET with the help of the Microsoft.VisualBasic.Compatibility class.
In Visual Basic 6.0 a date variable is stored internally in double format and all the manipulations on double format are applicable to the date type. Hence double type of data has been frequently used for date manipulations. In Visual Basic .NET, double type variables cannot be used for date manipulation.
Fixed-length strings are not supported in Visual Basic .NET. In Visual Basic .NET fixed-length strings cause a problem when used in user-defined types such as structures. So it is recommended to use strings instead of fixed-length strings.
User-defined types can be defined in a Visual Basic application as shown in this code snippet:
Type Customer Customer_ID As Integer Customer_Name As String End Type
In Visual Basic .NET, the Type keyword is not supported. Structure is the keyword for declaring user-defined types in Visual Basic .NET as follows:
Structure Customer Customer_ID As Integer Customer_Name As String End Structure
Visual Basic .NET also supports access modifiers for the members of the structure. Access modifiers like public, private, and friend can be used with the members of a structure. Many of the Windows API function calls require the data types to be passed as parameters. Thus, when these function declarations are upgraded to Visual Basic .NET, they will get converted into equivalent structures.
In Visual Basic .NET, user-defined types are called structures. Structures are in many ways similar to a class. Structures can have constructors, methods, properties, and so on. Structures, however, do not support inheritance. Also, structure variables are value type variables, and they do not use heap memory. A structure inherits from the System.ValueType object. Classes, on the other hand, are reference type variables. Instances of classes are normally allocated on heap memory. Classes support inheritance.
Even though user-defined types such as classes and structures are supported in Visual Basic .NET, fixed-length strings in user-defined types are not supported. In such cases, data marshalling techniques must be used in Visual Basic .NET.
Variants are initialized to Empty, which automatically gets converted to zero when used in a numeric expression, or to an empty string when used in a string expression. Empty is not supported in Visual Basic .NET. Object type variables are initialized to Nothing in Visual Basic .NET.
Null and Null Propagation
Null values indicate that a variable contains no valid data. If any part of an expression evaluates to null, the entire expression evaluates to a null. Null propagation is not supported in Visual Basic .NET. Normally null values are obtained when database operations are involved. Because null propagation is not supported, Visual Basic .NET offers a database-programming model, which test fields explicitly for null before retrieving their values. Variants containing null are marshalled into the CLR as objects of type DBNull.
Def<Type> statements are used to set the default data type for variables in Visual Basic 6.0. The following code snippet shows an example of the DefInt type:
DefInt A-D Private Sub Form_Click() AVariable = 1 BVariable = "Error"'this will give an error MsgBox (AVariable) MsgBox (BVariable) End Sub
The DefType statements are used at the module level. In the preceding code snippet, all variables that begin with A, B, C, and D are defined to be of type integer. The sample code gives an execution error when the string "Error" is assigned to the variable BVariable, which is of integer type variable. This type of data type definition is not supported in Visual Basic .NET.
Scope of Local Variables
In Visual Basic 6.0, if a variable is declared in a block, it is also visible outside the scope of the block, and the Visual Basic 6.0 code can use the value inside the variable. So the following code is valid in Visual Basic 6.0:
Do Dim iLoopCounter As Long iLoopCounter = iLoopCounter + 1 Loop Until iLoopCounter > 20 MsgBox(iLoopCounter)
This type of code is not supported in Visual Basic .NET. If a variable needs to be used outside a block in Visual Basic .NET, it needs to be declared outside of the block. If the preceding code is written in Visual Basic .NET, it will give a compilation error that the variable iLoopCounter has not been declared.
There is a very huge difference in the way objects are cleared from memory between Visual Basic 6.0 and Visual Basic .NET. In Visual Basic 6.0, the COM reference-counting mechanism is used to free up the object instances. When objects are not in cycles, reference counting will immediately detect when an object is no longer being used and will run its termination code.
In Visual Basic .NET, a garbage collector does the job of cleaning up the unreferenced objects. The garbage collector is a feature of the Microsoft .NET Framework, and it is applicable for all the languages supported by the Microsoft .NET Framework.
The developer does not know when the garbage collector will collect the unreferenced objects. This is known as undeterministic garbage collection. The garbage collector also compacts the memory that is in use. The .NET Framework provides a class called GC that contains a method Collect. Developers can directly call this method to force garbage collection. However, calling the Collect method too often can cause performance issues, because the garbage collector suspends all executing threads before performing garbage collection. Hence it is better to let the garbage collector determine when to perform the garbage collection.
In Visual Basic 6.0, the Option Base statement is used to determine the lower bound if a range is not specified in the declaration. We can specify arrays with any lower bounds. This is not supported in Visual Basic .NET. All arrays in Visual Basic .NET have to start off with a base of 0 to make the language interoperable with the other languages supported in the Microsoft .NET Framework. In fact, the Option Base statement is not supported in Visual Basic .NET.
In Visual Basic 6.0, ReDim is normally used to redimension an array that has been declared earlier. If the array has not been declared earlier, it is declared with the ReDim statement. In Visual Basic .NET, the ReDim statement can be used only if the array has been declared earlier.
In Visual Basic 6.0, error handling is done with the help of On Error statements. Normally this statement is placed at the beginning of a block of code, and it handles any errors that occur in that block of code. This is not a structured way of handling errors.
Visual Basic .NET has vastly improved the error-handling techniques. In Visual Basic .NET, conditions in which code fails are known as exceptions. A block, which can throw an exception, is enclosed within a try/catch block. If the block throws an exception, it is caught by the code in the catch block. The catch block can handle the exception on its own, or it can rethrow the exception to a higher exception-handling code.
Function Return Values
To return a value from a function, in Visual Basic 6.0, the function name is set to the value that is to be returned. In Visual Basic .NET, the desired value is returned by the Return statement.
In Visual Basic 6.0, there are two forms of assignment: Let assignment (the default) and Set assignment. In Visual Basic .NET, there is only one form of assignment. x = y means to assign the value of variable or property y to the variable or property x. The value of an object type variable is the reference to the object instances, so if x and y are reference type variables, a reference assignment is performed.
In Visual Basic 6.0, two forms of procedure calls are supported, one using the Call statement, which requires parentheses around the list of arguments, and one without the Call statement, which does not require parentheses around the argument list. Visual Basic .NET makes it mandatory to have parentheses in procedure calls.
In Visual Basic 6.0, procedures can be declared with the Static keyword, which indicates that the procedure's local variables are preserved between calls. In Visual Basic .NET, the Static keyword is not supported in the procedure, and all static local variables need to be explicitly declared with the Static statement.
In Visual Basic 6.0 the developer can define the parameter as either ByRef (by reference) or ByVal (by value). If the developer does not specify it, the parameter passing defaults to ByRef.
This has changed to ByVal in Visual Basic .NET: All parameters are ByVal by default. So the developer has to declare a subroutine or function explicitly as ByRef to get it to change a parameter.
In Visual Basic 6.0, optional Variant parameters with no default values are initialized to a special error code that can be detected by using the IsMissing function. Visual Basic .NET requires that a default value be specified for all optional parameters.
In Visual Basic 6.0, when variables are passed to a ParamArray argument, they can be modified by the called function. However when variables are passed to a ParamArray argument in Visual Basic .NET, they cannot be modified by the called function. ByRef ParamArray elements are not supported.
In Visual Basic 6.0, a parameter of a native API can be declared As Any, in which case a call to the native API could pass in any data type. This was supported to enable calling APIs whose parameters supported two or more data types.
In Visual Basic .NET, overloaded Declare statements have to be defined, and it allows a native API to be called with two or more data types.
In Visual Basic 6.0, the Implements statement is used to specify an interface or class that will be implemented in the class module. The Implements statement in Visual Basic .NET is different than in Visual Basic 6.0 with respect to the following points:
In Visual Basic 6.0, the Implements statement is used to indicate that the current class is implementing some other class. In Visual Basic .NET, the Implements keyword can be used only for interfaces.
Every method that implements an interface method requires an Implements keyword at the end of the method declaration statement. This will specify what interface method it implements.
In Visual Basic 6.0, the Get, Let, and Set property functions for a specific property can be declared with different levels of accessibility. For example, the property Get function can be Public, whereas the property Let is Friend. In Visual Basic .NET, the Get and Set functions for a property must both have the same level of accessibility.
In Visual Basic 6.0, any member can be marked as a default for the class. All built-in controls of Visual Basic 6.0 have some default properties. In Visual Basic .NET, only those properties that take parameters can be marked as default. It is common for those properties with parameters to be indexers into a collection.
In Visual Basic 6.0, enumeration constants can be referenced without qualification. In Visual Basic .NET, enumeration constants can be referenced without qualification only if an Import for the enumeration is added at the file or project level.
In Visual Basic 6.0, While statements can end with a Wend statement. However, in Visual Basic .NET, to be consistent with other block structures, the terminating statement for While is End While.
On..GoTo and On..GoSub
In Visual Basic 6.0, statements such as On <Expression> GoTo and On <Expression> GoSub are supported. Because these are unstructured ways of coding, they are not supported in Visual Basic .NET.
In Visual Basic 6.0, the GoSub Return statement is used to branch to a particular subroutine and to return from a subroutine within a procedure. GoSub Return is not a structured way of coding and is not supported in Visual Basic .NET.
In Visual Basic 6.0, the LSet statement is used to left-align a string within a string variable. LSet pads a string with spaces to make it a specified length or copies a variable of one user-defined type to a variable of a different user-defined type. This is not supported in Visual Basic .NET.
VarPtr, StrPtr, and ObjPtr
In Visual Basic 6.0, VarPtr, StrPtr, and ObjPtr return the addresses of variables as integers, which can then be passed to API functions that take addresses, such as RtlCopyMemory. VarPtr returns the address of a variable, StrPtr returns the address of a string, and ObjPtr returns the address of an object. These are not supported in Visual Basic .NET.
File input and output is part of the Visual Basic 6.0 language with statements such Open, Close, Input, and Output. In Visual Basic .NET, all file input and output operations are removed from the language and the functionality has been shifted into the Microsoft .NET Framework class library. Specifically, the classes in the namespace System.IO are used for file-handling purposes in the Microsoft .NET Framework.
Visual Basic 6.0 supports one .res file per project. Visual Basic .NET has rich support for resources. Forms can be bound to retrieve resources automatically from the new .resX-formatted resource files. Any CLR class can be stored in a .resX file.
Visual Basic Forms
Visual Basic 6.0 has its own forms package for creating graphical Windows applications. Windows Forms is a new forms package for Visual Basic .NET. It resides in the System.windows.Forms namespace of the Microsoft .NET class library. Because Windows Forms is built from the ground up to target the CLR, it can take advantage of all of its features.
In Visual Basic 6.0, the caption property is used to display the text in the title bar of the form. It is also used to display the text on the controls such as Label, Menu bar, and so on. The caption property has been changed to the text property in Visual Basic .NET.
In Visual Basic 6.0, Windows Forms can use any Windows fonts. However, Visual Basic .NET can use only true type or open type fonts.
A control array is a group of controls that share the same name and type. They also share the same event procedures. A control array has at least one element and can grow to as many elements as your system resources and memory permit. Elements of the same control array have their own property settings.
In Visual Basic .NET, the Windows Form architecture natively handles many of the scenarios for which control arrays were used. For instance, in Windows Forms you can handle more than one event on more than one control with a single event handler.
A WebClass is a Visual Basic component that resides on a Web server and responds to input from the browser. A WebClass typically contains WebItems that it uses to provide content to the browser and expose events.
Web Forms is a Microsoft .NET Framework feature that can be used to create a browser-based user interface for Web applications. Visual Basic .NET has a what you see is what you get (WYSIWYG) designer for graphical Web Form creation using controls from the Toolbox. This gives Web user-interface development the same feel as Windows development. Also, when the project is built, the Internet Information Services (IIS) server does not have to be stopped and restarted to deploy the changes in the application.
ActiveX documents are Visual Basic 6.0 applications that are packaged so that they can appear within Internet browser windows. They can also be used in other containers such as Microsoft Office Binder. They offer built-in viewport scrolling, hyperlinks, and menu negotiation. ActiveX documents are not supported in Visual Basic .NET.