Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

USING statement blocks

Status
Not open for further replies.

DarwinIT

Programmer
Apr 25, 2008
142
US
I have inherited a project that makes frequent use of USING blocks with sql connections and sql command executions. From what I understand - when you do this - connections will automatically be shut down when the process is run as well as all objects defined in that block will be disposed. If so - then an explicit close of the connection is superfluous, right? What is the advantage of having a nested using with connection and command? Seems redundant to me to have the second level of using?
 
Hi DarwinIT,

Only the object in the USING statement is disposed - not objects in the block.

So no - nesting USING statements isn't redindant.

In this example, myObj is disposed and obj2 isn't:
Code:
using(DisposeableObject myObj = new DisposeableObject()){
 DisposeableObject obj2 = new DisposeableObject();
}

Hope this helps,

Graeme


"Just beacuse you're paranoid, don't mean they're not after you
 
If I may interject another question here, does closing down a form dispose all of the objects within the form? I guess that can be extended to say, does closing a parent control dispose all of the child controls?
 
Ahh. That's a zebra of another colored stripe. Thanks for broadening my understanding of these rascally objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top