removing string reference question

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

    #1

    removing string reference question

    string SelectedSite
    using (SiteLauncher SiteLaunch = new SiteLauncher()
    {
    SiteLaunch.Show Dialog();
    SelectedSite = SiteLaunch.Sele ctedPokerRoom; //this is
    a property that returns a string
    }

    My question is, if after the using block is over, will the SiteLaunch
    object be able to dispose itself? Or does it stay alive because of the
    open reference with SelectedSite. If this is the case, is there a
    better way of doing this?

    Thanks

  • Jon Skeet [C# MVP]

    #2
    Re: removing string reference question

    Goose14 <gusabdala@gmai l.comwrote:
    string SelectedSite
    using (SiteLauncher SiteLaunch = new SiteLauncher()
    {
    SiteLaunch.Show Dialog();
    SelectedSite = SiteLaunch.Sele ctedPokerRoom; //this is
    a property that returns a string
    }
    >
    My question is, if after the using block is over, will the SiteLaunch
    object be able to dispose itself? Or does it stay alive because of the
    open reference with SelectedSite. If this is the case, is there a
    better way of doing this?
    No, it's fine. SiteLaunch may refer to the string, but the string won't
    refer to the SiteLaunch.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Peter Duniho

      #3
      Re: removing string reference question

      On Thu, 26 Jul 2007 12:49:05 -0700, Goose14 <gusabdala@gmai l.comwrote:
      string SelectedSite
      using (SiteLauncher SiteLaunch = new SiteLauncher()
      {
      SiteLaunch.Show Dialog();
      SelectedSite = SiteLaunch.Sele ctedPokerRoom; //this is
      a property that returns a string
      }
      >
      My question is, if after the using block is over, will the SiteLaunch
      object be able to dispose itself? Or does it stay alive because of the
      open reference with SelectedSite. If this is the case, is there a
      better way of doing this?
      SelectedSite doesn't refer to the SiteLauncher instance. It refers to the
      string that the SiteLauncher instance returned. So I don't see any reason
      that SelectedSite would in any way affect the lifetime of SiteLaunch, or
      in any way be affected by the disposal of SiteLaunch.

      That said, suppose you have actually referenced the SiteLauncher instance
      somehow and retained that reference outside the "using" statement block.
      For example, instead of referencing something simple like a string that is
      immutable and doesn't contain a reference back to anything else, perhaps
      you had a reference to something that was modified by SiteLauncher during
      disposal, or which kept a reference to the SiteLauncher object.

      Then you would in fact have a problem. My understanding is that not only
      "will the SiteLaunch object be able to dispose itself", that's exactly
      what the "using" statement always does. It always disposes the object,
      before exiting the "using" statement. So if the retained object was
      modified by disposal of the SiteLauncher instance, or you were somehow
      able to get the SiteLauncher instance reference from the retained object,
      that would be an issue. The simple answer is to not do that. :)

      Pete

      Comment

      • Goose14

        #4
        Re: removing string reference question

        On Jul 26, 2:49 pm, Goose14 <gusabd...@gmai l.comwrote:
        string SelectedSite
        using (SiteLauncher SiteLaunch = new SiteLauncher()
        {
        SiteLaunch.Show Dialog();
        SelectedSite = SiteLaunch.Sele ctedPokerRoom; //this is
        a property that returns a string
        }
        >
        My question is, if after the using block is over, will the SiteLaunch
        object be able to dispose itself? Or does it stay alive because of the
        open reference with SelectedSite. If this is the case, is there a
        better way of doing this?
        >
        Thanks
        Thank you to both.

        Comment

        Working...