It's probably because using() checks the using variable to ensure it is not
null before it calls Dispose() on it.
IOW, using is functionally equivalent to this:
{code}
IDisposable disposable = ...
try
{
// use disposable here
}
finally
{
if( disposable != null )
disposable.Dispose();
}
{code}
You can leverage that to have an object disposed of by using() conditionally
by just assigning the using variable to null.
{code}
using( IDisposable disposable = ... )
{
if( condition )
disposable = null; // supress call to Dispose()
}
{code}
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com
Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
"Paul Richardson"
wrote in message
news:6224873@discussion.autodesk.com...
The only difference in the following code in IL form is show below the code.
A few extra lines in finally block
and the 'callvirt' call in the finally block. Anyone know why the
difference? I've never had a problem with 'using'.
{code}
public class UsingVTryFinally
{
//class scope to make the IL cleaner to read.
private static readonly Document doc =
Application.DocumentManager.MdiActiveDocument;
private static Editor ed = doc.Editor;
private static void UsingTest()
{
PromptEntityResult per = ed.GetEntity("Select an entity: ");
using (Transaction tr =
doc.Database.TransactionManager.StartTransaction())
{
Entity bt =
(Entity)tr.GetObject
(doc.Database.BlockTableId, OpenMode.ForRead);
}
}
private static void TryTest()
{
PromptEntityResult per = ed.GetEntity("Select an entity: ");
Transaction tr =
doc.Database.TransactionManager.StartTransaction();
try
{
Entity bt =
(Entity)tr.GetObject
(doc.Database.BlockTableId, OpenMode.ForRead);
}
finally
{
tr.Dispose();
}
}
}
{code}
//using statement IL finally block
{code}
finally
{
IL_0043: ldloc.0
IL_0044: brfalse.s IL_004c
IL_0046: ldloc.0
IL_0047: callvirt instance void
[mscorlib]System.IDisposable::Dispose()
IL_004c: endfinally
} // end handler
{code}
//try finally finally block
{code}
finally
{
IL_0043: ldloc.0
IL_0044: callvirt instance void
[acdbmgd]Autodesk.AutoCAD.Runtime.DisposableWrapper::Dispose()
IL_0049: endfinally
} // end handler
{code}
wrote in message news:6224768@discussion.autodesk.com...
chandru.pachai said:
"And also remember that do not use using {} block when you open some object
using transaction.getObject() method, that will also cause you problem."
?!
What problem it could cause? Is that why sample codes from Autodesk use
Try...Catch...Finally transaction.Dispose() End Try? Is this some kind of
insider (or from insider) news?