Several times I’ve mentioned the Type Inference feature of the Visual Basic Upgrade Companion. The main objective of the component that we call the “Typer” was to eliminate Upgrade Issue 1037 (Couldn't resolve default property of object ‘x’) that appeared then the VBUC wasn’t able to correctly determine the data type of a variable and consequently couldn’t resolve its default property.
Even though dealing with EWI 1037 was the design goal, having a typing mechanism in the VBUC allowed us to improve a great deal of transformations. One particular feature that benefitted from it was the migration of VB6 Enums. To illustrate this, let’s look at an example.
First, let’s see some VB6 code (doesn’t really do much except use Enums):
1: Public Enum Myenum
2: EnumValue0 = 0
3: EnumValue1 = 1
4: EnumValue2 = 2
5: EnumValue3 = 3
6: EnumValue4 = 4
7: EnumValue5 = 5
8: EnumValue6 = 6
9: EnumValue7 = 7
10: End Enum
11:
12: Dim enumUsage As Integer
13:
14: Public Sub MyMethod()
15: enumUsage = EnumValue1
16: enumUsage = 4
17: If enumUsage = EnumValue4 Then
18: enumUsage = EnumValue7
19: End If
20: If enumUsage = 7 Then
21: enumUsage = 5
22: End If
23: End Sub
After running the code through the VB Upgrade Companion we get this:
1: Public Enum Myenum
2: EnumValue0 = 0
3: EnumValue1 = 1
4: EnumValue2 = 2
5: EnumValue3 = 3
6: EnumValue4 = 4
7: EnumValue5 = 5
8: EnumValue6 = 6
9: EnumValue7 = 7
10: End Enum
11:
12: Dim EnumUsage As Myenum
13:
14: Public Sub MyMethod()
15: EnumUsage = Myenum.EnumValue1
16: EnumUsage = Myenum.EnumValue4
17: If EnumUsage = Myenum.EnumValue4 Then
18: EnumUsage = Myenum.EnumValue7
19: End If
20: If EnumUsage = Myenum.EnumValue7 Then
21: EnumUsage = Myenum.EnumValue5
22: End If
23: End Sub
The VBUC performs some very subtle transformations to improve the quality of the generated code. Let’s look at them in detail:
First of all, the usage pattern for the variable EnumUsage is analyzed by the Typer, and it determines that it is always used to store a value of the Myenum Enum. Thus, it infers that the correct data type should be Myenum instead of Integer, as seen in line 12:
12: Dim EnumUsage As Myenum
Since the Typer keeps track of the different ways in which a variable is used, and of the values that are assigned to it, it provides information to the VBUC so it can replace the assigned values with the corresponding enumeration. This applies even for comparisons – the code is translated from:
16: enumUsage = 4
...
20: If enumUsage = 7 Then
21: enumUsage = 5
to (notice the if in line 20):
16: EnumUsage = Myenum.EnumValue4
...
20: If EnumUsage = Myenum.EnumValue7 Then
21: EnumUsage = Myenum.EnumValue5
This is just one of the features that we have worked on over time to improve the quality of the code generated by the VB Upgrade Companion. It was built on top of the functionality provided by the Typer, and works with both VB6 built-in and user-declared enumerations. As shown in the example, this makes the code easier to maintain, and even looks as if it was written originally in .NET. :)