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 / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

DirectoryEntry.Close vs DirectoryEntry.Dispose

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ssg31415926 - 30 Aug 2005 20:44 GMT
When I've finished with a DirectoryEntry object, should I be calling
.Close() or .Dispose() or both?

I thought I read something when I first started using .NET saying you
should always call .Dispose() if it's been implemented on your class
when you've finished with it.

Then, the other day, I came across an article which said that you
should never call .Dispose() and now I'm a bit confused.

Can anyone shed some light on this, please?
Willy Denoyette [MVP] - 30 Aug 2005 21:25 GMT
> When I've finished with a DirectoryEntry object, should I be calling
> .Close() or .Dispose() or both?
[quoted text clipped - 7 lines]
>
> Can anyone shed some light on this, please?

I would suggest you called Close(), internally Close calls Dispose(), but
much better would be to use the 'using' statement like this:

   using (DirectoryEntry de = ...)
   {
   ....
   } // Here de will automaticlly be disposed of.

Willy.
ssg31415926 - 31 Aug 2005 07:07 GMT
I've not seen this 'using' syntax before but I like the look of it.  Is
this specifically for defining the part of your code in which the
object is used or does it have other purposes?
Chris Taylor - 30 Aug 2005 21:30 GMT
Hi,

Dispose method of DirectoryEntry calls the Close method. Ultimately Dispose
will also remove the DirectoryEntry from the owning container, however the
container will take care of this when it is it self disposed.

In short calling either Close or Dispose will achieve the desired effect of
releasing the underlying unmanaged resource. Personally I go with Close
since it reads better for me.

Hope this helps

Signature

Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

> When I've finished with a DirectoryEntry object, should I be calling
> .Close() or .Dispose() or both?
[quoted text clipped - 7 lines]
>
> Can anyone shed some light on this, please?
ssg31415926 - 31 Aug 2005 07:16 GMT
Could you possibly help me on this "owning container" - what is it and
how is it used?  I'm self taught (both C# and OO and probably have big
gaps in my knowlegde) and I haven't come across this before.

Feel free to point me in the direction of some material or a good book
that's beyond the basics.  Now that I've done the "Introduction to C#"
- level stuff, I'm not sure where to go (and I have to say that at the
Intro level, Microsoft's own: C# Step by step was pretty readable - I
particularly liked the way it taught you C# in the first half and
ignored Windows Forms/Web Forms stuff until the second half).

SSG
Jon Skeet [C# MVP] - 31 Aug 2005 07:19 GMT
> Dispose method of DirectoryEntry calls the Close method. Ultimately Dispose
> will also remove the DirectoryEntry from the owning container, however the
[quoted text clipped - 3 lines]
> releasing the underlying unmanaged resource. Personally I go with Close
> since it reads better for me.

However, calling Close don't have any built-in language support, so you
need to manually write a try/finally statement. Calling Dispose is as
simple as writing a using statement.

Signature

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


Rate this thread:







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.