开发者

check for the bigger value in c#

boxes containing numbers and i want to check if the value of the first text box is bigger than the othe开发者_如何学Cr like:

if (textbox1.Text > textbox2.Text){// do stuff}

so how can i make something like this.


You need to parse the text in the textbox to an int first:

if (int.Parse(textbox1.Text) > int.Parse(textbox2.Text)){// do stuff}

But beware: This will throw an exception, if any of the texts entered can't be parsed to an int. The cleaner approach would be to use TryParse if you can't be sure that the entered text is always in the correct format:

int number1;
int number2;

if(int.TryParse(textbox1.Text, out number1)
   && int.TryParse(textbox2.Text, out number2)
   && number1 > number2)
{
    // Do stuff
}


double value1=Double.Parse(textBox1.Text);
double value2=Double.Parse(textBox2.Text);
if (value1>value2)
 .......

Would this do your job or you need something more?


You simply need to convert the strings to numbers. Assuming we 're talking about integers and the range of an int suits you, you can do this with:

var value1 = int.Parse(textBox1.Text);
var value2 = int.Parse(textBox2.Text);

if (value1 > value2) { ... }

However, this will leave you exposed to exceptions resulting from the conversion (e.g. if a text is not a number but a string). In this case, TryParse allows you to detect this situation and respond accordingly.

int value1, value2;
if (!int.TryParse(textBox1.Text, out value1)) {
    // textBox1 does not hold a number
}
else if (!int.TryParse(textBox2.Text, out value2)) {
    // textBox2 does not hold a number
}
else {
    // OK, we have both numbers:
    if (value1 > value2) { ... }
}


Since everyone is assuming integers, I'll be the odd man out...

double value1, value2;

Double.TryParse(textBox1.Text, out value1);
Double.TryParse(textBox2.Text, out value2);

if(value1 < value2 ){ ... }


int value1 = int.Parse(textbox1.Text);
int value2 = int.Parse(textbox2.Text);

if (value1  > value2 )
{
   // do stuff
}


int text1 = Convert.ToInt32(textBox1.Text);
int text2 = Convert.ToInt32(textBox2.Text);
if(text1 < text2)
{
// do something
}


if ( Convert.ToInt32( textbox1.Text) > Convert.ToInt32( textbox2.Text))

{// do stuff}

or

if (int.Parse(textbox1.Text) > int.Parse(textbox2.Text))

{// do stuff}


Over complicated FTW?

if (CompareNumbers(textbox1.Text,textbox2.Text) > 0){// do stuff}

int CompareNumbers( string s1, string s2 )
{
    if ( s1 == s2 )
    {
        return 0;
    }
    if ( s1 == null )
    {
        return -1;
    }
    if ( s2 == null )
    {
        return 1;
    }
    if ( s1.Length != s2.Length )
    {
        return ( s1.Length - s2.Length );
    }
    for ( int index = 0; index < s1.Length; index++ )
    {
        int ch1, ch2, compare;

        ch1 = ( s1[index] - 0x30 );
        ch2 = ( s2[index] - 0x30 );
        compare = 0;
        if ( ch1 < 0 || ch1 > 9 )
        {
            compare = -1;
        }
        if ( ch2 < 0 || ch2 > 9 )
        {
            compare += 1;
        }
        if ( compare != 0 )
        {
            return compare;
        }
        if ( ch1 != ch2 )
        {
            return ( ch1 - ch2 );
        }
    }
    return 0;
}


Considering you are dealing with textbox (and so I suppose the text is inserted by the user) I would suggest using int.TryParse approach:

private bool Validate()
{
   int v1 = 0;
   int v2 = 0;
   if (!int.TryParse(tb1.Text, out v1) || int.TryParse(tb2.Text, out v2))
      return false;

   // Make the comparison
   return v1 > v2;
}

More concise version (but less clear, at least for me):

private bool Validate()
{
   int v1 = 0;
   int v2 = 0;
   return int.TryParse(tb1.Text, out v1) && 
          int.TryParse(tb2.Text, out v2) && 
          v1 > v2;
}


Use the CompareValidator class:

    <asp:CompareValidator 
ID="cmpAmount" 
runat="server" 
ControlToCompare='TextBox2'
ControlToValidate="TextBox1"
Type="Double"
Operator="GreaterThanEqual" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜