C# problem with if statement

Bg computers / N/A
March 19, 2009 at 02:43:37
Specs: Windows XP, Quad Duo - 256mb
This is the code i am using on an if statement:

private void button1_Click(object sender, EventArgs e)

double twidth;
double thight;
double area;
double tpsm;

if ((textBox1.Text==null) || (textBox2.Text==null))
MessageBox.Show("Please enter both dimentions");
twidth = double.Parse(textBox1.Text);
thight = double.Parse(textBox2.Text);

area = twidth * thight;
tpsm = 10000 / ((twidth / 10) * (twidth / 10));

area = area / 100;

textBox4.Text = Convert.ToString(tpsm);
textBox3.Text = Convert.ToString(area);



The program has two text boxes that allow the imput of two variables. I am trying to prevent the program crashing if the boxes have nothing entered in them (as it cant parse a null val).

My if/else statement is meant to only get to the else part if both of the textboxes are not null. I have clearly gone wrong somewhere because it still gets to the else part if the boxes are not filled in.

Any suggestions?

See More: C# problem with if statement

Report •

March 19, 2009 at 12:12:59
I don't think the Text property of any text box is ever going to be null. It may be empty, but it's still a non-null reference. Try .Text == "".

Report •
Related Solutions

Ask Question