该参数如何改变其值?

时间:2016-11-19 08:47:27

标签: c# winforms

        int temp;
        res = int.TryParse(discountTextBox.Text, out temp);

        if (res == false)
        {
            MessageBox.Show("Enter Discount in integers", "Error");

            return;
        }
        double Discount = (double) temp;
        if (!transactiondao.ValidateDiscount(curremployee, Discount, retail))
        {
            MessageBox.Show("Invalid Discount entered", "error");
            return;
        }

上面的代码块调用下面的方法。但是,虽然调试折扣具有用户指定的值。但是,在执行if块之后,它会变为0。

public bool ValidateDiscount(Employee emp, double GivenDiscount, int retailprice)
    {
        bool res = false;
        double sMaxDiscount = emp.GetAuthorizedMaxDiscount();

        //validating against retail price
        double rMaxDiscount;
        if (retailprice == 2000)
            rMaxDiscount = 4;
        else if (retailprice == 3000)
            rMaxDiscount = 5;
        else if (retailprice == 4000)
            rMaxDiscount = 8;
        else
            rMaxDiscount = -1;

        double MaxDiscount;
        if (sMaxDiscount >= rMaxDiscount)
            MaxDiscount = sMaxDiscount;
        else
            MaxDiscount = rMaxDiscount;

        if (GivenDiscount < MaxDiscount)
        {
            res = true;
        }

        return res;
    }

这是对ValidateDiscount的调用。注释if块并因此绕过validatediscount工作完美,折扣保留用户输入的值。这是怎么发生的?

1 个答案:

答案 0 :(得分:1)

您将cache:warmup声明为temp,稍后您将int作为双重投放。这不会给出期望的结果。试试这个。

temp