Thursday, March 22, 2012

Simple equality question

I have a TextBox.Text = Convert.ToString(Population). Population is an (int). I need an if statement to test TextBox.Text value is > 50000. I can't remember how to compare text values to int.

if ( TextBox1.Text > 50000 )
{
Label2.Text = " Greater";
{

Thank you,

Hi,

you can use the Convert.ToInt32() method to convert the text to an integer value and compare against 50000. Take a look at the documentation:http://msdn2.microsoft.com/en-us/library/sf1aw27b.aspx.

Grz, Kris.


if ( Convert.ToInt32(TextBox1.Text) > 50000 ){ Label2.Text =" Greater";{

Convert it back to an int before doing your check.

if (Int32.Parse(TextBox1.Text) > 50000){ Label2.Text =" Greater";}

Thank you - what is the difference between Convert.ToInt32 and Int32.Parse?


http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3689&SiteID=1


Int32.Parse is used when the Text value is a known string representation of a number. Convert.ToInt32 is used when the input type is unknown. Either way will work in this instance when the Text value is a known number.

0 comments:

Post a Comment