Data-Types Related Enhancements
Several data type concepts are incompatible in VB6 and .NET. The restrictions become increasingly complex when interactions between different data types arise (enums with integers, dates with strings, assignments between arrays with different bounds, etc.), and especially when late binding is present through generic data types like Variant, Form, Control, etc.
Several scenarios related to this data type information have been covered by conversion patterns in the VBUC. Some of them are described in this section.
Type Name conversion improvements
Additional support for the VB6 function TypeName was added in version 4.0.
The TypeName function is converted to the “is’ operator when used TypeName is used in a conditional statement.
Literal string values for types are changed to the corresponding type in .NET.
Private Sub DisplayTypeName(ByVal obj As Object)
If TypeName(obj) = "Integer" Then
If TypeName(obj) = "String" Then
If TypeName(obj) = "CommandButton" Then
If TypeName(obj) = "myClass" Then
private void DisplayTypeName( object obj)
if (obj is int)
if (obj is string)
if (obj is Button)
if (obj is myClass)
Advanced Coercion Rules
The VBUC has a module for recognizing coercion requirements and generating the appropriate conversions for the expressions involved. Some of these scenarios include:
- Integer to enumerate: when integers are used where a enum name is expected, it gets converted to the corresponding VB6 enum field, and then mapped to the semantic equivalent in .NET.
- IIF expressions: if the two expressions returned by the IIF have equivalent types, only one coercion is generated for the whole IIF invocation. Otherwise, each expression is handled in the most appropriate way to make it match the expected type.
- Several cases for conversions between types: objects to scalars (unboxing cases), dates vs. strings, arrays to arrays, fixed strings, octals and hexadecimals, etc.
Arrays Advanced Conversion
The arrays conversion can be particularly complex because they can be dimensioned and re-dimensioned several times with different bounds. The option base 0 or 1 is also a source of confusion, changing the default lower bounds that can be combined and interchanged later in some array references.
The .NET arrays must be regular, must have a lower bound equal to zero and cannot change their amount of dimensions, although they might change the dimension magnitudes. In order to resolve these inconsistencies, an analysis is performed upon all the arrays, declarations, redims and accesses, figuring out what the array bounds should be and whether they are used in a regular way or not. This information allows the VBUC to make decisions about how to declare the array in .NET, how to do the re-dimensioning processes and whether to apply corrections to the indexes used to access the array or not.
For the most difficult cases, where a reference to array is used with inconsistent dimensions or inconsistent lower bounds, the class Array is used to represent a much more flexible concept of the data type and all its references are modified accordingly.
Integer Optional Conversion to Short or Integer
The type Integer in VB6 corresponds to a 16-bits integer number, and the equivalent type in .NET would be a short. However it could be better to keep these variables as int in .NET, mainly because so many expressions return int and would need a cast before being assigned to a short variable. Some examples of these expressions are:
- Most arithmetic operations including integer values of 32-bits or less (byte, short, integer and most literals). Some very simple expressions like 3 + 2 return an integer.
- The VB6 library functions returning 16-bit Integer are often converted to equivalent .NET libraries returning 32-bit int.
The two cases above are so common that many conversions look much better when the Integer variables are converted to int. However, some cases where the behavior of overflowing operations should be preserved require that Integers are converted to the equivalent .NET 16-bit short. In those cases the necessary casting are automatically generated by the VBUC.
The VBUC can be configured to choose any of these two choices. By default it converts integer to integer.
Enums Advanced Conversion
Enum in Comparison operators
In comparisons, when one of the operands is an enum and the other operand is a numeric value corresponding to an actual value of the enumerate type, the numeric value is changed by the corresponding enum value, and it is translated to its equivalent in .NET, if there’s any. The comparison operator is also changed to either Is or Not Is.
Enums values byref
Enum values cannot be sent byref in .NET. A temporal variable is created in those cases to make the application compile and run properly.
Coercions between enum and other primitive types
A set of rules have been added to generate the appropriate transformations when using enumerates as integers or other primitive types. Other rules correct the uses of other primitives as enums, converting them to the corresponding integer value and, if possible, substituting literal values with the corresponding enumerate field.
Fixed Sized Strings
Visual basic 6.0 fixed size strings are converted using the InteropServices.MarshalAs attribute:
Dim s as string *20
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst=20) Dim s as string;
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst=20) string s;