You just need to change the greater-than-or-equals sign (>=) to simply be a greater-than sign (>) within your if-statement :
//If "abc" is strictly greater than "xyz"
if (Convert.ToDouble(abc.Text) > Convert.ToDouble(xyz.Text))
{
//Display your messages
lblerrorMessage.Text = "Please correct the following:";
lblabc.Text = "Must be less than.";
return;
}
else
{
//Otherwise things are fine
lblerrorMessage.Text = "";
lblabc.Text = "";
}
Alternatively, you could actually check if both of the values were 0 (although the first solution should fit your needs) :
double abc = Convert.ToDouble(abc.Text);
double xyz = Convert.ToDouble(xyz.Text);
//If "abc" is greater than or equal to "xyz" and abc and xyz are non-zero
if ((abc >= xyz) && (abc != 0 && xyz != 0))
{
//Display your messages
lblerrorMessage.Text = "Please correct the following:";
lblabc.Text = "Must be less than.";
return;
}
else
{
//Otherwise things are fine
lblerrorMessage.Text = "";
lblabc.Text = "";
}
I realise that you have marked Rion's post as the answer, but be careful because the two code samples shown are not the same? The way your original post was worded does not seem to be satisfied by the first piece of sample code. Your original post seems
to say that you want to show an error message if abc >= xyz but not if both are zero. For example, is xyz==abc==3.0 then that is an error. The first code sample would not show an error message if abc and xyz were both 3.0. Is that case an error or not?
Your original post implies that it is an error but the code shown does not treat it as an error.
I think the second piece of code obeys the requirements you stated in the original post.
Ahhh ... the joy of edge cases and requirements specifications.
honeysuckle
Member
6 Points
25 Posts
If statement (textbox = 0)
Feb 27, 2013 07:00 PM|LINK
I have an IF Statement that says this:
if abc.text is greater than or equal to xyz.text, then lblErrorMessage and lblabc will show a message, Else lblErrorMessage and lblabc shows nothing.
But sometimes abc.text and xyz will be 0 and thats fine. How do I write that?
if (Convert.ToDouble(abc.Text) >= Convert.ToDouble(xyz.Text)) { lblerrorMessage.Text = "Please correct the following:"; lblabc.Text = "Must be less than."; return; } else { lblerrorMessage.Text = ""; lblabc.Text = ""; }Rion William...
All-Star
27896 Points
4618 Posts
Re: If statement (textbox = 0)
Feb 27, 2013 07:08 PM|LINK
You just need to change the greater-than-or-equals sign (>=) to simply be a greater-than sign (>) within your if-statement :
//If "abc" is strictly greater than "xyz" if (Convert.ToDouble(abc.Text) > Convert.ToDouble(xyz.Text)) { //Display your messages lblerrorMessage.Text = "Please correct the following:"; lblabc.Text = "Must be less than."; return; } else { //Otherwise things are fine lblerrorMessage.Text = ""; lblabc.Text = ""; }Alternatively, you could actually check if both of the values were 0 (although the first solution should fit your needs) :
double abc = Convert.ToDouble(abc.Text); double xyz = Convert.ToDouble(xyz.Text); //If "abc" is greater than or equal to "xyz" and abc and xyz are non-zero if ((abc >= xyz) && (abc != 0 && xyz != 0)) { //Display your messages lblerrorMessage.Text = "Please correct the following:"; lblabc.Text = "Must be less than."; return; } else { //Otherwise things are fine lblerrorMessage.Text = ""; lblabc.Text = ""; }honeysuckle
Member
6 Points
25 Posts
Re: If statement (textbox = 0)
Feb 27, 2013 07:12 PM|LINK
Ok, thanks!

Paul Linton
Star
13431 Points
2535 Posts
Re: If statement (textbox = 0)
Feb 27, 2013 09:14 PM|LINK
I realise that you have marked Rion's post as the answer, but be careful because the two code samples shown are not the same? The way your original post was worded does not seem to be satisfied by the first piece of sample code. Your original post seems to say that you want to show an error message if abc >= xyz but not if both are zero. For example, is xyz==abc==3.0 then that is an error. The first code sample would not show an error message if abc and xyz were both 3.0. Is that case an error or not? Your original post implies that it is an error but the code shown does not treat it as an error.
I think the second piece of code obeys the requirements you stated in the original post.
Ahhh ... the joy of edge cases and requirements specifications.