이제 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();
}
}`
많은 도움을 많이주었습니다. – user3413431
@ user3413431 - 도움을 주셔서 감사합니다. 가능하면 답변 중 하나를 수락대로 표시하십시오 (녹색 체크 표시를 클릭하여). 이를 통해 동일한 문제를 가진 다른 사람들이 최종 솔루션이 무엇인지 알 수 있습니다! – Tommy
편집 된 문제를 해결하는 방법을 알고 계시나요? – user3413431