Classes communicating with other classes

P

PaulN

I need to instantiate a class (Class1) in the startup form (Form1), set
some of Class1's properties then open another form (Form2) and have it
read Class1's properties and set the remainder of Class1's properties
and finally to have Form1 examine Class1 and send changes to a database.

I've tried passing Class1 into a property of Form2 and every combination
of Friend declaration that I can think of and can't get this to work.

I'm not fluent in this class based paradigm. How can I make an
instantiated class visible to a class (Form) where it wasn't
instantiated?
 
G

Guest

PaulN,

You should be able to dimension a public or friend variable in the
declarations section of Form2 and assign an object from Form1 to the public
or friend variable.

You should also be able to change Form2's constructor to accept a reference
to the object from Form1.

Maybe you could post some code that you are trying.

Kerry Moorman
 
B

Brian Swanson

When you say you can't get it to work...What error are you getting? What
are the results?
 
P

PaulN

I created a do nothing solution to get rid of everything that doesn't
have to do with the problem I'm having.

'***********************************************************************
*****
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cls As Class1
Public Sub New()
MyBase.New()
InitializeComponent() 'this form contains only Button1 and
Lable1
cls = New Class1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.ShowDialog()
Label1.Text = cls.Test
End Sub
End Class
'***********************************************************************
*****
Public Class Form2
Inherits System.Windows.Forms.Form
Friend cls As Class1
Public Sub New()
MyBase.New()
InitializeComponent() 'this form contains no controls
cls.Test = "string" 'ERROR OCCURS HERE
End Sub
End Class
'***********************************************************************
*****
Public Class Class1
Private myTest As String
Public Sub New()
MyBase.new()
End Sub
Public Property Test() As String
Get
Return myTest
End Get
Set(ByVal Value As String)
myTest = Value
End Set
End Property
End Class

When I run this I get this error:
An unhandled exception of type 'System.NullReferenceException'
occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of
an object.

In the assignment operation of Form2.New
 
G

Guest

PaulN,

On Form1, in Button1' click event you need:

Dim frm As New Form2
frm.cls = cls '<------- add this line
frm.ShowDialog()
Label1.Text = cls.Test

Kerry Moorman
 
P

PaulN

Yeah, that's the ticket!!! Thanks. It's getting close.

Now, what event fires in Form2 when I call it's .ShowDialog() from
Form1? That's where I need to be filling in the blanks of the real
form2.

Thanks
 

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