formArray close / dispose issue

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

    formArray close / dispose issue

    I have created myself an issue, and perhaps my idea wasn't as bright as I
    thought it was originally.
    If someone can help me figure out what the correct train of thought should
    be - or if there is a solution to this one.

    I created a dummy mdi app.
    In the main form ( frmMainScreen ) I created a variable as such:
    <written in notepad to trim everything down>

    Public Shared frmFormsOpen() As frmMyTestForm

    Now, in a totally seperate sub form opend from the main form I do this on a
    button click:

    LengthOfGames = (frmMainScreen. frmFormsOpen.Le ngth - 1)
    ReDim Preserve frmMainScreen.f rmFormsOpen(Len gthOfGames)

    frmMainScreen.f rmFormsOpen(Len gthOfGames) = New frmMyTestForm
    frmMainScreen.f rmFormsOpen(Len gthOfGames).Mdi Parent = Me.MdiParent
    'Remember that this form is also an mdi child of the main form.
    'Set my own property for later use
    frmMainScreen.f rmFormsOpen(Len gthOfGames).Gam eID = anIntegerValue

    frmMainScreen.f rmFormsOpen(Len gthOfGames).Sho w()

    'Works Great
    So in other code on other child forms forms I can do stuff like this:

    For Each SearchForm As frmMyTestForm In
    frmMainScreen.f rmFormsOpen
    If SearchForm.Game ID = mytestID Then 'mytestID is set to a
    value from something else
    SearchForm.Brin gToFront()
    Exit For
    End If
    Next


    My problem is this:

    where do i actually remove the array element when the form is closed. I do
    not want it to stay in the array.
    I do not think I can do it in the _FormClosed of the
    frmMainScreen.f rmFormsOpen(Len gthOfGames)._Fo rmClosed
    because I am actually in the form itself still - so I am assuming I cannot
    kill the object when I am still using the object.
    ( im trying to kill myself from myself ).

    That is my issue I have created myself and I was wondering if someone can
    point me in the right direction on how to get around this issue.

    Thanks,

    Miro



  • Armin Zingler

    #2
    Re: formArray close / dispose issue

    "Miro" <miro@beero.com schrieb
    where do i actually remove the array element when the form is
    closed. I do not want it to stay in the array.
    Do yourself a favor and use a List(Of frmMyTestForm) instead of the array.
    (which VB version?) Safes you from item shifting and redimming.
    I do not think I can do it in the _FormClosed of the
    frmMainScreen.f rmFormsOpen(Len gthOfGames)._Fo rmClosed
    because I am actually in the form itself still - so I am assuming I
    cannot kill the object when I am still using the object.
    ( im trying to kill myself from myself ).
    You don't kill anything by removing one reference. You can safely remove the
    item from the list in the FormClosed event. The object on which the current
    method is executed is never destroyed.


    Armin

    Comment

    • Miro

      #3
      Re: formArray close / dispose issue

      Its vb 2008

      Thank you for the post.
      I was under the impression - since I am 'sitting' in the object still during
      _FormClose then I cannot Remove my object from underneith my feet.

      Im assuming then by your statement
      >The object on which the current method is executed is never destroyed.
      That once I leave the form and it does close - it will be destroyed in the
      background - I dont need to run anything else?

      I will google and search up "List" to use a list instead of an array.

      Thank you,

      Miro

      "Armin Zingler" <az.nospam@free net.dewrote in message
      news:ub7GnmXOJH A.1164@TK2MSFTN GP02.phx.gbl...
      "Miro" <miro@beero.com schrieb
      >where do i actually remove the array element when the form is
      >closed. I do not want it to stay in the array.
      >
      Do yourself a favor and use a List(Of frmMyTestForm) instead of the array.
      (which VB version?) Safes you from item shifting and redimming.
      >
      >I do not think I can do it in the _FormClosed of the
      >frmMainScreen. frmFormsOpen(Le ngthOfGames)._F ormClosed
      >because I am actually in the form itself still - so I am assuming I
      >cannot kill the object when I am still using the object.
      >( im trying to kill myself from myself ).
      >
      You don't kill anything by removing one reference. You can safely remove
      the
      item from the list in the FormClosed event. The object on which the
      current method is executed is never destroyed.
      >
      >
      Armin
      >

      Comment

      • Armin Zingler

        #4
        Re: formArray close / dispose issue

        "Miro" <miro@beero.com schrieb
        Im assuming then by your statement
        >>The object on which the current method is executed is never destroyed.
        That once I leave the form and it does close - it will be destroyed in the
        background - I dont need to run anything else?
        Yes, nothing else is required.
        I will google and search up "List" to use a list instead of an array.
        Not required to google - look in the object browser. It's
        System.Collecti ons.Generic.Lis t(Of T)

        see also:



        Armin

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: formArray close / dispose issue

          Miro,

          You can find all the mdi forms by using
          \\\
          me.MDIParent.MD IChildren
          ///

          Gets an array of forms that represent the multiple-document interface (MDI) child forms that are parented to this form.


          Probably much easier,

          Cor


          "Miro" <miro@beero.com schreef in bericht
          news:Osau%23rWO JHA.3732@TK2MSF TNGP03.phx.gbl. ..
          >I have created myself an issue, and perhaps my idea wasn't as bright as I
          >thought it was originally.
          If someone can help me figure out what the correct train of thought should
          be - or if there is a solution to this one.
          >
          I created a dummy mdi app.
          In the main form ( frmMainScreen ) I created a variable as such:
          <written in notepad to trim everything down>
          >
          Public Shared frmFormsOpen() As frmMyTestForm
          >
          Now, in a totally seperate sub form opend from the main form I do this on
          a button click:
          >
          LengthOfGames = (frmMainScreen. frmFormsOpen.Le ngth - 1)
          ReDim Preserve frmMainScreen.f rmFormsOpen(Len gthOfGames)
          >
          frmMainScreen.f rmFormsOpen(Len gthOfGames) = New frmMyTestForm
          frmMainScreen.f rmFormsOpen(Len gthOfGames).Mdi Parent = Me.MdiParent
          'Remember that this form is also an mdi child of the main form.
          'Set my own property for later use
          frmMainScreen.f rmFormsOpen(Len gthOfGames).Gam eID = anIntegerValue
          >
          frmMainScreen.f rmFormsOpen(Len gthOfGames).Sho w()
          >
          'Works Great
          So in other code on other child forms forms I can do stuff like this:
          >
          For Each SearchForm As frmMyTestForm In
          frmMainScreen.f rmFormsOpen
          If SearchForm.Game ID = mytestID Then 'mytestID is set to a
          value from something else
          SearchForm.Brin gToFront()
          Exit For
          End If
          Next
          >
          >
          My problem is this:
          >
          where do i actually remove the array element when the form is closed. I
          do not want it to stay in the array.
          I do not think I can do it in the _FormClosed of the
          frmMainScreen.f rmFormsOpen(Len gthOfGames)._Fo rmClosed
          because I am actually in the form itself still - so I am assuming I cannot
          kill the object when I am still using the object.
          ( im trying to kill myself from myself ).
          >
          That is my issue I have created myself and I was wondering if someone can
          point me in the right direction on how to get around this issue.
          >
          Thanks,
          >
          Miro
          >
          >
          >

          Comment

          • Jack Jackson

            #6
            Re: formArray close / dispose issue

            You are confusing removing a reference to an object from a list with
            destruction of the object.

            An object can have many references. Removing those references does
            nothing other than, when all references have been removed, make the
            object available for garbage collection. It does not cause the object
            to be destroyed.

            And as Armin said, please don't use arrays for this. List (Of T) is
            much better.

            On Tue, 28 Oct 2008 23:57:20 -0400, "Miro" <miro@beero.com wrote:
            >Its vb 2008
            >
            >Thank you for the post.
            >I was under the impression - since I am 'sitting' in the object still during
            >_FormClose then I cannot Remove my object from underneith my feet.
            >
            >Im assuming then by your statement
            >>The object on which the current method is executed is never destroyed.
            >That once I leave the form and it does close - it will be destroyed in the
            >background - I dont need to run anything else?
            >
            >I will google and search up "List" to use a list instead of an array.
            >
            >Thank you,
            >
            >Miro
            >
            >"Armin Zingler" <az.nospam@free net.dewrote in message
            >news:ub7GnmXOJ HA.1164@TK2MSFT NGP02.phx.gbl.. .
            >"Miro" <miro@beero.com schrieb
            >>where do i actually remove the array element when the form is
            >>closed. I do not want it to stay in the array.
            >>
            >Do yourself a favor and use a List(Of frmMyTestForm) instead of the array.
            >(which VB version?) Safes you from item shifting and redimming.
            >>
            >>I do not think I can do it in the _FormClosed of the
            >>frmMainScreen .frmFormsOpen(L engthOfGames)._ FormClosed
            >>because I am actually in the form itself still - so I am assuming I
            >>cannot kill the object when I am still using the object.
            >>( im trying to kill myself from myself ).
            >>
            >You don't kill anything by removing one reference. You can safely remove
            >the
            >item from the list in the FormClosed event. The object on which the
            >current method is executed is never destroyed.
            >>
            >>
            >Armin
            >>

            Comment

            • Miro

              #7
              Re: formArray close / dispose issue

              Thank you -
              I have changed it to use the List(Of T) and it works great and 10x easier.

              I used an array because of my old dos day programming - arrays is all we
              had.

              Miro

              "Armin Zingler" <az.nospam@free net.dewrote in message
              news:ej2Ja1XOJH A.1488@TK2MSFTN GP03.phx.gbl...
              "Miro" <miro@beero.com schrieb
              >Im assuming then by your statement
              >>>The object on which the current method is executed is never destroyed.
              >That once I leave the form and it does close - it will be destroyed in
              >the
              >background - I dont need to run anything else?
              >
              Yes, nothing else is required.
              >
              >I will google and search up "List" to use a list instead of an array.
              >
              Not required to google - look in the object browser. It's
              System.Collecti ons.Generic.Lis t(Of T)
              >
              see also:

              >
              >
              Armin
              >

              Comment

              • Miro

                #8
                Re: formArray close / dispose issue

                I see,

                I was the understanding that if i say
                dim bla as new form1

                then I was under the impression the object instantiated and the object
                exists in bla which was dim'd here.

                To 'remove' it from this 'dim' would be pulling the rug from under me.

                As I understand your comment - the object "REFERENCE" is dim'd here, but the
                actaul object exists somewhere else, so I can
                "undim" my bla without worries. ( if undim was a word ) but basically
                remove it from bla.

                Miro

                "Jack Jackson" <jjackson@cinno vations.netwrot e in message
                news:sj2hg493kp ktrv5vjqr06b6a8 r8tab41gv@4ax.c om...
                You are confusing removing a reference to an object from a list with
                destruction of the object.
                >
                An object can have many references. Removing those references does
                nothing other than, when all references have been removed, make the
                object available for garbage collection. It does not cause the object
                to be destroyed.
                >
                And as Armin said, please don't use arrays for this. List (Of T) is
                much better.
                >
                On Tue, 28 Oct 2008 23:57:20 -0400, "Miro" <miro@beero.com wrote:
                >
                >>Its vb 2008
                >>
                >>Thank you for the post.
                >>I was under the impression - since I am 'sitting' in the object still
                >>during
                >>_FormClose then I cannot Remove my object from underneith my feet.
                >>
                >>Im assuming then by your statement
                >>>The object on which the current method is executed is never destroyed.
                >>That once I leave the form and it does close - it will be destroyed in the
                >>background - I dont need to run anything else?
                >>
                >>I will google and search up "List" to use a list instead of an array.
                >>
                >>Thank you,
                >>
                >>Miro
                >>
                >>"Armin Zingler" <az.nospam@free net.dewrote in message
                >>news:ub7GnmXO JHA.1164@TK2MSF TNGP02.phx.gbl. ..
                >>"Miro" <miro@beero.com schrieb
                >>>where do i actually remove the array element when the form is
                >>>closed. I do not want it to stay in the array.
                >>>
                >>Do yourself a favor and use a List(Of frmMyTestForm) instead of the
                >>array.
                >>(which VB version?) Safes you from item shifting and redimming.
                >>>
                >>>I do not think I can do it in the _FormClosed of the
                >>>frmMainScree n.frmFormsOpen( LengthOfGames). _FormClosed
                >>>because I am actually in the form itself still - so I am assuming I
                >>>cannot kill the object when I am still using the object.
                >>>( im trying to kill myself from myself ).
                >>>
                >>You don't kill anything by removing one reference. You can safely remove
                >>the
                >>item from the list in the FormClosed event. The object on which the
                >>current method is executed is never destroyed.
                >>>
                >>>
                >>Armin
                >>>

                Comment

                • Miro

                  #9
                  Re: formArray close / dispose issue

                  If I only had your brain to pick before I start to code :-)

                  Thanks,

                  Miro

                  "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                  news:uJ0%23R7aO JHA.4332@TK2MSF TNGP06.phx.gbl. ..
                  Miro,
                  >
                  You can find all the mdi forms by using
                  \\\
                  me.MDIParent.MD IChildren
                  ///
                  >
                  Gets an array of forms that represent the multiple-document interface (MDI) child forms that are parented to this form.

                  >
                  Probably much easier,
                  >
                  Cor
                  >
                  >
                  "Miro" <miro@beero.com schreef in bericht
                  news:Osau%23rWO JHA.3732@TK2MSF TNGP03.phx.gbl. ..
                  >>I have created myself an issue, and perhaps my idea wasn't as bright as I
                  >>thought it was originally.
                  >If someone can help me figure out what the correct train of thought
                  >should be - or if there is a solution to this one.
                  >>
                  >I created a dummy mdi app.
                  >In the main form ( frmMainScreen ) I created a variable as such:
                  ><written in notepad to trim everything down>
                  >>
                  >Public Shared frmFormsOpen() As frmMyTestForm
                  >>
                  >Now, in a totally seperate sub form opend from the main form I do this on
                  >a button click:
                  >>
                  >LengthOfGame s = (frmMainScreen. frmFormsOpen.Le ngth - 1)
                  >ReDim Preserve frmMainScreen.f rmFormsOpen(Len gthOfGames)
                  >>
                  >frmMainScreen. frmFormsOpen(Le ngthOfGames) = New frmMyTestForm
                  >frmMainScreen. frmFormsOpen(Le ngthOfGames).Md iParent = Me.MdiParent
                  >'Remember that this form is also an mdi child of the main form.
                  >'Set my own property for later use
                  >frmMainScreen. frmFormsOpen(Le ngthOfGames).Ga meID = anIntegerValue
                  >>
                  >frmMainScreen. frmFormsOpen(Le ngthOfGames).Sh ow()
                  >>
                  >'Works Great
                  >So in other code on other child forms forms I can do stuff like this:
                  >>
                  > For Each SearchForm As frmMyTestForm In
                  >frmMainScreen. frmFormsOpen
                  > If SearchForm.Game ID = mytestID Then 'mytestID is set to a
                  >value from something else
                  > SearchForm.Brin gToFront()
                  > Exit For
                  > End If
                  > Next
                  >>
                  >>
                  >My problem is this:
                  >>
                  >where do i actually remove the array element when the form is closed. I
                  >do not want it to stay in the array.
                  >I do not think I can do it in the _FormClosed of the
                  >frmMainScreen. frmFormsOpen(Le ngthOfGames)._F ormClosed
                  >because I am actually in the form itself still - so I am assuming I
                  >cannot kill the object when I am still using the object.
                  >( im trying to kill myself from myself ).
                  >>
                  >That is my issue I have created myself and I was wondering if someone can
                  >point me in the right direction on how to get around this issue.
                  >>
                  >Thanks,
                  >>
                  >Miro
                  >>
                  >>
                  >>
                  >
                  >

                  Comment

                  • Jack Jackson

                    #10
                    Re: formArray close / dispose issue

                    Making a variable no longer point at an object does not "pull the
                    rug". When all references to an object go away, the object becomes
                    eligible to be garbage collected at some time in the future.

                    Forms are a little different in that the framework keeps track of them
                    and has a reference to all open forms (Application.Op enForms), so even
                    if you clear your reference there is still one being held by the
                    framework.

                    For example:

                    Public Class MyClass
                    Public A As Integer = 0
                    End Class

                    Dim x As New MyClass ' There is one reference to an instance
                    ' of MyClass
                    Dim y as MyClass = x ' Now there are two references
                    ' to the instance
                    x = Nothing ' Now there is one reference (y)
                    y = Nothing ' Now there are no references and
                    ' the instance is eligible for garbage
                    ' collection.

                    On Thu, 30 Oct 2008 10:59:30 -0400, "Miro" <miro@beero.com wrote:
                    >I see,
                    >
                    >I was the understanding that if i say
                    >dim bla as new form1
                    >
                    >then I was under the impression the object instantiated and the object
                    >exists in bla which was dim'd here.
                    >
                    >To 'remove' it from this 'dim' would be pulling the rug from under me.
                    >
                    >As I understand your comment - the object "REFERENCE" is dim'd here, but the
                    >actaul object exists somewhere else, so I can
                    >"undim" my bla without worries. ( if undim was a word ) but basically
                    >remove it from bla.
                    >
                    >Miro
                    >
                    >"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                    >news:sj2hg493k pktrv5vjqr06b6a 8r8tab41gv@4ax. com...
                    >You are confusing removing a reference to an object from a list with
                    >destruction of the object.
                    >>
                    >An object can have many references. Removing those references does
                    >nothing other than, when all references have been removed, make the
                    >object available for garbage collection. It does not cause the object
                    >to be destroyed.
                    >>
                    >And as Armin said, please don't use arrays for this. List (Of T) is
                    >much better.
                    >>
                    >On Tue, 28 Oct 2008 23:57:20 -0400, "Miro" <miro@beero.com wrote:
                    >>
                    >>>Its vb 2008
                    >>>
                    >>>Thank you for the post.
                    >>>I was under the impression - since I am 'sitting' in the object still
                    >>>during
                    >>>_FormClose then I cannot Remove my object from underneith my feet.
                    >>>
                    >>>Im assuming then by your statement
                    >>>>The object on which the current method is executed is never destroyed.
                    >>>That once I leave the form and it does close - it will be destroyed in the
                    >>>background - I dont need to run anything else?
                    >>>
                    >>>I will google and search up "List" to use a list instead of an array.
                    >>>
                    >>>Thank you,
                    >>>
                    >>>Miro
                    >>>
                    >>>"Armin Zingler" <az.nospam@free net.dewrote in message
                    >>>news:ub7GnmX OJHA.1164@TK2MS FTNGP02.phx.gbl ...
                    >>>"Miro" <miro@beero.com schrieb
                    >>>>where do i actually remove the array element when the form is
                    >>>>closed. I do not want it to stay in the array.
                    >>>>
                    >>>Do yourself a favor and use a List(Of frmMyTestForm) instead of the
                    >>>array.
                    >>>(which VB version?) Safes you from item shifting and redimming.
                    >>>>
                    >>>>I do not think I can do it in the _FormClosed of the
                    >>>>frmMainScre en.frmFormsOpen (LengthOfGames) ._FormClosed
                    >>>>because I am actually in the form itself still - so I am assuming I
                    >>>>cannot kill the object when I am still using the object.
                    >>>>( im trying to kill myself from myself ).
                    >>>>
                    >>>You don't kill anything by removing one reference. You can safely remove
                    >>>the
                    >>>item from the list in the FormClosed event. The object on which the
                    >>>current method is executed is never destroyed.
                    >>>>
                    >>>>
                    >>>Armin
                    >>>>

                    Comment

                    • Miro

                      #11
                      Re: formArray close / dispose issue

                      Thank you - excellent example for me to understand. -makes perfect sense
                      now.

                      I appreciate your time.

                      Thank you again,

                      Miro

                      "Jack Jackson" <jjackson@cinno vations.netwrot e in message
                      news:64tjg4t86o uqp8eg9cvh40dmo 764hlcvd2@4ax.c om...
                      Making a variable no longer point at an object does not "pull the
                      rug". When all references to an object go away, the object becomes
                      eligible to be garbage collected at some time in the future.
                      >
                      Forms are a little different in that the framework keeps track of them
                      and has a reference to all open forms (Application.Op enForms), so even
                      if you clear your reference there is still one being held by the
                      framework.
                      >
                      For example:
                      >
                      Public Class MyClass
                      Public A As Integer = 0
                      End Class
                      >
                      Dim x As New MyClass ' There is one reference to an instance
                      ' of MyClass
                      Dim y as MyClass = x ' Now there are two references
                      ' to the instance
                      x = Nothing ' Now there is one reference (y)
                      y = Nothing ' Now there are no references and
                      ' the instance is eligible for garbage
                      ' collection.
                      >
                      On Thu, 30 Oct 2008 10:59:30 -0400, "Miro" <miro@beero.com wrote:
                      >
                      >>I see,
                      >>
                      >>I was the understanding that if i say
                      >>dim bla as new form1
                      >>
                      >>then I was under the impression the object instantiated and the object
                      >>exists in bla which was dim'd here.
                      >>
                      >>To 'remove' it from this 'dim' would be pulling the rug from under me.
                      >>
                      >>As I understand your comment - the object "REFERENCE" is dim'd here, but
                      >>the
                      >>actaul object exists somewhere else, so I can
                      >>"undim" my bla without worries. ( if undim was a word ) but basically
                      >>remove it from bla.
                      >>
                      >>Miro
                      >>
                      >>"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                      >>news:sj2hg493 kpktrv5vjqr06b6 a8r8tab41gv@4ax .com...
                      >>You are confusing removing a reference to an object from a list with
                      >>destruction of the object.
                      >>>
                      >>An object can have many references. Removing those references does
                      >>nothing other than, when all references have been removed, make the
                      >>object available for garbage collection. It does not cause the object
                      >>to be destroyed.
                      >>>
                      >>And as Armin said, please don't use arrays for this. List (Of T) is
                      >>much better.
                      >>>
                      >>On Tue, 28 Oct 2008 23:57:20 -0400, "Miro" <miro@beero.com wrote:
                      >>>
                      >>>>Its vb 2008
                      >>>>
                      >>>>Thank you for the post.
                      >>>>I was under the impression - since I am 'sitting' in the object still
                      >>>>during
                      >>>>_FormClos e then I cannot Remove my object from underneith my feet.
                      >>>>
                      >>>>Im assuming then by your statement
                      >>>>>The object on which the current method is executed is never destroyed.
                      >>>>That once I leave the form and it does close - it will be destroyed in
                      >>>>the
                      >>>>backgroun d - I dont need to run anything else?
                      >>>>
                      >>>>I will google and search up "List" to use a list instead of an array.
                      >>>>
                      >>>>Thank you,
                      >>>>
                      >>>>Miro
                      >>>>
                      >>>>"Armin Zingler" <az.nospam@free net.dewrote in message
                      >>>>news:ub7Gnm XOJHA.1164@TK2M SFTNGP02.phx.gb l...
                      >>>>"Miro" <miro@beero.com schrieb
                      >>>>>where do i actually remove the array element when the form is
                      >>>>>closed. I do not want it to stay in the array.
                      >>>>>
                      >>>>Do yourself a favor and use a List(Of frmMyTestForm) instead of the
                      >>>>array.
                      >>>>(which VB version?) Safes you from item shifting and redimming.
                      >>>>>
                      >>>>>I do not think I can do it in the _FormClosed of the
                      >>>>>frmMainScr een.frmFormsOpe n(LengthOfGames )._FormClosed
                      >>>>>because I am actually in the form itself still - so I am assuming I
                      >>>>>cannot kill the object when I am still using the object.
                      >>>>>( im trying to kill myself from myself ).
                      >>>>>
                      >>>>You don't kill anything by removing one reference. You can safely
                      >>>>remove
                      >>>>the
                      >>>>item from the list in the FormClosed event. The object on which the
                      >>>>current method is executed is never destroyed.
                      >>>>>
                      >>>>>
                      >>>>Armin
                      >>>>>

                      Comment

                      Working...