3

What is the difference between the following two statements (in terms of memory management):

Dim frm as New MyForm()
frm.Show()

VS

MyForm.Show()

I am originally a C# developer, how does the second one make sense or even compile for that matter in VB.NET? (Show() is not a Shared/Static method) What is happening in the second case?

Denis
  • 11,796
  • 16
  • 88
  • 150
  • 1
    See also: http://stackoverflow.com/questions/4698538/there-is-a-default-instance-of-form-in-vb-net-but-not-in-c-why – Brad Jul 26 '11 at 16:01

1 Answers1

5

MyForm.Show() is a hold over from VB6 and prior versions for compatibility reasons. In those versions, there typically was no creating multiple instances of a form so when MyForm.Show() was used, it automatically created a singleton instance of the form for default usage. You shouldn't use that method and the preferred method of creating an instance and calling the .Show() method on it is the right way and compatible with c# and other .net languages.

Robert Beaubien
  • 3,126
  • 4
  • 22
  • 29
  • @Denis, for your second example, anytime you reference `MyForm` as an object, you're actually calling a function similar to `If _MyForm Is Nothing Then _MyForm = New MyForm()` `Return _MyForm` – Hand-E-Food Jul 26 '11 at 23:29