Do you need to explicitly instantiate a form in VB.NET?

If the project contains a class Form, can the form be displayed:

Form1.Show

or do you need to first create an instance of the form?

Dim frm As New Form1
frm.Show
+4
source share
2 answers

Yes, it can be an instance of the default form , it remains compatible with the language for VB6. If it were me, I would have avoided it like a plague, it’s only muddy waters. Create your own instances.

+5
source

, , . ; , .

VB.NET VB 2005, . , VB6, VB6 , . , , , , - . , , .

, VB.NET, . , . , , , .

, . , , , , , , , .

, :

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

, .

, singleton:

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

Form1 , .

Form1.Instance.Show()
Form1.Instance.Activate()

, .

+4

Source: https://habr.com/ru/post/1531461/


All Articles