Error Module level form obj Mltiple forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    Error Module level form obj Mltiple forms

    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 B. Harlow [MVP - Outlook]

    #2
    Re: Error Module level form obj Mltiple forms

    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" <irviner@arc.lo srios.edu> wrote in message
    news:08e701c37e 4d$611f2b00$a30 1280a@phx.gbl.. .[color=blue]
    > 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?[/color]


    Comment

    • Bob

      #3
      Re: Error Module level form obj Mltiple forms

      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.
      [color=blue]
      >-----Original Message-----
      >Bob,
      >Sounds like spaghetti? :-)
      >
      >Can you post a sample of what you are doing? Along with[/color]
      which routine you[color=blue]
      >were calling when you got the stack overflow.
      >
      >Hope this helps
      >Jay
      >
      >"Bob" <irviner@arc.lo srios.edu> wrote in message
      >news:08e701c37 e4d$611f2b00$a3 01280a@phx.gbl. ..[color=green]
      >> 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[/color][/color]
      help?[color=blue]
      >
      >
      >.
      >[/color]

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Error Module level form obj Mltiple forms

        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:[color=blue]
        > Dim objForm1 As New Form1()[/color]
        [color=blue]
        > Dim objForm2 As New Form2()[/color]

        To:
        [color=blue]
        > Dim objForm1 As Form1
        > Dim objForm2 As Form2[/color]

        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" <irviner@arc.lo srios.edu> wrote in message
        news:014201c37e 66$daeb8d20$a00 1280a@phx.gbl.. .[color=blue]
        > 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.
        >[color=green]
        > >-----Original Message-----
        > >Bob,
        > >Sounds like spaghetti? :-)
        > >
        > >Can you post a sample of what you are doing? Along with[/color]
        > which routine you[color=green]
        > >were calling when you got the stack overflow.
        > >
        > >Hope this helps
        > >Jay
        > >
        > >"Bob" <irviner@arc.lo srios.edu> wrote in message
        > >news:08e701c37 e4d$611f2b00$a3 01280a@phx.gbl. ..[color=darkred]
        > >> 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[/color][/color]
        > help?[color=green]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        • Bob

          #5
          Re: Error Module level form obj Mltiple forms

          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
          [color=blue]
          >-----Original Message-----
          >Bob,
          >You are using object initialization syntax. Which is[/color]
          causing your problems.[color=blue]
          >
          >When you create a new Form1 a new form2 gets created,[/color]
          when a new form2 gets[color=blue]
          >created a new form1 gets created, which causes a new[/color]
          form2, which causes a[color=blue]
          >new form1, which will go on until you run out of stack[/color]
          space.[color=blue]
          >
          >Change these two lines:[color=green]
          >> Dim objForm1 As New Form1()[/color]
          >[color=green]
          >> Dim objForm2 As New Form2()[/color]
          >
          >To:
          >[color=green]
          >> Dim objForm1 As Form1
          >> Dim objForm2 As Form2[/color]
          >
          >Now the problem becomes how to initialize the variables.[/color]
          Normally I pass an[color=blue]
          >instance of the object to the constructor of the form[/color]
          (add an overloaded Sub[color=blue]
          >New). Unfortunately this will not work in this case as[/color]
          you have two forms[color=blue]
          >that mutually refer to each other. In that case I[/color]
          normally have the second[color=blue]
          >object accept the first as a parameter to the[/color]
          constructor, in the constuctor[color=blue]
          >of the second I call a property of the first to set the[/color]
          reference to the[color=blue]
          >second.
          >
          >Out of curiosity: What are you attempting to do, that[/color]
          you have each form[color=blue]
          >refer to the other? I suspect a Singleton Pattern might[/color]
          be a better[color=blue]
          >approach.
          >
          >Hope this helps
          >Jay
          >
          >
          >"Bob" <irviner@arc.lo srios.edu> wrote in message
          >news:014201c37 e66$daeb8d20$a0 01280a@phx.gbl. ..[color=green]
          >> Jay,
          >>
          >> The project has two forms with nothing on either of[/color][/color]
          them.[color=blue][color=green]
          >>
          >> 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[/color][/color]
          a[color=blue][color=green]
          >> stack overflow.
          >>[color=darkred]
          >> >-----Original Message-----
          >> >Bob,
          >> >Sounds like spaghetti? :-)
          >> >
          >> >Can you post a sample of what you are doing? Along[/color][/color][/color]
          with[color=blue][color=green]
          >> which routine you[color=darkred]
          >> >were calling when you got the stack overflow.
          >> >
          >> >Hope this helps
          >> >Jay
          >> >
          >> >"Bob" <irviner@arc.lo srios.edu> wrote in message
          >> >news:08e701c37 e4d$611f2b00$a3 01280a@phx.gbl. ..
          >> >> Declaring a module level form object in multiple[/color][/color][/color]
          froms[color=blue][color=green][color=darkred]
          >> >> within a project to show another project form[/color][/color][/color]
          causes a[color=blue][color=green][color=darkred]
          >> >> stack overflow and other assorted errors. Can[/color][/color][/color]
          anyone[color=blue][color=green]
          >> help?[color=darkred]
          >> >
          >> >
          >> >.
          >> >[/color][/color]
          >
          >
          >.
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Error Module level form obj Mltiple forms

            Bob,[color=blue][color=green]
            > >that mutually refer to each other. In that case I[/color]
            > normally have the second[color=green]
            > >object accept the first as a parameter to the[/color]
            > constructor, in the constuctor[color=green]
            > >of the second I call a property of the first to set the[/color]
            > reference to the[color=green]
            > >second.[/color][/color]
            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" <irviner@arc.lo srios.edu> wrote in message
            news:056e01c37e ea$f14ae410$a40 1280a@phx.gbl.. .[color=blue]
            > 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
            >[color=green]
            > >-----Original Message-----
            > >Bob,
            > >You are using object initialization syntax. Which is[/color]
            > causing your problems.[color=green]
            > >
            > >When you create a new Form1 a new form2 gets created,[/color]
            > when a new form2 gets[color=green]
            > >created a new form1 gets created, which causes a new[/color]
            > form2, which causes a[color=green]
            > >new form1, which will go on until you run out of stack[/color]
            > space.[color=green]
            > >
            > >Change these two lines:[color=darkred]
            > >> Dim objForm1 As New Form1()[/color]
            > >[color=darkred]
            > >> Dim objForm2 As New Form2()[/color]
            > >
            > >To:
            > >[color=darkred]
            > >> Dim objForm1 As Form1
            > >> Dim objForm2 As Form2[/color]
            > >
            > >Now the problem becomes how to initialize the variables.[/color]
            > Normally I pass an[color=green]
            > >instance of the object to the constructor of the form[/color]
            > (add an overloaded Sub[color=green]
            > >New). Unfortunately this will not work in this case as[/color]
            > you have two forms[color=green]
            > >that mutually refer to each other. In that case I[/color]
            > normally have the second[color=green]
            > >object accept the first as a parameter to the[/color]
            > constructor, in the constuctor[color=green]
            > >of the second I call a property of the first to set the[/color]
            > reference to the[color=green]
            > >second.
            > >
            > >Out of curiosity: What are you attempting to do, that[/color]
            > you have each form[color=green]
            > >refer to the other? I suspect a Singleton Pattern might[/color]
            > be a better[color=green]
            > >approach.
            > >
            > >Hope this helps
            > >Jay
            > >
            > >
            > >"Bob" <irviner@arc.lo srios.edu> wrote in message
            > >news:014201c37 e66$daeb8d20$a0 01280a@phx.gbl. ..[color=darkred]
            > >> Jay,
            > >>
            > >> The project has two forms with nothing on either of[/color][/color]
            > them.[color=green][color=darkred]
            > >>
            > >> 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[/color][/color]
            > a[color=green][color=darkred]
            > >> stack overflow.
            > >>
            > >> >-----Original Message-----
            > >> >Bob,
            > >> >Sounds like spaghetti? :-)
            > >> >
            > >> >Can you post a sample of what you are doing? Along[/color][/color]
            > with[color=green][color=darkred]
            > >> which routine you
            > >> >were calling when you got the stack overflow.
            > >> >
            > >> >Hope this helps
            > >> >Jay
            > >> >
            > >> >"Bob" <irviner@arc.lo srios.edu> wrote in message
            > >> >news:08e701c37 e4d$611f2b00$a3 01280a@phx.gbl. ..
            > >> >> Declaring a module level form object in multiple[/color][/color]
            > froms[color=green][color=darkred]
            > >> >> within a project to show another project form[/color][/color]
            > causes a[color=green][color=darkred]
            > >> >> stack overflow and other assorted errors. Can[/color][/color]
            > anyone[color=green][color=darkred]
            > >> help?
            > >> >
            > >> >
            > >> >.
            > >> >[/color]
            > >
            > >
            > >.
            > >[/color][/color]


            Comment

            • Bob

              #7
              Re: Error Module level form obj Mltiple forms

              Jay,

              Thanks for all your help! It is greatly appreciated.

              Comment

              Working...