Error Module level form obj Mltiple forms

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Declaring a module level form object in multiple froms
within a project to show another project form causes a
stack overflow and other assorted errors. Can anyone help?
 
Bob,
Sounds like spaghetti? :-)

Can you post a sample of what you are doing? Along with which routine you
were calling when you got the stack overflow.

Hope this helps
Jay
 
Jay,

The project has two forms with nothing on either of them.

Code in Form 1
------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form
'Windows Form Designer generated code
Dim objForm2 As New Form2()
End Class
-------------------------------------------


Code in Form2
-------------------------------------------
Public Class Form2
Inherits System.Windows.Forms.Form
'Windows Form Designer generated code
Dim objForm1 As New Form1()
End Class
 
Bob,
You are using object initialization syntax. Which is causing your problems.

When you create a new Form1 a new form2 gets created, when a new form2 gets
created a new form1 gets created, which causes a new form2, which causes a
new form1, which will go on until you run out of stack space.

Change these two lines:
Dim objForm1 As New Form1()
Dim objForm2 As New Form2()
To:

Dim objForm1 As Form1
Dim objForm2 As Form2

Now the problem becomes how to initialize the variables. Normally I pass an
instance of the object to the constructor of the form (add an overloaded Sub
New). Unfortunately this will not work in this case as you have two forms
that mutually refer to each other. In that case I normally have the second
object accept the first as a parameter to the constructor, in the constuctor
of the second I call a property of the first to set the reference to the
second.

Out of curiosity: What are you attempting to do, that you have each form
refer to the other? I suspect a Singleton Pattern might be a better
approach.

Hope this helps
Jay
 
Jay,

Thanks for the help! What was I trying to do?

The project involves switching between two forms from
seversl event procedures within each form. Instead of
creating the form object in each of the button click
events so that a show method could be used, I thought it
would be simpler to declare the form object as a module
level object instead of a procedure level object.

Thanks again!

Bob
 
Bob,I'm not sure what happened here, it easier than this:

Seeing as the first form is creating the second form. The first form just
needs to save the reference of the second form that it created. The second
form just needs a parameter to the first on its constructor so that it can
save the reference to the first.

Something like:
Public Class Form1

Private Readonly m_form2 As Form2

Public Sub New()
m_form2 = New Form2(me)
End Sub

End Class

Public Class Form2

Private Readonly m_form1 As Form1

' needed for the designer
Private Sub New()
End Sub

Public Sub New(form1 As Form1)
MyClass.New()
m_form1 = form1
End Sub

End Class

Of course the above assumes that when one form closes the other form is soon
to close.

Hope this helps
Jay
 
Back
Top