开发者

Best use of System.Windows.Forms.Application.Run()

开发者 https://www.devze.com 2022-12-30 14:19 出处:网络
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

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 开发者_StackOverflow中文版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 !


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())


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消