System.Windows.Forms.Application.Run() 的最佳使用 [英] Best use of System.Windows.Forms.Application.Run()

查看:21
本文介绍了System.Windows.Forms.Application.Run() 的最佳使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个从主子程序开始的程序,它执行一些代码,然后使用

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 是一种形式,也是一种类,所以我想知道使用它的利弊:

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)

对比

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.

感谢您的评论!

推荐答案

是的,您的第一个代码片段让 OOP 纯粹主义者不寒而栗.Application.Run() 需要一个表单object,你传递的是表单type.这是可能的,这是从 VB6 延续下来的时代错误.不可否认,这是非常糟糕的风格,它使程序员无法理解类型和对象之间的区别.对于理解面向对象编程至关重要的东西.当你开始使用线程时,它也是相当致命的.

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.

您的第二个代码段更接近所需的内容,只是它无法工作.您将一个未初始化的对象传递给 Run().修复:

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)

或者利用 VB.NET 语法:

Or taking advantage of VB.NET syntax:

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

或者因为您实际上从不需要 Main 方法中的对象:

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

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

这篇关于System.Windows.Forms.Application.Run() 的最佳使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆