2014-03-13 8 views
0

이제 operator을 입력해도 프로그램을 실행할 때 operator 오류 메시지가 표시됩니다. 어떤 아이디어?
결과 수정에 대한 의견을 보내 주셔서 감사합니다. 그것은 크게 감사했다 :) 나는 어떻게 든 IsValidData() 메서드 정의에 대한 코드를 오용하고 IsOperator(txtOperator, "+,-,*,/") 영역에서 잘못 호출했다고 생각하고있다.이름 'result'이 (가) 현재 컨텍스트에 존재하지 않습니다.

` 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(); 
    } 
}` 

답변

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; 
+0

많은 도움을 많이주었습니다. – user3413431

+0

@ user3413431 - 도움을 주셔서 감사합니다. 가능하면 답변 중 하나를 수락대로 표시하십시오 (녹색 체크 표시를 클릭하여). 이를 통해 동일한 문제를 가진 다른 사람들이 최종 솔루션이 무엇인지 알 수 있습니다! – Tommy

+0

편집 된 문제를 해결하는 방법을 알고 계시나요? – user3413431

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; 
    } 
2

Calculate 방법에서는 if 문장의 각 부분에서, result 네 번이라는 새로운 변수를 만들었습니다,하지만 기술적으로 (이 범위 밖이다) 당신이 반환 할 때 존재하지 않는 그것.

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; 
} 
관련 문제