VB Migration Partner

KNOWLEDGE BASE - Database and data-binding


Previous | Index | Next 

[PRB] Passing ADOX properties to ByRef argument may cause unexpected runtime exceptions

We have noticed a weird behavior with ADOX objects. For example, consider the following VB6 code:
    Sub Test(ByVal con As ADOX.Catalog)
        Dim i As Integer
        For i = 0 To con.Tables.Count – 1
            DisplayTableName con.Tables(i).Name
        Next
    End Sub

    Sub DisplayTableName(tableName As String)
        List1.AddItem tableName
    End Sub
Notice that the tableName parameter is taken by-reference, even though it isn’t assigned inside the DisplayTableName procedure. This code behaves correctly under VB6 but not under VB.NET. In fact, the ByRef argument causes the Name property of the ADOX.Table object to be reassigned on exit from the DisplayTableName procedure, which in turn causes a COMException error with the following error message:
    Table 'XYZ' already exists
In other circumstances you might have different error messages. For example, even if the assignment to the Table’s Name property is successful, such an assignment causes the Tables collection of the Catalog object to be rebuilt and the Table being modified is moved to the end of the collection itself.

If the assignment occurs inside a For Each loop, VB.NET throws an exception because a collection can’t be modified while it is being enumerated. If it occurs inside a For loop – as in previous code – it may cause certain tables to be skipped during iteration and certain other tables to be processed twice, but you get no exception and the bug might go unnoticed.

All these issues can be fixed by ensuring that the property of the ADOX object be passed using by-value rather than by-reference semantics. In most cases the receiving parameter is (implicitly) declared ByRef only because this keyword is assumed by default under VB6 and you can safely add an explicit ByVal keyword in the original VB6 code.

If the parameter does require by-reference semantics, you must ensure that you pass the ADOX property by value, as in this code:

    DisplayTableName con.Tables(i).Name & ""
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