Home > Back-end >  "Form" Reference to a non-shared member requires an object reference
"Form" Reference to a non-shared member requires an object reference

Time:07-28

Not sure why I am getting this below error when I tried to set MDIParent of Form2 to Form1. I have the same code running on.NetFramewok but it is not working in .Net Core 3.1

appreciate your help and guidance in understanding the bigger picture.

error at Form2.MdiParent

Form2.MdiParent = Me

enter image description here

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Form2.MdiParent = Me
    End Sub
End Class

Form2

Partial Class Form2
    Inherits System.Windows.Forms.Form

    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Text = "Form2"
    End Sub

CodePudding user response:

It appears that .NET Core 3.1 does not support VB default form instances. If you want to stick with that framework, you'll need to create all form instances explicitly, e.g.

Dim f2 As New Form2 With {.MdiParent = Me}

f2.Show()

If you want to keep using default instances then you'll need to upgrade to .NET 6.

To make a form class a singleton, which works similarly to default instances, you do pretty much as you would for any other class. You add a private constructor, so the class can only be created internally, then you add a Shared property to expose the one and only instance. In the case of forms, you need to check whether the last instance has been disposed, as that will require a new one to be created:

Public Class Form2

    Private Shared _instance As Form2

    Private Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    End Sub

    Public Shared ReadOnly Property Instance As Form2
        Get
            If _instance Is Nothing OrElse _instance.IsDisposed Then
                _instance = New Form2
            End If

            Return _instance
        End Get
    End Property

End Class

You could then do this:

Form2.Instance.MdiParent = Me
Form2.Instance.Show()

CodePudding user response:

As to what @John answered above.

It seems .Net Core 3.1 does not support default Form instance. .Net Core3.1 shipped with Visual Studio 2019.

But when I downloaded VS 2022 and created a simple .Net Windows From Project targeting .Net 6. I was able to call Form2 directly without the need to instantiate an instance of Form2

Public Class Form1
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Form2.MdiParent = Me
        Form2.Show()
    End Sub
End Class

Just incase if someone stumbles through the same issue.

  • Related