Upgrading Commonly-Used Visual Basic 6.0 Language Features
In addition to commonly used objects as described in Chapter 7, it may be necessary to upgrade certain Visual Basic 6.0 language features that have become obsolete and are not directly supported in .NET. This chapter describes the unsupported language features and the alternate functionality you can use to achieve the same effect in the new platform, for instance, how to upgrade a VB6 default property, collection class or typeof to .NET, or how does the VB.NET compatibility library work. Remember, there may be more than one way to solve problems that arise from these issues, so if a particular suggestion does not suit you, try searching the Web for a more appropriate solution.
How does the Upgrade Wizard convert default properties to .NET?
Default properties in Visual Basic 6.0 allow you to simplify your code by specifying a property that is set or retrieved by using only the object name. Thus, a commonly used property for an object can be referenced in code using only the object name instead of the fully qualified object.property name. However, default properties are no longer supported in the Visual Basic .NET.
When using the upgrade wizard to upgrade code with default properties, it will modify code by inserting the property name wherever a default property is used. This happens only if the wizard is able to determine which property is the default property for each object reference. If the upgrade wizard cannot determine the property being referenced as a default property, as is the case with late-bound objects, the wizard will leave your code unchanged but insert a warning comment indicating the issue needs to be resolved.
What happens when the Upgrade Wizard is applied to VB 6.0 Collection Classes?
Collection classes allow you to group objects, typically to allow for better organization and processing of related objects. It is possible to build your own custom collections, often by using the Visual Basic 6.0 Class Builder.
Upgrading custom collections created with the class builder can be accomplishedwith the upgrade wizard. However, the number of manual adjustments that will have to be made will depend on any modifications made to the collection code beyond what was generated by the class builder.
For collections with little or no custom code, the upgrade wizard will do most of the upgrade work for you, inserting only a ToDo comment to flag the need to review the resulting code. With simple modifications, the final result will behave as the original collection.
How does the upgrade wizard deal with commonly-used functions and objects?
Visual Basic .NET provides a compatibility library for many Visual Basic 6.0 functions and objects, to ease upgrade while maintaining the functional behavior of your application. Whenever appropriate, the upgrade wizard will substitute a function in your original code with the Visual Basic .NET equivalent available in the compatibility library. The upgrade wizard will also add a reference to the Visual Basic 6.0 compatibility library namespace (Microsoft.VisualBasic.Compatibility) to your solution whenever any such substitutions are made.
The compatibility library will minimize the amount of effort required to upgrade. This library attempts to duplicate the functional behavior of the original Visual Basic 6.0 functions, but the functionality upgraded to members of that library should be considered as the initial attempt to upgrade that will leverage the application toward a more extensive assimilation to Visual Basic .NET. Some of the functionality in the compatibility library is also available in the Microsoft .NET Framework, allowing the possibility of different upgrade options that you should evaluate according to the available time and resources. In general, upgrading functionality to the .NET Framework instead of using the compatibility library will generate an application with an improved design and usage of references because the quantity of redundant components will be reduced.
What are the major changes implemented in Visual Basic .NET for the TypeOf clause?
In Visual Basic 6.0, the TypeOf clause can check an object’s type at run time. This clause is used in a decision statement (If) to select a course of action based on the type of an object. The clause evaluates to True when the object is of the specified type; otherwise, it evaluates to False. Visual Basic .NET supports the TypeOf keyword, but its behavior has changed.
- In Visual Basic 6.0, user-defined types are considered object types for use in the TypeOf clause. In Visual Basic .NET, user-defined types (otherwise known as structures) are no longer considered object types and therefore cannot be used in a TypeOf clause.
- In Visual Basic 6.0, inheritance is only possible by implementing interfaces. In Visual Basic .NET, true inheritance is supported; this allows classes to inherit from other classes. This impact affects the TypeOf clause because it will base the answer on the type of the underlying object that a variable refers to, not the type of the variable.
How are references to Visual Basic 6.0 Enum Values upgraded?
Upgrading code that refers to any predefined Visual Basic 6.0 enumerator values requires special care. The upgrade wizard may replace the enumerator reference with a new object in Visual Basic .NET that represents the same value, but it is not a simple enumerator value. In some cases, you may encounter a compiler error when upgrading code that references Visual Basic 6.0 predefined enumerator values.
For example, because the declarations for some color objects are initialized with values that are not constants, literals, or enumerations, each line results in the compiler error “Constant expression is required.” To fix these issues, you can define your own constant values and replace the use of the Visual Basic .NET property for each enumerator with your constants, or alternatively, you can use nonconstant values directly.
How do I deal with changes to Visual Basic 6.0 Arrays in Visual Basic .NET?
In Visual Basic 6.0, you can specify the starting index for arrays using the Option Base declaration or by specifying the index range in the array declaration. In Visual Basic .NET, all arrays have a starting index of 0. As a result, the Option Base 1 declaration is not supported, nor is specifying an array index range.
For example, all array declarations must have a starting index of 0. Thus, code in which array declarations have a non-zero starting index must be adjusted. Applying the upgrade wizard to code with such declarations will result in UPGRADE_WARNING comments inserted in the upgraded code wherever the declarations appear. Furthermore, some declarations may be changed and others left unchanged depending on the starting index in the original code.
Does Visual Basic .NET still maintains compatibility with language features from older versions of VB?
In Visual Basic .NET, some of these features have been eliminated because they lead to confusing and cryptic coding styles and are not appropriate for the kind of largescale distributed application development that is becoming dominant in today’s business environment.
The following Visual Basic 6.0 language features are upgraded by the upgrade wizard but are not recommended for use in your applications: DefInt, DefStr, DefObj, DefDbl, DefLng, DefBool, DefCur, DefSng, DefDec, DefByte, DefDate, DefVar, GoTo, Imp, and Eqv. DefType statements will be automatically upgraded by the upgrade tool and comments will be generated in the target code to indicate that DefType statements were removed and explicit declarations of the affected variables were added. The upgrade wizard will handle this situation whenever possible, but it is recommended that you remove deprecated features that are not supported by the Visual Basic .NET language from your original source code prior to upgrade.
The following features are not upgraded by the upgrade wizard, and the developer must eliminate them: GoSub…Then, LSet, VarPtr, ObjPtr, StrPtr, and Null and Empty.