Inherting Forms and stack overload

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

    #1

    Inherting Forms and stack overload


    CustDisplay Form (parent form) has the following declaration:

    Public Class CustDisplayForm
    Inherits System.Windows. Forms.Form
    'Dim addCustsForm As New CustAddForm
    Dim editCustForm As New EditCustomerFor m

    'Varibles for the database location and how to access it
    Const providerStr = "Provider='Micr osoft.JET.OLEDB .4.0';"
    Const dataSourceStrin g = "Data Source='C:\Docu ments and
    Settings\jfitzp atrick\My Documents\Teste r.mdb'"

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeCompo nent()

    'Add any initialization after the InitializeCompo nent() call

    End Sub

    EditCustForm has the following declartion:
    ublic Class EditCustomerFor m
    Inherits EditCustomersPr oject.CustDispl ayForm

    #Region " Windows Form Designer generated code "

    ' Public Sub New()
    'MyBase.New()

    'This call is required by the Windows Form Designer.
    ' InitializeCompo nent()

    'Add any initialization after the InitializeCompo nent() call
    ' End Sub

    The problem is that when I run the programme which has custDisplayForm
    laoding as the main form, it gets stuck into a never ending loop. The
    loop consits of the follwoing sub rountines being called over and over
    again
    Cust

    EditCustomersPr oject.CustDispl ayForm.New()
    EditCustomersPr oject.EditCusto merForm.EditCus tomerForm()

    The inhertiatance was created by me ad new a new inherited form using
    the wizards.


    --
    Madman
    ------------------------------------------------------------------------
    Madman's Profile: http://www.hightechtalks.com/m146
    View this thread: http://www.hightechtalks.com/t2271131

  • Mattias Sjögren

    #2
    Re: Inherting Forms and stack overload

    Madman,
    [color=blue]
    >The problem is that when I run the programme which has custDisplayForm
    >laoding as the main form, it gets stuck into a never ending loop. The
    >loop consits of the follwoing sub rountines being called over and over
    >again
    >Cust
    >
    >EditCustomersP roject.CustDisp layForm.New()
    >EditCustomersP roject.EditCust omerForm.EditCu stomerForm()[/color]

    Right, because of this line
    [color=blue]
    >Dim editCustForm As New EditCustomerFor m[/color]

    Why is it there? You don't seem to be using editCustForm.


    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Larry Lard

      #3
      Re: Inherting Forms and stack overload


      Madman wrote:[color=blue]
      > CustDisplay Form (parent form) has the following declaration:
      >
      > Public Class CustDisplayForm
      > Inherits System.Windows. Forms.Form
      > 'Dim addCustsForm As New CustAddForm
      > Dim editCustForm As New EditCustomerFor m[/color]

      OK, so this means that part of the operation of creating a
      CustDisplayForm is creating a new EditCustomerFor m

      [...][color=blue]
      > EditCustForm has the following declartion:
      > ublic Class EditCustomerFor m
      > Inherits EditCustomersPr oject.CustDispl ayForm[/color]

      And this means that an EditCustomerFor m is a particular kind of
      CustDisplayForm .

      So when you try and start your project with a CustDisplayForm , in
      creating that, we must create an EditCustomerFor m, but an
      EditCustomerFor m is a kind of CustDisplayForm , so we have created a
      loop: Creating a CustDisplayForm involves creating a CustDisplayForm .

      You can see the same problem more simply if you just create a Form1 and
      put

      Public f As New Form1

      in its declarations - trying to create such a form involves infinite
      recursion.

      The correct solution depends on your intentions, but I suspect just
      taking the New out of the declaration of editCustomForm is what you
      want:

      Dim editCustForm As EditCustomerFor m

      because a CustDisplayForm doesn't *always* have a valid editCustForm,
      does it?

      Alternatively you might want to consider if EditCustomerFor m should be
      inheriting from CustDisplayForm (is an edit form a particular kind of
      display form) - if you are implementing a common look-and-feel to your
      forms, then they should in fact they should both be inheriting from a
      common base form.


      --
      Larry Lard
      Replies to group please

      Comment

      • Madman

        #4
        Re: Inherting Forms and stack overload


        What I require is form the displaycustfrom to display the editcusfrom.
        The reason why I used inheriance is because I wanted to use all the
        functionality of displaycsutform but with an extra fucntionality.

        If I use the line
        Dim editCustForm As EditCustomerFor m

        Then then call editCustForm.sh ow() to display the form, I get the
        following runtime mesage: Object reference not set to an instance of an
        object.

        Which means I sitll need to create an isntance of the editCustForm
        object

        Put simpy I woudl like custDisplayForm is be the parent and
        editCustForm to be the child of custDisplayForm


        --
        Madman
        ------------------------------------------------------------------------
        Madman's Profile: http://www.hightechtalks.com/m146
        View this thread: http://www.hightechtalks.com/t2271131

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Inherting Forms and stack overload

          Madman,
          Each time you create a CustDisplayForm , you create a new EditCustomerFor m.

          Public Class CustDisplayForm
          Inherits System.Windows. Forms.Form

          Dim editCustForm As New EditCustomerFor m

          ...
          End Class

          Seeing as EditCustomerFor m inherits from CustDisplayForm

          Public Class EditCustomerFor m
          Inherits EditCustomersPr oject.CustDispl ayForm
          ...
          End Class

          A "CustDisplayFor m" is created as part of EditCustomerFor m (a single object,
          the "inner" part is CustDisplayForm the "outer" part is "EditCustomerFo rm"_,
          which in turn creates a new EditCustomerFor m, which in turn creates a
          "CustDisplayFor m" as part of EditCustomerFor m, which in turn creates a new
          EditCustomerFor m, which in turn creates a "CustDisplayFor m"... and so on ...
          until you get the stack overflow!

          Rather then define "editCustFo rm As New EditCustomerFor m" as a field at the
          class level, define it in the sub where you display it.

          Private menuEditCustome r_Click(...) Handles menuEditCustome r.Click
          Dim editCustForm As New EditCustomerFor m
          editCustForm.Sh ow(...)
          End Sub

          --
          Hope this helps
          Jay [MVP - Outlook]
          ..NET Application Architect, Enthusiast, & Evangelist
          T.S. Bradley - http://www.tsbradley.net


          "Madman" <Madman.1xkg1z@ no-mx.forums.yourd omain.com.au> wrote in message
          news:Madman.1xk g1z@no-mx.forums.yourd omain.com.au...
          |
          | What I require is form the displaycustfrom to display the editcusfrom.
          | The reason why I used inheriance is because I wanted to use all the
          | functionality of displaycsutform but with an extra fucntionality.
          |
          | If I use the line
          | Dim editCustForm As EditCustomerFor m
          |
          | Then then call editCustForm.sh ow() to display the form, I get the
          | following runtime mesage: Object reference not set to an instance of an
          | object.
          |
          | Which means I sitll need to create an isntance of the editCustForm
          | object
          |
          | Put simpy I woudl like custDisplayForm is be the parent and
          | editCustForm to be the child of custDisplayForm
          |
          |
          | --
          | Madman
          | ------------------------------------------------------------------------
          | Madman's Profile: http://www.hightechtalks.com/m146
          | View this thread: http://www.hightechtalks.com/t2271131
          |


          Comment

          • Larry Lard

            #6
            Re: Inherting Forms and stack overload


            Madman wrote:[color=blue]
            > What I require is form the displaycustfrom to display the editcusfrom.
            > The reason why I used inheriance is because I wanted to use all the
            > functionality of displaycsutform but with an extra fucntionality.
            >
            > If I use the line
            > Dim editCustForm As EditCustomerFor m
            >
            > Then then call editCustForm.sh ow() to display the form, I get the
            > following runtime mesage: Object reference not set to an instance of an
            > object.
            >
            > Which means I sitll need to create an isntance of the editCustForm
            > object[/color]

            Yes - so create one *when you need it*. If you declare As New at form
            level, that means *every* custDisplayForm *automatically* creates for
            itself an EditCustomerFor m, which is what is causing the loop. Note
            that the edit form you create *is* (by virtue of the inheritance) a
            display form itself, but it does NOT have an edit form itself!

            So declare without the New, then just say

            editCustForm = New EditCustomerFor m()

            when you actually want one to exist. It is important that you
            understand this difference between declaration and creation.

            --
            Larry Lard
            Replies to group please

            Comment

            • Madman

              #7
              Re: Inherting Forms and stack overload


              :D Thank you Larry for you help. The code you have supplied me works.


              --
              Madman
              ------------------------------------------------------------------------
              Madman's Profile: http://www.hightechtalks.com/m146
              View this thread: http://www.hightechtalks.com/t2271131

              Comment

              Working...