Author Topic: Why does the VS IDE show this as an error?  (Read 8619 times)

0 Members and 1 Guest are viewing this topic.

sinc

  • Guest
Re: Why does the VS IDE show this as an error?
« Reply #15 on: May 13, 2007, 09:31:42 AM »
As others have said, the "Using" statement is basically a "short-cut" for this exact situation.  It's used A LOT in .NET programming, so you will be seeing it constantly in code written by others.  And since it's simpler, you will probably want to get familiar with it.

Both of the following snippets are equivalent:

Code: [Select]
Transaction tr = null;

try {
  tr = db.TransactionManager.StartTransaction();
  // more mojo here
} finally {
  tr.Dispose();
}

Code: [Select]
using (Transaction tr = db.TransactionManager.StartTransaction();
{
  // more mojo here
}

mkweaver

  • Bull Frog
  • Posts: 352
Re: Why does the VS IDE show this as an error?
« Reply #16 on: May 14, 2007, 08:49:08 AM »

Code: [Select]
Transaction tr = null;

try {
  tr = db.TransactionManager.StartTransaction();
  // more mojo here
} finally {
  tr.Dispose();
}

Code: [Select]
using (Transaction tr = db.TransactionManager.StartTransaction();
{
  // more mojo here
}

Okay, using sure gives cleaner code, but is there any way to capture an error thrown when the initialization of TR fails?



TonyT

  • Guest
Re: Why does the VS IDE show this as an error?
« Reply #17 on: May 14, 2007, 12:19:58 PM »
I wouldn't initialize the 'tr' variable inside the try block, because
that would cause the code in the finally block to call Dispose()
on it, no matter what. That's not exactly what you want.

As far as equating try/finally to what using() does, this would
be closer:

Code: [Select]

   Transaction tr = myDb.TransactionManager.StartTransaction()
   try
   {
        // use 'tr' here
   }
   finally
   {
        if( tr != null )
           tr.Dispose();
   }


If you want to test to see if the tr variable was correctly
initialized, you do something like this:

Code: [Select]

   Transaction tr = myDb.TransactionManager.StartTransaction()
   if( tr == null )
      throw new InvalidOperationException("No transaction!");
   try
   {
        // use 'tr' here
   }
   finally
   {
        if( tr != null )
           tr.Dispose();
   }


The odds that StartTransaction() will fail to return a valid
Transaction is slim-to-none, but if it ever did happen, your
code should not attempt to deal with it, because it means
something is wrong at the API level, rather than in your
own code.


Code: [Select]
Transaction tr = null;

try {
  tr = db.TransactionManager.StartTransaction();
  // more mojo here
} finally {
  tr.Dispose();
}

Code: [Select]
using (Transaction tr = db.TransactionManager.StartTransaction();
{
  // more mojo here
}

Okay, using sure gives cleaner code, but is there any way to capture an error thrown when the initialization of TR fails?




mkweaver

  • Bull Frog
  • Posts: 352
Re: Why does the VS IDE show this as an error?
« Reply #18 on: May 14, 2007, 01:34:10 PM »
Okay, it sounds like "using" is the way to go, then.

Thanks for the input everyone.

Mike Weaver
AlasCAD

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: Why does the VS IDE show this as an error?
« Reply #19 on: May 14, 2007, 01:36:27 PM »
Mkweaver

The using statement is not a replacement for try ...catch…finally. its purpose is to emit a dispose call on an object that that implements IDisposable. If you feel that a statement could potentially throw an exception, by all means wrap it with a try catch. Having said that, there is nothing wrong with having nested try…catch…finally blocks. Or sticking a “using” block inside of a try…catch. have fun  8-)

Dan