VB Migration Partner has demonstrated to be able to deal even with the most intricate conversion issues. Nevertheless, at times even we at Code Architects are startled by the power and flexibility that its pragmas can provide.

First, some background. The AutoNew pragma can be applied to variables declared using the "As New" syntax, to ensure that they preserve the same semantics they have in the original VB6 code. Like most pragmas, the AutoNew pragma can be applied at the project-, file-, method-, and variable-level scope. Here's an example of a variable-level scoped pragma:

    '## col.AutoNew
    Dim col As New Collection

A few days ago, one of our customers asked whether it was possible to apply the AutoNew pragma to all the variables of type Collection, but not to variables of other types. According to official docs this isn't posible, and in fact our first answer was that it was necessary to insert a distinct AutoNew pragma for each and every Collection variable. Alas, the application contained so many Collection variables that this task would have taken a lot of time and would have cluttered the original VB6 code with too many pragmas.

Then we got the "a-ha" revelation!

The PreProcess pragma gives VB Migration Partner the ability to modify the VB6 code before it is parsed, therefore it is possible to use a carefully crafted PreProcess pragma to automatically insert an AutoNew pragma immediately before the declaration of a Collection variable that uses the As New syntax. Here's the pragma you need:

'## project:PreProcess "\b(Public|Private|Dim)\s+(?<name>\w+)\s+As\s+New\s+(VBA\.)?Collection\b", "'## ${name}.AutoNew\r\n$0", True

For example, when applied to the following VB6 code:

    Dim col As New Collection
    Dim col2 As New VBA.Collection
    Dim col3 As Collection
    Dim frm As New Form1

the above PreProcess pragma transforms it into the following piece of text:

    '## col.AutoNew
    Dim col As New Collection
    '## col2.AutoNew
    Dim col2 As New VBA.Collection
    Dim col3 As Collection
    Dim frm As New Form1

When VB Migration Partner parses this code, it learns that the col and col2 variables must preserve the auto-instancing semantics. Notice that the col3 variable isn't affected because it lacks the As New clause and that the frm variable isn't affected because it isn't of Collection type.

Problem solved! Wink