简体   繁体   English

Save()方法会返回false还是总是返回true?

[英]Will the Save() Method ever return a false or is it always going to return true?

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? 为什么CodeRush会警告我代码中未使用的声明?

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? Save()方法会返回false还是总是返回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. 因此,我认为该方法始终是正确的,因为任何会导致该方法失败的方法都会在数据层上浮出水面,并且永远不会使其返回此处以返回false。

This method will always return true or it will throw an exception because the statement 此方法将始终返回true或将引发异常,因为该语句

     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语句与您在此处查找的内容不完全相同。 Using insures that the dispose method is called on the object passed to the using statement. 使用确保在传递给using语句的对象上调用dispose方法。 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. 有关using语句的更多信息,请参见https://msdn.microsoft.com/en-us/library/yh598w02.aspx

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. 这表明没有将false分配给success

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM