Using an unassigned local variable. But always falls into the task

with this code, I don’t understand why, if the assignment of the variable in the finally block does not understand that ALWAYS will be assigned. I think that I do not have a valid option when the currency will not be assigned. If you know, it will be great to understand why. appreciate it!

Thank!

CurrencyVO currency;

try
{
     if (idConnection.HasValue && idConnection != 0)
     {
         currencyConnection = client.GetConnection(idConnection.Value);
         model.Connection = currencyConnection;
     }
     else 
     {
         int providerUserKey = (int)Models.UserModel.GetUser().ProviderUserKey;
         currencyConnection = client.GetConnection(providerUserKey);
     }                        
     currency = model.Currencies.SingleOrDefault(c => c.IdCountry == currencyConnection.idcountry) ?? new CurrencyVO();    
} 
catch
{
      currency = new CurrencyVO();                    
} 
finally
{
      model.PublishedContainer.Currency = currency;
}

the error occurs on the finally block. If I pull it out of the finally block as follows:

                } catch {
                    currency = new CurrencyVO();
                }
                model.PublishedContainer.Currency = currency;

It works great.

+5
source share
4 answers

, #, ( ) - , , . , finally , http://msdn.microsoft.com/en-us/library/aa691181.aspx:

try stt :

try try-block finally finally-block

  • v try v stmt.
  • v finally- , v stmt.
  • ...

, , currency try, finally.

+8

, new CurrencyVO() catch? -!

+1

, . , catch . , #. , . . finally . .

, , . .

+1

catch CurrencyVO: /. , : currency . .

edit: :

CurrencyVO currency = null;

:

if (currency != null)
   model.PublishedContainer.Currency = currency;

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/a994a0ff-432b-4d23-b7d2-838b0b961de0

0

All Articles