It is saying that input string was not in correct format, the line thats bold is the problem? Please tell me whats wrong with it, and how to fix it, thanks in advance.
{
double a;
double b;
double c;
a = double.Parse(textBox1.Text);
b = double.Parse(textBox2.Text);
c = Math.Sqrt((a * a) + (b * b));
label1.Text = c.ToString();
}
Loading
ashish JadhavPosted Jan 28, 2010, 3:24 AM
This will surely work...
double a;
double b;
double c;
a =Convert.ToDouble(textBox1.Text);
b = Convert.ToDouble(textBox2.Text);
c = Math.Sqrt((a * a) + (b * b));
label1.Text = c.ToString();
Nilanka DharmadasaPosted Jan 28, 2010, 2:00 AM
Change your code as follows.
double a;
double b;
double c;
if((double.TryParse(textBox1.Text, a)) && (double.TryParse(textBox2.Text, b))
{
c = Math.Sqrt((a * a) + (b * b));
label1.Text = c.ToString();
}
else
MessageBox.Show("You have entered an invalid input.");
If you find my answer useful, please tick 'Do you like this answer' checkbox.
Santhosh NPosted Jan 28, 2010, 1:10 AM
You can only parse values which are parsable but not anything you give...