VB Migration Partner

KNOWLEDGE BASE - Forms and controls


Previous | Index | Next 

[PRB] Form-level variables in default forms don’t retain their values between calls

In the attempt to simplify the migration from VB6, Microsoft introduced the support for default form instances in VB2005. However, this feature isn’t complete and still creates many problems.

One of such problems is that, even if the code appears to be referencing the same form instance, it actually creates a new form each time the form is shown. To prove this point, consider the following VB.NET code:

        ' inside Form1
        Private Sub Form_Load() Handles MyBase.Load
            ' assign a variable belonging to Form2
            Form2.UserName = "John Doe"
        End Sub
  
        Private Sub Command1_Click() Handles Command1.Click
            ' load  and show Form2, but also display it’s handle
            Debug.PrintLine(Form1.Handle.ToInt32())
            Form2.Show()
        End Sub

If you click Command1, then close the second form that appears, and click Command1 again, you will notice that the form’s handle is differente, which in turn proves that you are dealing with two different instances, even though both were accessed through the Form2 variable.

As a consequence for this fact, the second instance of Form2 will have its UserName variable set to an empty string. If your code assumes that the UserName variable value is preserved between calls you are in trouble.

The simplest way to work around this issue is to move the UserName variable into a BAS module (before the migration) or to a VB.NET Module (after the migration), so that its value is preserved between calls.

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