名称'结果'在当前上下文中不存在

时间:2014-03-13 02:32:35

标签: c# compiler-errors

现在,当我运行该程序时,即使我输入了正确的operator,它也会给我operator错误消息。任何想法?
也谢谢你们的结果修复。非常感谢:)我认为我在某种程度上误用了IsValidData()方法定义的代码并在IsOperator(txtOperator, "+,-,*,/")区域中调用了错误。

   ` private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {

        try
        {
            //Set Validation
            if(IsValidData())
            {

                decimal Operand1 = Convert.ToDecimal (txtOperand1.Text);
                string Operator = Convert.ToString (txtOperator.Text);
                decimal Operand2 = Convert.ToDecimal(txtOperand2.Text);
                decimal result = Calculate(Operand1, Operator, Operand2);

                txtResult.Text = result.ToString("f4");
                txtOperand1.Focus();
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" +       ex.StackTrace, "Exception");
        }
    }
    //Set IsValidData()
    public bool IsValidData()
    {
        return
            IsPresent(txtOperand1, "Operand 1") &&
            IsDecimal(txtOperand1, "Operand 1") &&
            IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

            IsPresent(txtOperator, "Operator") &&
            IsOperator(txtOperator, "+,-,*,/") &&

            IsPresent(txtOperand2, "Operand 2") &&
            IsDecimal(txtOperand2, "Operand 2") &&
            IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
    }
    //Setup IsPresent
    public bool IsPresent(TextBox textBox, string name)
    {
        if (textBox.Text == "")
        {
            MessageBox.Show(name + " is required to continue.", "Entry Error");
            textBox.Focus();
            return false;
        }
        return true;

    }
    //Setup IsDecimal
    public bool IsDecimal(TextBox textBox, string name)
    {
        try
        {
            Convert.ToDecimal(textBox.Text);
            return true;
        }
        catch (FormatException)
        {
            MessageBox.Show(name + " must be a decimal value.", "Entry Error");
            textBox.Focus();
            return false;
        }
    }
    //Setup IsOperator
    public bool IsOperator(TextBox textBox, string operators)
    {
        try
        {
            foreach (string s in operators.Split(new char[] { ',' }))
            {
                if (textBox.Text.Trim() == s)
                    return true;
                else
                    throw new ArgumentException("The operator must be a valid operator: +,-, *, /", "name");
            }
            return true;

        }
        catch (ArgumentException ex)
        {
            MessageBox.Show(ex.Message);
            txtOperator.Focus();
            return false;
        }
    }
    //Setup IsWithinRange.
    public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
    {
        decimal number = Convert.ToDecimal(textBox.Text);
        if (number < min || number > max)
        {
            MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");
            textBox.Focus();
            return false;
        }
        return true;
    }
    //Setup Calculate Method.
    private decimal Calculate(decimal Operand1, string Operator, decimal Operand2)
    {
        if (Operator == "+")
        {
            decimal result = Operand1 + Operand2;
        }
        else if (Operator == "-")
        {
            decimal result = Operand1 - Operand2;
        }
        else if (Operator == "*")
        {
            decimal result = Operand1 * Operand2;
        }
        else
        {
            decimal result = Operand1 / Operand2;
        }
        return result;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}`

3 个答案:

答案 0 :(得分:2)

Calculate方法中,您已在result语句的每个部分中创建了一个名为if的新变量四次,但它在技术上并不是当你试图返回时,它存在(它超出了范围)。

if语句之前定义一次,然后设置值。

private decimal Calculate(decimal Operand1, string Operator, decimal Operand2)
{
    decimal result;

    if (Operator == "+")
    {
        result = Operand1 + Operand2;
    }
    else if (Operator == "-")
    {
        result = Operand1 - Operand2;
    }
    else if (Operator == "*")
    {
        result = Operand1 * Operand2;
    }
    else
    {
        result = Operand1 / Operand2;
    }
    return result;
}

答案 1 :(得分:1)

您需要在if / else语句范围之外声明您的变量。在If / Else语句中定义变量时,一旦函数退出if / else语句,该变量就会失去作用域。

decimal result;
if (Operator == "+")
        {
            result = Operand1 + Operand2;
        }
        else if (Operator == "-")
        {
            result = Operand1 - Operand2;
        }
        else if (Operator == "*")
        {
            result = Operand1 * Operand2;
        }
        else
        {
            result = Operand1 / Operand2;
        }
        return result;

答案 2 :(得分:0)

范围内没有名为result的变量。

  //Setup Calculate Method.
    private decimal Calculate(decimal Operand1, string Operator, decimal Operand2)
    {
        decimal result;
        if (Operator == "+")
        {
            result = Operand1 + Operand2;
        }
        else if (Operator == "-")
        {
            result = Operand1 - Operand2;
        }
        else if (Operator == "*")
        {
            result = Operand1 * Operand2;
        }
        else
        {
            result = Operand1 / Operand2;
        }
        return result;
    }