Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has three textboxes on it. The first two do calculation and the answer goes into textbox3. I am trying to display a message that will only display if the value in textbox3 is 50 or greater. Here is my code:

C#
if (TextBoxHC50.Text < 50)
        {
            lblMessage.Text = "Your answer has increased more than 50%,";
        }
        else if (TextBoxHC50.Text > 50)
        {
            
        }


What am I doing wrong for this not to work?
Posted

Try converting your value first.
C#
int iTextBoxValue = Convert.ToInt32(TextBoxHC50.Text);
if (iTextBoxValue < 50) {
    lblMessage.Text = "Your answer has increased more than 50%,";

} else if (iTextBoxValue > 50) {
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Oct-14 12:49pm    
Convert should not be used. This is really the parsing. How do you take into account that the input is not valid, not parsable as Int32?
Please see my answer.
—SA
SteveyJDay 13-Oct-14 14:51pm    
My assumption was the input had been validated by this point in code.
Sergey Alexandrovich Kryukov 13-Oct-14 15:37pm    
No, it cannot be. If parsing is not yet done, it cannot be considered validated. If if is done, converting would be pointless, because the side effect of validation is always having some recognized integer value. If you think that validation can be done in some other way, think again. For example, even if the input is only digits (which can be achieved by filtering and would be very reasonable), it still can be invalid (if value is more than int.MaxValue). If you think that validation should be done separately from getting the value, also think again. Repeating potentially similar action is a sign of poor design. So, from all points of view, your advice is wrong.
—SA
Computer Wiz99 13-Oct-14 16:13pm    
That works for me. Question, what is the best way to round value?
Sergey Alexandrovich Kryukov 13-Oct-14 16:36pm    
Math.Round.
However, most likely, you don't need any rounding (which can be dangerous, because you can get loss of precision). Most likely, you need just proper string formatting, to show rounded value on screen. This is not the same. If you explain your problem (separate question, perhaps), I'll answer.
Do yourself a big favor: don't follow the advice in this answer.
—SA
You cannot compare a string with integer value, like your 50. (You should not also hard-code immediate constants; this is bad for maintenance; better declare explicit constants, do it just once.)

Also, you are not taking into account that the text box input can be not a valid string representing an integer value. Assuming you want to work with the int type, use int.TryParse. You could use int.Parse, too, but then get ready to handle possible exceptions.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900