tags:

views:

76

answers:

2

Hello i Have a programa that has starts in a main sub, it executes some code and after that code it opens a form using

System.Windows.Forms.Application.Run(General)

General is a form but also a class, so I wonder what it the pros or cons of using :

System.Windows.Forms.Application.Run(General)

vs

Dim gen as general
System.Windows.Forms.Application.Run(gen)

In the first I am opening a form using the name of the class, and I read that it is best to declare the instance as an object variable.

So far I used the first approach without any problem.

Thanks for your comments !

+1  A: 

In the last snippet, you're passing a null reference to the Run() method. Don't forget to create an instance of the form first.

Joel Coehoorn
Oh yes, the correct one is Dim gen as New General
carlos
+1  A: 

Yes, your first code snippet gives OOP purists the shivers. Application.Run() requires a form object, you are passing the form type. That this is possible is an anachronism carried over from VB6. It is admittedly very bad style, it prevents programmers from understanding the difference between a type and an object. Something that's crucial to understand to get ahead with object oriented programming. It is also quite lethal when you start using threads.

Your second snippet is closer to what's needed, except that it cannot work. You pass an uninitialized object to Run(). Fix:

Dim gen as General = New General()
System.Windows.Forms.Application.Run(gen)

Or taking advantage of VB.NET syntax:

Dim gen as New General
System.Windows.Forms.Application.Run(gen)

Or since you never actually need the object in the Main method:

System.Windows.Forms.Application.Run(New General())
Hans Passant
Ok, so it works (because it works) but is a bad programming style. But what are the possible consecuences of using this error?.Thanks a lot !!!!
carlos
Well, it is not an error. The My Application feature explicitly supports it help VB6 programmers get up to speed on VB.NET. I mentioned one big consequence, Form1.InvokeRequired always returns False when used in a worker thread. Making it next to impossible for VB6 programmers to ever get threading to work right.
Hans Passant
Thanks for your comments !!
carlos