Object references in different form classes

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'm trying to write a Windows application that obtains
information from a web service. I can access the web
service successfully, but I can't access the object
returned by the web service in different forms.

My application is something like this:

MainForm.vb:

Public Class MainForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

Public Sub GetResultCollection( ...
Dim WebService As New ThisWebService
Dim WebServiceResult As New ThisWebResult
Dim WebServiceElement As New ThisResultElement
WebServiceResult = WebService.getResult("parameter")
Dim ResultForm As New myResultForm
ResultForm.Show()
End Sub
End Class

myResultForm.vb:

Public Class myResultForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

Public Sub ShowResults( ...
For Each WebServiceElement In
WebServiceResult.resultElements
myResultForm.TextBox.Text =
WebServiceElement.textfield
End Sub
End Class

Note that objects declared in MainForm are accessed in
myResultForm. And that is the problem I'm having.

I can't figure out how to make VB .NET recognize the
objects in myResultForm.

Note ... this is a "scope of variables" problem. The web
service calls all work and if I keep everything in one
form, everything works great. But I just can't get the
objects in the first form to be "seen" in the second one.
 
1. Give Form2 a public property (myForm1) of type Form1

Dim frm as Form1
Public Property myForm1 as Form1
..
..
..
End Property


2. Set the myForm1 property of Form2 before calling Show

Dim f as new Form2
f.myForm1 = me
f.show


3. Make sure the variables you are trying to hit on Form1 are Public

( Public WebService As New ThisWebService , not Dim [which defaults to
Private] )


4. Access them on Form2 by calling

myForm1.VariableName.


There are a few other ways to do this, but this one will work.

hth


Kevin
 
-----Original Message-----
1. Give Form2 a public property

Very Good! This gave me the clue I needed to move
forward. But I believe there are a few bugs in your code
that need to be corrected, so I'm posting the work I did
for the benefit of any future programmers who might be
this answer.

I reworked the example to make it more simple. Since web
services had nothing to do with it, the example below are
simple standard Windows forms.

Here's what worked for me. Please let me know if this can
be improved.

Form1.vb:

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public o As Object
Public newform As New Form2
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
o = 12345
MsgBox("Form1: " & o)
newform.myForm1 = Me
newform.Show()
End Sub
End Class

Form2.vb:

Public Class Form2
Inherits System.Windows.Forms.Form
'Bug 1 - the object is passed as a property, so it must
be declared in this form
Private MainForm As Form1
#Region " Windows Form Designer generated code "
Property myForm1() As Form1
'Bug 2 Properties must have Get and Set procedures
Get
End Get
Set(ByVal Value As Form1)
MainForm = Value
End Set
End Property
Private Sub Form2_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'Bug 3 MainForm is the object that is set by the property
MsgBox("Form2: " & MainForm.o)
End Sub
End Class
 
By the way, I posted the "anonymous" messages above.

I consider this problem to be solved at this time.

(Although I would still appreciate seeing alternative
ideas if anyone has them.)

Dan
 
Back
Top