0

This question is really a follow up to an answer on another thread but is off topic to that original question so I am asking it in a new thread.

Why does CodeRush warn me of an Unused Declaration in code?

The question can stand on its own so the link to the original question is just a reference for the curious.

My question is simply this? Will the Save() Method ever return a false or is it always going to return true?

    public bool Save()
    {
        bool success = false;

        using (DataAccess.ExecuteDataTable("[dbo].[udp_Customers_ups]",
            DataAccess.Parameter(CustomerIdColumn, CustomerId),
            DataAccess.Parameter(CodeColumn, Code),
            DataAccess.Parameter(CompanyColumn, Company),
            DataAccess.Parameter(CommentsColumn, Comments),
            DataAccess.Parameter(ContactColumn, Contact),
            DataAccess.Parameter(StreetColumn, Street),
            DataAccess.Parameter(CityColumn, City),
            DataAccess.Parameter(StateColumn, State),
            DataAccess.Parameter(ZipcodeColumn, Zipcode),
            DataAccess.Parameter(PhoneColumn, Phone),
            DataAccess.Parameter(IsNewColumn, IsNew),
            DataAccess.Parameter(IsDeletedColumn, IsDeleted),
            DataAccess.Parameter(LastUpdatedColumn, LastUpdated),
            DataAccess.Parameter(UpdatedByColumn, UpdatedBy)))
        {
            success = true;
        }      

         return success;
    } 

I have tried to force a situation where the using wont work but every time I do I get a different error. So I am thinking that this method is always going true because anything that is going to cause it to fail is going to surface on the data layer and will never make it back here to return a false.

Community
  • 1
  • 1
Buck Hicks
  • 1,534
  • 16
  • 27
  • if you wrapped your whole using in a try catch block then the catch would execute and success would still be false - I'm not sure if that's what you're looking for? – NDJ Apr 04 '15 at 18:35

2 Answers2

2

This method will always return true or it will throw an exception because the statement

     return success;

is always preceded ("dominated") by

        success = true;

I'm suspecting the using statement is not quite what you were looking for here. Using insures that the dispose method is called on the object passed to the using statement. And the code inside the loop is executed no matter what. See https://msdn.microsoft.com/en-us/library/yh598w02.aspx for more info on the using statement.

Jeff Siver
  • 7,434
  • 30
  • 32
0

Your code is equivalent to

    public bool Save()
    {
        using (DataAccess.ExecuteDataTable("[dbo].[udp_Customers_ups]",
            DataAccess.Parameter(CustomerIdColumn, CustomerId),
            DataAccess.Parameter(CodeColumn, Code),
            DataAccess.Parameter(CompanyColumn, Company),
            DataAccess.Parameter(CommentsColumn, Comments),
            DataAccess.Parameter(ContactColumn, Contact),
            DataAccess.Parameter(StreetColumn, Street),
            DataAccess.Parameter(CityColumn, City),
            DataAccess.Parameter(StateColumn, State),
            DataAccess.Parameter(ZipcodeColumn, Zipcode),
            DataAccess.Parameter(PhoneColumn, Phone),
            DataAccess.Parameter(IsNewColumn, IsNew),
            DataAccess.Parameter(IsDeletedColumn, IsDeleted),
            DataAccess.Parameter(LastUpdatedColumn, LastUpdated),
            DataAccess.Parameter(UpdatedByColumn, UpdatedBy)))
        {
            return true;
        }      
    } 

This shows that the assignment of false to success is unused.

btlog
  • 4,760
  • 2
  • 29
  • 38