VB Migration Partner

KNOWLEDGE BASE - Language


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 




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