Form loading trap in VB.NET

One of the first problems a VB6 developer encounters when migrating to VB.NET is that the way forms are loaded has changed completely. Although this (typical) VB6 method of loading the main application form is syntactically correct, it will definitely not produce the desired result:

Module Functions
   Public Sub Main()
      Dim F As New MainForm
      F.Show()
   End Sub
End Module

In VB6, this would load and display an instance of a form named "MainForm". In VB.NET, it would do exactly the same but then immediately afterwards would close the form and terminate the application.

Why?

The problem is that VB.NET treats the object "F" as a local instance of the "MainForm" class, which is destroyed as soon as the startup method Main() is exited. The solution is to use the Application.Run() method instead and take advantage of the built-in function overloading facilities that the .NET framework provides:

Module Functions
   Public Sub Main()
      Dim F As New MainForm
      Application.Run(F)
   End Sub
End Module

Did I fall for the same trap when I started out in VB.NET? Bloody oath I did.

Due to the large volume of spam, comments are disabled. If you have anything relevant to say, you can leave a , or contact me directly.

About the author

Ivan's mugshotI'm Ivan Lutrov and I'm the owner of Lutrov Interactive. I have 25 years of experience producing interactive work and I create cost effective business websites that are simple, engaging and easy to use. I preach what I practice, and I say what I really think, even if it's sometimes not what you expect to hear. Subscribe to the Lutrov Interactive feed via RSS and follow me on Twitter.