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
But from the VB.NET design environment, click the Project menu. From the drop down menu, click Add Windows Form. The Add New Item dialogue box appears. Select Windows Form under Templates.
We can add a new form to the project by selecting Project->Add New Item from the main menu which displays a window which has many items to add to your project. Select Windows Form from that window and click on Add button to add a new form to the project. We can also add a new form from the Solution Explorer.
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.html http://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.
Yes it can, it is the default Form instance it was left in the Language for VB6 compatability. If it was me I would avoid it like the plague, it only muddies the waters. Create your own instances instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With