need help with these errors

Yeung

Member
Joined
Jun 15, 2007
Messages
22
Programming Experience
Beginner
I can't switch back to a form i hide there was an error message. for example i used me.hide and form2.show on form1 and after doing something on form2 i wanted to switch back to form1 so i used me.hide and form1.show again.

but an error message is being displayed when i try to compile it and i created an instance of both forms prior to these.

also whenever i declare as something as a new instance of a form, i.e. Dim frm As New Form1 after the windows generated code or after the public class declaration, i get an error message that says "An unhandled exception of type 'System.StackoverflowException' occured in WindowsApplication1.exe and a yellow highlight is on the code Mybase.New()

Am i wrong in declaring that instance of a form after windows generated code or public class declaration since i want every subroutine to use it.
 
Am i wrong in declaring that instance of a form after windows generated code or public class declaration since i want every subroutine to use it

You mean your code looks like this:

VB.NET:
Public Class Form1
  Inherits Form

  Public f1 As New Form1

Thinking about this; why do you suppose this code might crash?
 
You mean your code looks like this:

VB.NET:
Public Class Form1
  Inherits Form

  Public f1 As New Form1

Thinking about this; why do you suppose this code might crash?
yes and after that i posted it after the windows generated codes, i'm just new to vb.net so i'm a little clueless :confused:

any help would be greatly appreciated

for now i am making those declarations on all subroutines that needs access to other forms.
 
Don't you see that creating a new Form1 in that case will cause another new Form1 to be created, which in turn will create another new Form1 again and again forever? Of course that will StackoverflowException.

There are less options for you with .Net 1 managing forms than .Net 2, but still some:
- You can keep a shared collection of open forms in a Module
- you can keep references of forms opened by one form, and expose it to childs
- in case you want to reference the form that created current form you can set the Parent or ParentForm when creating it
- in case of MDI there is the MdiParent and MdiChildren properties

.Net 2 adds a few options by having "default form instances" available and the Application.OpenForms which is similar to the first option above.

It's pretty much about having a structured layout for the program flow and figure what form needs to reference another and how to obtain that reference.

Also a common beginner mistake is take for granted that a child method should do something to another class/form, when it probably should rather expose a result that the caller will retrieve, remember the one that created the child already have the reference and is also usually the one that needs something from it and should get the "result" it ask for, not the other way around.
 
I can't switch back to a form i hide there was an error message. for example i used me.hide and form2.show on form1 and after doing something on form2 i wanted to switch back to form1 so i used me.hide and form1.show again.
what about this one? it says i can't load back a form i unloaded or something like that.
 
Is "form1" a reference to the form instance you hid?
 
Back
Top