VB Migration Partner

KNOWLEDGE BASE - Language


Previous | Index | Next 

[PRB] Late-bound code might throw an exception or not work as expected

If you use a DefaultMemberSupport pragma to activate support for default members in late-bound mode, VB Migration Partner renders references to default properties exposed by Object or Variant variables by wrapping the reference with a call to GetDefaultMember6 or SetDefaultMember6 helper methods (or the corresponding GetDefaultMember and SetDefaultMember methods in the VB6Helpers class, if you are converting to C#). These methods discover the name of the default member of the object passed in their first argument and call it for the actual read or write operation. The results of the discovery step are cached for each distinct .NET type, therefore the actual overhead in a real-world application is usually negligible.

In some cases, however, the discovery step may fail. This is the case with COM objects exposed by properties in late-bound mode. Consider for example this code:

        '## DefaultMemberSupport True
        Sub ShowADOProperty(ByVal obj As Object, ByVal name As String)
            ' obj is an ADO Connection, Command, or Recordset
            Debug.Print obj.Properties(name)
        End Sub

VB Migration Partner translates the code above as follows:

 
        Sub ShowADOProperty(ByVal obj As Object, ByVal name As String)
            ' obj is an ADO Connection, Command, or Recordset
            Debug.WriteLine(GetDefaultMember6(obj.Properties(name)))
        End Sub


which throws the following exception at runtime: 

Default member discovery for System.__ComObject objects isn’t supported.

The problem is: .NET reflection is unable to enumerate members of the COM object returned by the obj.Properties(n) method, and consequently is unable to determine which member is the default member. The control support library intentionally throws this exception to ensure that the developer doesn’t overlook the problem.

In most cases, the fix to this problem is quite simple: specify the name of the default property, so that VB Migration Partner won’t generate the call to GetDefaultMember6 or SetDefaultMember6 methods. For example, you can insert a ReplaceStatement pragma:

 
        Sub ShowADOProperty(ByVal obj As Object, ByVal name As String)
            ' obj is an ADO Connection, Command, or Recordset
            '##ReplaceStatement Debug.WriteLine(obj.Properties(name).Value)
            Debug.Print obj.Properties(name)
        End Sub
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