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