Reputation: 3
I have a problem with the following code.
bool TextBox2INT = true;
bool TextBox1INT = true;
int outputValue = 0;
int ButtonFind;
int TextBox1INT2 = Convert.ToInt32(TextBox1INT);
int TextBox2INT2 = Convert.ToInt32(TextBox2INT);
TextBox2INT = int.TryParse(textBox2.Text, out outputValue);
TextBox1INT = int.TryParse(textBox1.Text, out outputValue);
ButtonFind = (int)Math.Round((double)(TextBoxt1INT2 * 0.0333m * TextBox2INT2) + (double)(TextBox1INT2));
textBoxFind.Text = ButtonFind.ToString();
The problem is that the code works perfectly fine but when im debugging the answer in textBoxFind.Text is always 1.
Upvotes: 0
Views: 562
Reputation: 1499840
You're calling Convert.ToInt32(bool)
which will only ever give 0 or 1.
I think you meant:
int TextBox1INT2, TextBox2Int2;
bool textBox1Valid = int.TryParse(textBox2.Text, out TextBox1INT2);
bool textBox2Valid = int.TryParse(textBox1.Text, out TextBox2INT2);
You should also take action if int.TryParse
returns false - for example, telling the user to enter a valid number, instead of performing the calculation anyway. I'd also change your variable names, so you'd have something like:
int parsedTextBox1, parsedTextBox2;
bool textBox1Valid = int.TryParse(textBox2.Text, out parsedTextBox1);
bool textBox2Valid = int.TryParse(textBox1.Text, out parsedTextBox2);
if (!textBox1Valid || !textBox2Valid)
{
// Do something to warn the user here, e.g. a message box
return;
}
double result = (parsedTextBox1 * 0.0333m * parsedTextBox2) + parsedTextBox1;
textBoxFind.Text = ((int) Math.Round(result)).ToString();
Upvotes: 3