问题描述
如果项目包含 Form
类,该表单可以显示为:
Form1.Show
还是需要首先创建表单实例?
Dim frm as New Form1
frm.Show
$如已建议的那样,在您的第二个代码段显式创建实例时,使用表单名称将使用默认实例,这已被建议。
如果项目包含 Form
类,该表单可以显示为:
Form1.Show
还是需要首先创建表单实例?
Dim frm as New Form1
frm.Show
$如已建议的那样,在您的第二个代码段显式创建实例时,使用表单名称将使用默认实例,这已被建议。
在这两种情况下,都有一个表单类的实例。
默认实例直到VB 2005才在VB.NET中存在,这是第三个问题。版。添加它们是为了使从VB6的过渡更加容易,因为某些VB6开发人员由于必须显式创建对象而感到困惑。但是,他们设法造成了新的混乱,因为这意味着形式似乎在行为上与其他类型有所不同。另外,有些人试图在多线程应用程序中使用默认实例,这会产生问题,因为默认实例是特定于线程的。
通常没有必要为该实例实现单例模式VB.NET中的表单,因为默认实例会为您执行此操作。真正的单例的唯一优点是它不是特定于线程的。还值得注意的是,如果默认情况下为项目启用了应用程序框架,则启动表单是其类型的默认实例。
个人而言,除非我想要单例功能,否则我永远不会使用默认实例。它对您的作用是使访问其他表单中的表单成员变得更容易,但是需要默认实例才能实现的类似操作反而是不好的做法。
您可能想查看我的一些博客文章,以获取有关默认实例以及如何在没有它们的情况下在表单之间进行通信的信息:
制作确保您已阅读第二部分的所有三个部分。
要回答有关如何实现单例模式的问题:
公共类表格1
'''< summary>
’’’是唯一的实例。
’’< / summary>
私人共享的实例为Form1
’’< summary>
’’’取得唯一实例。
’’< / summary>
作为Form1的公共共享只读属性实例
获取
'如果没有实例或实例已被破坏...
如果_instance Nothing否则Else _instance.IsDisposed然后
'...创建一个新的。
_instance =新Form1
End如果
返回_instance
End获取
End属性
'唯一的构造函数是私有的因此无法在外部创建实例。
Private Sub New()
’设计人员需要此调用。
InitializeComponent()
’在InitializeComponent()调用之后添加任何初始化。
最终子级
最终级
您然后仅通过Instance属性与Form1进行交互,例如
Form1.Instance.Show()
Form1.Instance.Activate()
这可确保显示唯一实例并具有焦点。
If a project contains a Form
class, can the form be shown by:
Form1.Show
or does an instance of the form need to be created first?
Dim frm As New Form1
frm.Show
As has been suggested, using the form name uses the default instance while you second snippet explicitly creates an instance. In both cases there is an instance of the form class; it's just a matter of whether the system creates it for you or you create it yourself.
Default instances did not exist in VB.NET until VB 2005, which was the third version. They were added to make the transition from VB6 easier because some VB6 developers were confused by having to explicitly create objects. They managed to create new confusion though, because it meant that forms seemed to behave differently to other types. Also, some people tried to use default instances in multi-threaded applications and that creates issues because default instances are thread-specific.
There's generally no point implementing a singleton pattern for forms in VB.NET because default instances do that for you. The only advantage to a genuine singleton would be that it would not be thread-specific. It's also worth noting that, if the application framework is enabled for your project, which it is by default, the startup form is the default instance of its type.
Personally, I would never use a default instance unless I wanted singleton functionality. What it does do for you is make it easier to access members of forms from other forms but anything like that that requires a default instance to achieve is bad practice anyway.
You might like to check out a couple of my blog posts for information on default instances and how to communicate between forms without them:
http://jmcilhinney.blogspot.com.au/2009/07/vbnet-default-form-instances.htmlhttp://jmcilhinney.blogspot.com.au/2012/04/managing-data-among-multiple-forms-part.html
Make sure that you read all three parts of the second one.
To answer the question of how to implement the singleton pattern:
Public Class Form1
''' <summary>
''' The one and only instance.
''' </summary>
Private Shared _instance As Form1
''' <summary>
''' Gets the one and only instance.
''' </summary>
Public Shared ReadOnly Property Instance As Form1
Get
'If there is no instance or it has been destroyed...
If _instance Is Nothing OrElse _instance.IsDisposed Then
'...create a new one.
_instance = New Form1
End If
Return _instance
End Get
End Property
'The only constructor is private so an instance cannot be created externally.
Private Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
You then interact with Form1 only via the Instance property, e.g.
Form1.Instance.Show()
Form1.Instance.Activate()
That ensures that the one and only instance is displayed and has focus.
这篇关于您是否必须在VB.NET中显式创建表单实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!