Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Reference to a non-shared member requires an object reference

Hi all,

I have an application with two forms, in which the following occurrs:
1. MainForm opens ChildForm.  
2. Before ChildForm closes it should call to a sub in MainForm.
3. This sub should take parameters passed from ChildForm and populate a TreeView within MainForm.

This worked fine when the project was configured as a Windows Application. However, it has become necessary to change this to a Class Library. After doing so, the call to the sub in MainForm errors out with "Reference to a non-shared member requires an object reference".  If I change the sub to Public Shared, it cannot manipulate the control it is supposed to populate.  I tried creating an object reference from the ChildForm as it suggests but when I step through the code, it seems to work but nothing populates in the TreeView.

Any help will be extremely helpful.  Thanks!



Public Class MainForm
...
    ' Populate the TreeView with the data from ChildForm
    Private Sub GetInfoFromChildForm(ByVal Info as string)

        ...

    End Sub
...
End Class


Public Class ChildForm
...
    ' Pass the info to MainForm
    Private Sub PassInfo()

        ' Call the sub in MainForm that populates the MainForm TreeView
        MainForm.GetInfoFromChildForm(InfoToPass)

    End Sub
...
End Class
0
esdTeamsters
Asked:
esdTeamsters
  • 2
  • 2
  • 2
1 Solution
 
AlexFMCommented:
Public Class MainForm

...
    ' create and show ChildForm
     dim childForm as ChildForm
     childForm = new ChildForm()
     childForm.mainForm = Me
     ...

...
    ' Populate the TreeView with the data from ChildForm
    Private Sub GetInfoFromChildForm(ByVal Info as string)

        ...

    End Sub
...
End Class


Public Class ChildForm

    public mainForm as MainForm
...
    ' Pass the info to MainForm
    Private Sub PassInfo()

        ' Call the sub in MainForm that populates the MainForm TreeView
        mainForm.GetInfoFromChildForm(InfoToPass)

    End Sub
...
End Class
0
 
esdTeamstersAuthor Commented:
No luck so far. I get an "Object reference not set to an instance of an object" error at the line that calls the MainForm sub.  I tried changing:

public mainForm as MainForm

to

public mainForm as New MainForm

...and it doesn't error out on me, but doesn't populate the control either.
0
 
esdTeamstersAuthor Commented:
My mistake,  I got it to work.  Thanks for the quick response!!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AlexFMCommented:
dim childForm as ChildForm
childForm = new ChildForm()
childForm.mainForm = Me                  '  did you add this line?

Last line sets reference to main form in child form.

public mainForm as MainForm   ' no need in New

If you don't khow where to apply this code, post code fragment where you create and show child form from main form.

0
 
wiswalldCommented:
Thats not it. What I mean is if I put a button on form1 (lets say) and type in form2.Button2_Click after I put in Form2. I should get a list of options like controls on Form2. I do not see them and if I just type them in I get the error.
0
 
wiswalldCommented:
Sorry I posted to the wrong question.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now