Previous | Index | Next 

[HOWTO] Reduce compilation errors caused by late binding or undetected conversions

VB Migration Partner is able to account for properties that return different types in VB6 and VB.NET. For example, VB6 color properties are 32-bit integers, whereas .NET color properties are System.Drawing.Color values, therefore the converted code includes calls to methods that perform the conversion:

        ' VB.NET         
        txtName.ForeColor = FromOleColor6(VBRUN.ColorConstants.vbRed)
        Dim color As Integer = ToOleColor6(txtName.ForeColor)
        // C#         
        txtName.ForeColor = VB6Helpers.FromOleColor(VBRUN.ColorConstants.vbRed);
        int color = VB6Helpers.ToOleColor(txtName.ForeColor);

where the FromOleColor6 and ToOleColor6 wrapper methods are defined in the CodeArchitects.VBLibrary DLL.

However, if the control that exposes the color property is accessed in late-bound mode, VB Migration Partner can’t detect that the property being read from or written to is a color property, therefore the code generator can’t include calls to wrapper methods. Consider the following VB6 code:

           
        Sub SetForeColor(ByVal ctrl As Control, ByVal color As Long)
            ctrl.ForeColor = color
        End Sub

        Function GetForeColor(ByVal ctrl As Control) As Long
            GetForeColor = ctrl.ForeColor
        End Function

and the migrated .NET code:

        ' VB.NET         
        Sub SetForeColor(ByVal ctrl As Object, ByVal color As Integer)
            ctrl.ForeColor = color
        End Sub

        Function GetForeColor(ByVal ctrl As Object) As Integer            
            ' UPGRADE_WARNING (#0354): Unable to read default member of symbol 
            ' 'ctrl.ForeColor'. Consider using the GetDefaultMember6 helper method.            
            Return ctrl.ForeColor
        End Function
        // C# (assumes that UseDynamic pragma has been used)
        public void SetForeColor(dynamic ctrl, int color)
        {
	ctrl.ForeColor = color;
        }

        public int GetForeColor(dynamic ctrl) 
        {
            // UPGRADE_WARNING (#0354): Unable to read default member of symbol 
            // 'ctrl.ForeColor'. Consider using the GetDefaultMember6 helper method.
            return ctrl.ForeColor;
         }

As you can expect, the SetForeColor method throws a TypeMismatch exception, when it tries to assign a 32-bit integer to the ForeColor property; likewise, the GetForeColor method throws a TypeMismatch when converting the ForeColor property to an integer.

You can use two simple techniques to avoid such an issue. In the first technique, you add an AssumeType pragma in the original code, to inform VB Migration Partner that the late bound variable is going to store a reference to a control. You don’t need to specify the exact kind of control that the variable will store (if you knew it, there would be no need to use late binding), it suffices that the pragma points to a VB6 control:

           
        Sub SetColors(ByVal ctrl As Object, ByVal fore As Long, ByVal back As Long)
            '## ctrl.AssumeType VB.Label	
            ctrl.ForeColor = fore
            ctrl.BackColor = back
        End Sub

In the second technique, you use one or more of the “dummy” methods gathered in the VBMigrationPartner_Support module. This file contains a VB6 module that defines many of the special methods defined in the CodeArchitects.VBLibrary DLL, so that you can invoke them from your VB6 code. Here’s how you can edit the original VB6 code to use these special methods:

           
        Sub SetColors(ByVal ctrl As Object, ByVal fore As Long, ByVal back As Long)
            ctrl.ForeColor = FromOleColor6(fore)
            ctrl.BackColor = FromOleColor6(back)
        End Sub

The above code migrates to VB.NET or C# correctly and runs as expected.

 

Previous | Index | Next