Previous | Index | Next 

[PRB] Limitations in the conversion of #If blocks

If the VB6 code contains an #If #Else #EndIf block, VB Migration Partner converts only the code inside the “true” block; the code in the “false” portion is copied as-is in the .NET project. Consider the following VB6 code:

        #Const VERSION = "Registered"

        #If VERSION = "Registered" Then
        ' Load data from last session
        LoadSessionData "session.dat"
        #ElseIf VERSION = "Demo" Then
        ' Display a reminder
        MsgBox "Remember to register this program"
        #End If

VB Migration Partner converts correctly the call to LoadSessionData method but not the call to MsgBox:

        #Const VERSION = "Registered"

        #If VERSION = "Registered" Then
        ' Load data from last session
        LoadSessionData("session.dat")
        #ElseIf VERSION = "Demo" Then
        ' Display a reminder
        MsgBox "Remember to register this program"
        #End If

If you later modify the the value of any compilation constants, you need to manually fix the code that is now in the “true” block. If the amount of code that must be manually fixed isn’t negligible, we recommend that you change the #Const or #If expressions in the original VB6 code, run VB Migration Partner again to correctly convert the code, and then manually merge the code that you have obtained in the two runs of the tool. In this particolare example, you should change the following statement:

        #Const VERSION = "Demo"

and then re-run VB Migration Partner.

Notice that, in some cases, you might need to rearrange the original VB6 code before the migration. For example, consider the following example, in which the SerialNumber property is defined as read-write for registered versions and as read-only for unregistered ones:

        #Const VERSION = "Registered"

        Private m_SerialNumber As String

        Public Property Get SerialNumber() As String
            SerialNumber = m_SerialNumber
        End Property

        #If VERSION = "Registered" Then
        Public Property Let SerialNumber(ByVal newValue As String)
            m_SerialNumber = newValue
        End Property
        #End If

VB Migration Partner must merge the two Property procedures, and delivers the following VB.NET code:

        #Const VERSION = "Registered"

        Private m_SerialNumber As String

        #If VERSION = "Registered" Then
        Public Property SerialNumber() As String
            Get
                Return m_SerialNumber
            End Get
            Set (ByVal newValue As String)
                m_SerialNumber = newValue
            End Set
        End Property
        #End If

The result is correct, but if you change the VERSION compilation constant to “Unregistered”, then the SerialNumber property goes away instead of just being rendered as read-only. In such cases you can make your fixing job easier if you re-arrange the original VB6 code as follows:

        #Const VERSION = "Registered"

        Private m_SerialNumber As String

        #If VERSION = "Registered" Then
        Public Property Get SerialNumber() As String
            SerialNumber = m_SerialNumber
        End Property

        Public Property Let SerialNumber(ByVal newValue As String)
            m_SerialNumber = newValue
        End Property

        #Else

        Public Property Get SerialNumber() As String
            SerialNumber = m_SerialNumber
        End Property

        #End If

In fact, you can now run VB Migration Partner and obtain the following VB.NET code:

        #Const VERSION = "Registered"

        Private m_SerialNumber As String = ""

        #If VERSION = "Registered" Then

        Public Property SerialNumber() As String
            Get
                Return m_SerialNumber
            End Get
            Set(ByVal newValue As String)
                m_SerialNumber = newValue
            End Set
        End Property

        #Else

        ' UPGRADE_WARNING (#0244): Code in current #If ,#ElseIf, or #Else block 
                                   hasn't been upgraded.
        Public Property Get SerialNumber() As String
            SerialNumber = m_SerialNumber
        End Property

        #End If

Next, change the definition of the compilation constant as follows:

        #Const  VERSION = "Unregistered"

and re-run VB Migration Partner to obtain the following VB.NET code:

        #Const VERSION = "Unregistered"

        Private m_SerialNumber As String = ""

        #If VERSION = "Registered" Then
        ' UPGRADE_WARNING (#0244): Code in current #If ,#ElseIf, or #Else block
                                   hasn't been upgraded.
        Public Property Get SerialNumber() As String
            SerialNumber = m_SerialNumber
        End Property

        Public Property Let SerialNumber(ByVal newValue As String)
            m_SerialNumber = newValue
        End Property

        #Else

        Public ReadOnly Property SerialNumber() As String
            Get
                Return m_SerialNumber
            End Get
        End Property

        #End If

You can now merge the two versions of the SerialNumber properties in one VB.NET code:

        #Const VERSION = "Registered"

        Private m_SerialNumber As String = ""

        #If VERSION = "Registered" Then

        Public Property SerialNumber() As String
            Get
                Return m_SerialNumber
            End Get
            Set(ByVal newValue As String)
                m_SerialNumber = newValue
            End Set
        End Property

        #Else

        Public ReadOnly Property SerialNumber() As String
            Get
                Return m_SerialNumber
            End Get
        End Property

        #End If

We suggest that you perform this “manual merge” only when the application has been completely migrated and tested, as your changes can’t be integrated in the convert-test-fix cycle.

 

Previous | Index | Next