Question

In a VB.NET application, what is the proper way to open a form?

Dim f1 as new Form1()
f1.Show()

or

Form1.Show()

I have inherited a codebase that is nearly 100% the latter.

What exactly is happening here? From what I understand, this should be an error considering Form1 is never initialized with a new. Does .NET automatically instantiate an object for all of your forms?

What are the issues that can be caused by this, specifically memory issues?
Is it bad practice to use Forms like this?

Was it helpful?

Solution

there is already a question on this - Why is there a default instance of every form in VB.Net but not in C#?

It seems it's a vb specific syntax - in c# you'd certainly need to instantiate a new instance of the form, and even in vb I'd suggest that would be best practice...after all you are dealing with an instance of the form. I'm actually surprised that vb.net lets you call show as a static method on a form...it just doesn't compute for me

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top