473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Classes communicating with other classes

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?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
6 1042
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

"PaulN" wrote:
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?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #2
When you say you can't get it to work...What error are you getting? What
are the results?

"PaulN" <pa***@adhe.arknet.nospam.edu> wrote in message
news:pa***@adhe.arknet.nospam.edu:
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?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #4
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

"PaulN" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #5
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6
PaulN,

You can probably use Form2's Form_Load event.

Kerry Moorman

"PaulN" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Piotr Getka | last post by:
Hi I have 3 classes: - CMainFrame - CPitApp - CPitView In class CPitApp i create object CMainFrame* pMainFrame = new CMainFrame and in class CMainFrame there is:
1
by: ptkacz | last post by:
I've got two classes, B and C, each performing essentially the same functionality, except for the fact that they each operate on a different data type, and that they each have a class method that...
3
by: Stan | last post by:
Hallo, I have developed an application in MS Access 2000 (Polish version) under MS Windows XP prof (also Polish). Now I would like to run this code on MS Windows XP EN and MS Access XP EN. I have...
0
by: DD | last post by:
I am trying to place a Word document into a form. I get the following error A problem occured while Microsoft Access was Communicating with the OLE or Activex Control the name of the ole is...
2
by: Matthew Playne | last post by:
Hi, I am having a problem communicating between to classes. I have an MDI application. The parent window contains a mainMenu and a toolbar and toolbuttons, and the child form contains a...
1
by: TGF | last post by:
Hello, I have a little dilemma (At least it is to me). I have two classes, Class A and class B, where Class A contains the UI thread and class B contains another thread. I want to be able to...
10
by: Gabe Moothart | last post by:
Hi, I have 3 applications (2 services and a winforms app) that need to be able to send/recieve messages from each other. What is the best way to do this in .NET? I looked briefly at remoting, but...
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.