Two forms sharing data

D

DaveE

I have a project consisting of two forms and a class that
I created. The class, called Project, has several
properties and a method. Form1 instantiates my Project
class and populates the properties from textboxes. When
the user presses the Next button, Form2 should display the
data in the class properties. I'm getting an error in
Form2 because the class object hasn't been referenced.

I'm new to the .NET environment. Could someone point me
in the right direction?

Thanks much.
 
C

Carol Torkko

Hi Dave,
I know that you instantiated the class in Form1, but
where did you declare the variable used to instantiate the
class and what modifier did you use? Carol
 
D

DaveE

My code looks like this:
Public Class frmMain
Inherits System.Windows.Forms.Form
Public objProject as New Project()
....
 
C

Carol Torkko

Hi Dave,
Declare your object variable in a module. So add a module
to your project and declare it there. You can set it as
friend if your object is only used within your project.
If you have multiple projects in your solution and you
want to make it available to all projects, make it
public.

Carol
 
C

Chris Dunaway

Overload the constructor of Form2 to take an instance of your project class
and when you create form2, pass in the instance that you created in Form1:

Public Class frmMain
Inherits System.Windows.Forms.Form
Public objProject as New Project()

Dim f2 As New Form2(objProject)

f2.Show



'In Form2:

Public Sub New(o As Project)
'Do something with o here
End Sub

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top