VB Migration Partner

KNOWLEDGE BASE - Language


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 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers