Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

am developing c# windows application
by this i have got few suggestion my code below

my flow is:
am calculating normal,average,high.

if my textbox value <=12 means i need to get "you got an normal marks", message
if my textbox value between 13 to 20 means i need to get "you got an average marks",message
if my textbox value >=21 means i need to get "you got an high marks",message

but in this concept is not working in my code below ,
so please review the below my code and let me know where i would made mistake in my code,
so that i can able to recorrect my mistake.

my code :

C#
private void button2_Click(object sender, EventArgs e)
     {
         if (textBox1.Text <= "12")
         {
             label3.Text = "afected";
         }
         else if (textBox1.Text == "20")
         {
             label3.Text = "average";
         }
         else if (textBox1.Text >= "21")
         {
             label3.Text = "normal";
         }


     }




thanks in advance..
Posted

1 solution

textBox1.Text <= "12"


This is incorrect. You have a string value (textBox1.Text), on which you are trying to make a number comparison with.

You have to transform your string value to an integer one to be able to compare.

So :

int value; // Here we decalre our integer variable

bool ok = int.TryParse(textBox1.Text, out value); // Here we try to get an integer value from the textbox

if (ok)
{
   if (value <= 12)
   {
      label3.Text = "afected";
   }
   else if (value <= 20) // Here you have to check for an inequality, not an equality to 20
   {
      label3.Text = "average";
   }
   else // Here will match the case where value > 20
   {
      label3.Text = "normal";
   }
}
else
   label3.Text = "textBox1.Text does not represent a valid integer";


Hope this helps
 
Share this answer
 
Comments
stellus 21-Mar-13 7:16am    
thank you very much phil.
the code which was provide its working fine my side.
thank you once again a lot..
phil.o 21-Mar-13 7:19am    
You're welcome ; but the most important is to understand why it was not working, and how it does work now. Hope you will apply this knowledge/understanding in your future developments.
And, please, could you vote if that was helpful ? Thanks ;)
H.Brydon 21-Mar-13 12:21pm    
Silence from OP, +5 from me.
phil.o 21-Mar-13 12:24pm    
Thanks :)

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