Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

removing string reference question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Goose14 - 26 Jul 2007 20:49 GMT
string SelectedSite
using (SiteLauncher SiteLaunch = new SiteLauncher()
           {
               SiteLaunch.ShowDialog();
               SelectedSite = SiteLaunch.SelectedPokerRoom; //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] - 26 Jul 2007 21:04 GMT
> string SelectedSite
> using (SiteLauncher SiteLaunch = new SiteLauncher()
[quoted text clipped - 8 lines]
> 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.

Signature

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

Peter Duniho - 26 Jul 2007 21:07 GMT
> string SelectedSite
> using (SiteLauncher SiteLaunch = new SiteLauncher()
[quoted text clipped - 8 lines]
> 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
Goose14 - 27 Jul 2007 17:44 GMT
> string SelectedSite
> using (SiteLauncher SiteLaunch = new SiteLauncher()
[quoted text clipped - 10 lines]
>
> Thanks

Thank you to both.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.