I need help with my Code Its driving me CRAZY
well, I am to design a form(a calculator but without buttons when selecting an operator +-*/) that accepts an operand(such as a number) and operator(+-*/) and a second operand(another number) and then gives the result, I then
need to have it show an error message if an invalid operator is enter by the user. My problem is even if the right operator +,*,-,/ is entered it still shows an error
message stating its an invalid???
this has really been driving me crazy i can't seem to figure it out??
I really think its the isOperator method that is causing the issues??
HTML Code:
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
string Operator1 = Convert.ToString(txtOperator.Text);
decimal result = 0m;
result = Calculate(operand1, Operator1, operand2);
txtResult.Text = result.ToString("n4");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
}
private decimal Calculate(decimal operand1, string Operator1, decimal operand2)
{
if (Operator1 == "+")
{
operand1 += operand2;
}
else if (Operator1 == "-")
{
operand1 -= operand2;
}
else if (Operator1 == "*")
{
operand1 *= operand2;
}
else if (Operator1 == "/")
{
operand1 /= operand2;
}
{
return operand1;
}
}
public bool IsPresent(TextBox textbox, string name)
{
if (textbox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textbox.Focus();
return false;
}
return true;
}
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;
}
}
public bool IsOperator(TextBox textbox, string name)
{
string validOperator1 = "";
try
{
validOperator1 = Convert.ToString(textbox.Text);
if (validOperator1 != "+" | validOperator1 != "-" | validOperator1 != "*" | validOperator1 != "/")
{
MessageBox.Show(name + " Must be a Valid Operator", "Entry Error");
textbox.Focus();
}
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
return true;
}
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;
}
public bool IsValidData()
{
return
IsPresent(txtOperand1, "Operand 1") &&
IsDecimal(txtOperand1, "Operand 1") &&
IsWithinRange(txtOperand1, "Operand 1", 1, 999) &&
IsPresent(txtOperator, "Operator") &&
IsOperator(txtOperator, "Operator") &&
IsPresent(txtOperand2, "Operand 2") &&
IsDecimal(txtOperand2, "Operand 2") &&
IsWithinRange(txtOperand2, "Operand 2", 1, 999);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void ClearResult(object sender, EventArgs e)
{
txtResult.Text = "";
}
}
}
Re: I need help with my Code Its driving me CRAZY
You're saying that it's not valid if it's not equal to * or it's not equal to - or it's not equal to * or it's not equal to /. If the user enters one of those four operators then one of your conditions will be true but the other three will be false, so the whole expression will be false. You should be using logical AND operators, not logical OR operators. Only if all four conditions is true is the value invalid.
By the way, it's very rare that you should be using the logical | and & operators. You should almost exclusively use the || and && operators because they short-circuit. You should only use the single versions when you specifically don't want to short-circuit, but such code would generally be considered poor in C# anyway.
Re: I need help with my Code Its driving me CRAZY
Quote:
Originally Posted by
jmcilhinney
You're saying that it's not valid if it's not equal to * or it's not equal to - or it's not equal to * or it's not equal to /. If the user enters one of those four operators then one of your conditions will be true but the other three will be false, so the whole expression will be false. You should be using logical AND operators, not logical OR operators. Only if all four conditions is true is the value invalid.
By the way, it's very rare that you should be using the logical | and & operators. You should almost exclusively use the || and && operators because they short-circuit. You should only use the single versions when you specifically don't want to short-circuit, but such code would generally be considered poor in C# anyway.
Thanks your Awesome!!!
:)
Re: I need help with my Code Its driving me CRAZY
Quote:
Originally Posted by
QuestionPlease
Thanks your Awesome!!!
:)
Yes... yes I am. ;)