-1

I have an issue with a small piece of code I've made. For the code I have to make a small check. When the value of _mmdTextBox is bigger then 1999 it should give a MessageBx.Show("Value to high"). If the value is smaller then 0 there should be a MessageBox.Show("Value to low").

This is what I have made so far:

private void _mmdButton_Click(object sender, EventArgs e)
{
    var value = _mmdTextBox.Text;
    if (value > 1999 && value < 0)
    {
         MessageBox.Show("Value is to high");
    }
    else
    {
       // action
    }
}

This is the error that I get when I do it like the code above:

Error 1 Operator '>' cannot be applied to operands of type 'string' and 'int'

4

8 回答 8

11
int value;
if(Int32.TryParse(_mmdTextBox.Text, out value)
{
    if (value > 1999)
    {
        MessageBox.Show("Value is too high");
    }
    else if(value < 0)
    {
        MessageBox.Show("Value is too low");
    }
    else
    {
        // action
    }
}
else
{
    // not a number
}
于 2013-09-26T08:23:10.843 回答
1

TextBox.Text returns string. So your value will be string. You can't compare string with an integer using < or > operators.

From MSDN;

All numeric and enumeration types define....

Try to convert your value to int if it is available.

int value;
if(Int32.TryParse(_mmdTextBox.Text, out value)
{
    if (value > 1999)
    {
         MessageBox.Show("Value is too high");
    }
    if(value < 0)
    {
         MessageBox.Show("Value is too low");
    }
}
于 2013-09-26T08:21:29.283 回答
0

C# 是一种强类型语言,如果要比较 2 值。它们应该是同一类型。因此,您需要将文本框文本转换为 int,还需要进行验证以确保来自 text bot 的值是 int ok

我认为这对你有帮助

于 2013-09-26T08:28:15.627 回答
0

Your if statement could be changed to look like this:

var value = Convert.ToInt32(_mmdTextBox.Text);  //Convert to int in order to compare against an int

if (value > 1999)
{
     MessageBox.Show("Value is to high");
}
else if (value < 0)
{
     MessageBox.Show("Value is to low");
}
else
{
    //Action
}

You are comparing the value as type string to type int.

int.TryParse() would be a better option for the int conversion as the user could enter anything. (Like in Erno De Weerd's answer)

于 2013-09-26T08:22:50.060 回答
-1

您正在比较_mmdTextBox.Text哪个是string类型的常量int!是不可能的!您应该将前者转换为int

   int value;
   if(!int.TryParse(_mmdTextBox.Text, out value)) 
   {
       MessageBox.Show("Bad integer value in textbox");
       return;
   }
于 2013-09-26T08:23:32.860 回答
-1

您的问题是,该值是字符串而不是数字。您可以使用 Parse 或 TryParse 来解决它。

如果你想使用.TryParse()它,让它看起来像MSDN 中的这个例子

int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
    if (number > 1999 || number < 0)
    {
       MessageBox.Show("Value is invalid");
    }
    else
    {
       // action
    }
}
else
{
   //Show that the input is not a numeric value
}
于 2013-09-26T08:27:46.397 回答
-1

试试它而不是你的 if 条件

if (Convert.ToInt32(value) > 1999 && !Convert.ToInt32(value)< 0)
于 2013-09-26T08:28:20.183 回答
-1

怎么可能是 > 1999 AND < 0 ?

TryParse 可能更适合您的转换,但这应该有效:

private void _mmdButton_Click(object sender, EventArgs e)
        {
            var value = Convert.ToInt32(_mmdTextBox.Text);

            if (value > 1999)
            {
                MessageBox.Show("Value is too high");
            }
            else if (value < 0)
            {
                MessageBox.Show("Value is too low");
            }
            else
            {
                MessageBox.Show("Value is ok");
            }
        }
于 2013-09-26T08:25:41.350 回答