Home > Software design >  Reference to a non-shared member requires an object reference for showing/hiding forms
Reference to a non-shared member requires an object reference for showing/hiding forms

Time:11-08

I have 3 forms, the form2 contains some info that I wanted to appear in a data grid view in form3, I searched online how to do it, so am not really that well-versed or fully understand the changes and or codes I wrote, as long as it worked. The problem now is that buttons from other forms 1 & 2 that supposedly hides and or shows other forms, have errors, it's a project and I would greatly appreciate your help, thx.

form1 (error 1, in bold)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 
gin.Click
    Dim nun As String = "apple"
    Dim pw As Single = 7

    
    Dim name As String
    
    Dim wd As Double
   

    name = name.Text
    wd = word.Text
   
    If (name = nun And wd = pw) Then
        Me.Hide()
        **try**.Show() ' error here
    Else
        
        MsgBox("Denied", vbOKOnly   MsgBoxStyle.Critical, "TRY AGAIN")       End If

End Sub

form2 (code for data grid view)

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 
    Save.Click

    view.dgvView.CurrentRow.Cells(1).Value = Id.Text
    view.dgvView.CurrentRow.Cells(2).Value = fruit.Text
    view.dgvView.CurrentRow.Cells(3).Value = veggie.Text
    view.dgvView.CurrentRow.Cells(4).Value = pasta.Text
    view.dgvView.CurrentRow.Cells(5).Value = drink.Text
End Sub

form 3 (where error 2 is, the one in bold and the code above is the code that I don't fully understand )

Public Class sky
    Private Sub dgvView_CellContentClick(ByVal sender As System.Object, ByVal e As 
    System.Windows.Forms.DataGridViewCellEventArgs) Handles sky.CellContentClick
        If e.ColumnIndex = 1 Then
            Dim entry As New sky(Me)
            entry.ShowInTaskbar = False
            entry.ShowDialog()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles blue.Click
        Me.Close()
        **sky**.Show()

    End Sub
End Class

CodePudding user response:

frmEntry is not a variable, it is a type (a class name). You can use the variable entry created with Dim entry As New frmEntry(Me) to access the form object. But since this variable is a local variable, it is only accessible inside of the Sub that declared it. Declare it on the class level (outside of the Subs), so that other Subs can access it.

Public Class frmView
    Dim entry As frmEntry ' <===== declare here

    Private Sub FirstSub()
        entry = New frmEntry(Me)  ' <==== Create and assign here
        entry.ShowInTaskbar = False
        entry.Show()
    End Sub

    Private Sub SecondSub()
        entry.Close() ' <=== Reference it from somewhere else
    End Sub
End Class

Note that a class (and therefore also a Form) is a reference type. Other than a value type like Integer, a variable of a reference type does not store the object it is assigned, but a reference to this object. See the difference:

Dim a, b As Integer
Dim f1, f2 As frmTest

a = 5
b = a
b = 10
' Now a = 5 and b = 10

f1 = new frmTest()
f1.Text = "Test 1"
f2 = f1
f2.Text = "Test 2"
' Now f1.Text = "Test 2" and f2.Text = "Test 2" because there is only one Form
' object referenced by the two variables.



  • Related