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? 6 1882
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
"Bob" <ir*****@arc.losrios.edu> wrote in message
news:08****************************@phx.gbl... 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?
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
-------------------------------------------
When I build the project (version 2002 or 2003), I get a
stack overflow. -----Original Message----- Bob, Sounds like spaghetti? :-)
Can you post a sample of what you are doing? Along with
which routine youwere calling when you got the stack overflow.
Hope this helps Jay
"Bob" <ir*****@arc.losrios.edu> wrote in message news:08****************************@phx.gbl... 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,
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
"Bob" <ir*****@arc.losrios.edu> wrote in message
news:01****************************@phx.gbl... 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 -------------------------------------------
When I build the project (version 2002 or 2003), I get a stack overflow.
-----Original Message----- Bob, Sounds like spaghetti? :-)
Can you post a sample of what you are doing? Along with which routine youwere calling when you got the stack overflow.
Hope this helps Jay
"Bob" <ir*****@arc.losrios.edu> wrote in message news:08****************************@phx.gbl... 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?
.
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 -----Original Message----- 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 getscreated a new form1 gets created, which causes a new
form2, which causes anew 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 aninstance of the object to the constructor of the form
(add an overloaded SubNew). Unfortunately this will not work in this case as
you have two formsthat mutually refer to each other. In that case I
normally have the secondobject accept the first as a parameter to the
constructor, in the constuctorof the second I call a property of the first to set the
reference to thesecond.
Out of curiosity: What are you attempting to do, that
you have each formrefer to the other? I suspect a Singleton Pattern might
be a betterapproach.
Hope this helps Jay
"Bob" <ir*****@arc.losrios.edu> wrote in message news:01****************************@phx.gbl... 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 -------------------------------------------
When I build the project (version 2002 or 2003), I get
a stack overflow.
>-----Original Message----- >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 > >"Bob" <ir*****@arc.losrios.edu> wrote in message >news:08****************************@phx.gbl... >> 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, that mutually refer to each other. In that case I normally have the secondobject accept the first as a parameter to the constructor, in the constuctorof the second I call a property of the first to set the reference to thesecond.
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
"Bob" <ir*****@arc.losrios.edu> wrote in message
news:05****************************@phx.gbl... 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
-----Original Message----- 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 getscreated a new form1 gets created, which causes a new form2, which causes anew 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 aninstance of the object to the constructor of the form (add an overloaded SubNew). Unfortunately this will not work in this case as you have two formsthat mutually refer to each other. In that case I normally have the secondobject accept the first as a parameter to the constructor, in the constuctorof the second I call a property of the first to set the reference to thesecond.
Out of curiosity: What are you attempting to do, that you have each formrefer to the other? I suspect a Singleton Pattern might be a betterapproach.
Hope this helps Jay
"Bob" <ir*****@arc.losrios.edu> wrote in message news:01****************************@phx.gbl... 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 -------------------------------------------
When I build the project (version 2002 or 2003), I get a stack overflow.
>-----Original Message----- >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 > >"Bob" <ir*****@arc.losrios.edu> wrote in message >news:08****************************@phx.gbl... >> 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? > > >. >
.
Jay,
Thanks for all your help! It is greatly appreciated. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
by: olaf |
last post by:
Hi!
When using the "me." keyword (for ex. me.FilterOn = True), I sometimes
(not always) get the error message:
"The expression yuo entered refers to an object that is closed or
doesn't exist."...
|
by: Anthony England |
last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled
error is encountered, but it seems that this also happens when the variable
is defined as private at form-level.
So...
|
by: jcrouse |
last post by:
I am using the following code to trap errors in a sub routine:
Try
Executable code
Catch ex As Exception
Dim strInputE As String = Application.StartupPath & "\Error.txt"
Dim srE As...
|
by: Bob |
last post by:
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?
|
by: BerkshireGuy |
last post by:
I have a form that contains two subforms. Both of these subforms use
the same function.
Rather than repeating the code, I want to be able to pass the form name
to the code. Right now, its a...
|
by: Lysander |
last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
|
by: hyperpau |
last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding.
I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com).
Ergo, I...
|
by: hyperpau |
last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding.
I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com).
Ergo, I...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |