Previous | Index | Next 

[HOWTO] Exclude portions of VB6 code from the migration process and replace it with custom .NET statements

It is often necessary to exclude a group of VB6 statements from the migration process, or to specify a different set of statements for the .NET application. VB Migration Partner gives you many options for excluding a portion of VB6 code or specifying a different set of statements.

The simplest way to hide a group of VB6 statements from the migration process is by preceding them with a ParseMode Off pragma, as in this example:

'##ParseMode Off
' (...these statements won’t be output as .NET code...)
' ...
'##ParseMode On
' (...these statements will be processed by VB Migration Partner...)
' ...

Alternatively, you can use the ParseMode Remarks pragma, in which case the excluded statements appear in the .NET project as commented out code. Both the ParseMode Off and ParseMode Remarks pragmas support a second argument, the number of statements to be excluded or commented, which is quite handy to exclude just one or two statements. For example, this VB6 code:

'##ParseMode Remarks, 1
DoSomething 123
DoSomething 456

translates to the following .NET code:

' IGNORED: DoSomething 123
DoSomething (456)

// C#
// IGNORED: DoSomething 123

In addition to excluding a portion of VB6 code, you can also include VB.NET or C# code, by means of the InsertStatement pragma, which simply emits its argument in the code produced by VB Migration Partner. Or you can use the ReplaceStatement to replace an individual VB6 statement with a VB.NET or C# statement. For example, assume that you must call the DoSomething method with the argument 123 under VB6 and with the argument 456 under VB.NET. This code does the trick:

'##ReplaceStatement DoSomething(456)
DoSomething 123

The argument of the InsertStatement and ReplaceStatement pragmas is emitted verbatim: VB Migration Partner doesn’t process the argument and the code you emit must be a valid VB.NET or C# statement, else a compilation error occurs when the code runs in Visual Studio. For this reason, if you are converting to C# the pragma should including a trailing semicolon:

'##ReplaceStatement DoSomething(456);
DoSomething 123

A third technique to exclude or include a portion of code in the migration process is by means of the VBC_VER compilation constant. Such a constant is undefined in VB6 and is equal to 8 in VB2005 or later version (and is correctly recognized by VB Migration Partner), thus you can write VB6 code such as this:

#If VBC_VER = 8 Then
    ' this code is ignored by VB6
    ' but is processed and migrated by VB Migration Partner
    ' ...
    ' this code is executed by VB6 but is ignored by VB Migration Partner
    ' ...
#End If

This last technique is especially effective when including a large portion of .NET code. Notice that, unlike the InsertStatement pragma, the VB6 code in the “true” portion of the #If block is correctly parsed and migrated by VB Migration Partner. It is therefore necessary that the code in the block contains valid VB6 statements.

A fourth technique is based on the OutputMode pragma, which allows you to uncomment a block of remark lines. You can therefore insert a block of commented VB.NET statements and have them uncommented when the output file is generated:

        '## OutputMode Uncomment
        ' ' This is a block of VB.NET code
        ' Debug.Print("x={0}", x)
        ' ...
        '## OutputMode On

The uncommented code must contain valid .NET statements. For example, when converting to C# the above code should be written as follows:

        '## OutputMode Uncomment
	' // This is a block of C# code
	' System.Diagnostics.Debug.Print("x={0}", x);
	' ...
	'## OutputMode On

Finally, if the amount of VB.NET code to be inserted isn’t negligible, you can store it in a text file and use a PostInclude pragma to include it in a specific point of the converted application:

        '## PostInclude c:\includes\copyright.txt


Previous | Index | Next