简体   繁体   English

C#foreach循环的条件和/或计算错误

[英]Condition and/or calculation bug with a C# foreach loop

I have a foreach loop in the code-behind file which is meant to evaluate and calculate for each item shown in a grid and update a total fee on the page. 我在代码隐藏文件中有一个foreach循环,用于评估和计算网格中显示的每个项目,并更新页面上的总费用。 The problem is that the calculation is only returning part of the answer. 问题在于计算只是返回答案的一部分。 The part it returns is correct, but it is not performing the full calculation. 它返回的部分是正确的,但是没有执行完整的计算。 I believe it is failing when it finds the first condition. 我认为找到第一个条件会失败。

The business logic is this: If an asset has a monthly or total cost less than or equal to $2000, then the cost of that asset is $1007.95 period, regardless of how many months it is checked out (refer to the duration spinner in the grid). 业务逻辑是这样的:如果asset的每月成本或总成本小于或等于$ 2000,则该asset的成本为$ 1007.95期限,而不管其签出了多少个月(请参阅网格中的duration器) )。

If an asset has a monthly or total cost greater than $2000, then the cost of that asset is its cost multiplied by the number of months ( duration ) it is checked out. 如果asset的每月成本或总成本大于$ 2000,则将该asset的成本乘以其成本乘以检出的月数( duration )。

I have inherited this bug & project, so I have included the full code for the foreach , and supplied two supporting screenshots below (I have had to obfuscate some of the information in the screenshots but relevant elements are shown). 我已经继承了这个错误和项目,所以我包括了foreach的完整代码,并在下面提供了两个支持的屏幕截图(我不得不混淆了屏幕截图中的某些信息,但显示了相关元素)。

The Code: 编码:

    private void PopulateFormFees(CheckOut c)
    {
        double dblTotalMonthlyFee = 0;
        double dblTotalCharge = 0; 

        c.CheckOutAssets.ForEach(delegate(CheckOutAsset coa)
        {
            if (coa.UsageFee < 2001)
            {
                dblTotalMonthlyFee = 1007.95;
                dblTotalCharge = 1007.95 * 1;
            }

            if (coa.UsageFee > 2000)
            {
                dblTotalMonthlyFee += coa.UsageFee;
                dblTotalCharge += (coa.UsageFee * coa.DurationMonths);
            }
        });

Screencaptures: 屏幕截图:

Screenshot 1 displays the form data on initial view. 屏幕快照1在初始视图上显示表单数据。 Here we can see that the Total Charge label is displaying $5,132.95 which indicates that the foreach loop is calculating for the $4,125.00 fee and one $1,007.95 fee, but not the others. 在这里,我们可以看到“总费用” label显示的是$ 5,132.95,这表明foreach循环正在为$ 4,125.00的费用和一项$ 1,007.95的费用进行计算,而其他项则没有。

总费用错误示例1

Screenshot 2 displays an example where duration was increased for each item. 屏幕截图2显示了一个示例,其中增加了每个项目的duration Here we see that the Total Charge label is displaying $21,632.95 which indicates that the foreach loop is calculating correctly for the $4,125.00 fee and for one $1,007.95 fee, but not the others. 在这里,我们看到“总费用” label显示的是$ 21,632.95,这表明foreach循环正在正确计算$ 4,125.00的费用和一项$ 1,007.95的费用,但其他项则没有。

总费用错误示例2

Thank you for your consideration and taking a look. 谢谢您的考虑和关注。

This just overwrites whatever values were stored in these variables before: 这只会覆盖之前存储在这些变量中的所有值:

dblTotalMonthlyFee = 1007.95;
dblTotalCharge = 1007.95 * 1;

Correct way is to add to current value, as in the other branch of the if : 正确的方法是添加到当前值,如if的另一个分支:

dblTotalMonthlyFee += 1007.95;
dblTotalCharge += 1007.95 * 1;

Change 更改

dblTotalMonthlyFee = 1007.95;
dblTotalCharge = 1007.95 * 1;

to

dblTotalMonthlyFee += 1007.95;
dblTotalCharge += 1007.95 * 1;

and it should work. 它应该工作。

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

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