Convert string to integer

瘦欲@ 提交于 2019-12-31 02:02:04

问题


I need help with my code. I would like to write only numbers/integers in my textbox and would like to display that in my listbox.

Is my code below in order? This seems to give an error.

    int yourInteger;
    string newItem;

    newItem = textBox1.Text.Trim();

    if (newItem == Convert.ToInt32(textBox1.Text))
    {
        listBox1.Items.Add(newItem);
    }

==== Update:

This is how my code looks like now. My question is, can listBox handle the data type "long"? Because when I entered the number 20,000,000 I just got an hour glass for 20 minutes. But when I tried this one with the console, I got the answer. So I'm not sure what kind of element can handle data type "long".

    string newItem;
    newItem = textBox1.Text.Trim();

    Int64 num = 0;
    if(Int64.TryParse(textBox1.Text, out num))
    {
        for (long i = 2; i <= num; i++)
        {
            //Controls if i is prime or not
            if ((i % 2 != 0) || (i == 2))
            {
                listBox1.Items.Add(i.ToString());
            }

        }
    }


    private void btnClear_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
    }

回答1:


Use this:

    int yourInteger;
    string newItem;

    newItem = textBox1.Text.Trim();
    Int32 num = 0;
    if ( Int32.TryParse(textBox1.Text, out num))
    {
        listBox1.Items.Add(newItem);
    }
    else
    {
        customValidator.IsValid = false;
        customValidator.Text = "You have not specified a correct number";
    }

This assumes you have a customValidator.




回答2:


int result = int.Parse(textBox1.Text.Trim());

If you want to check for validity:

int result;
if (int.TryParse(textBox1.Text.Trim(), out result)) // it's valid integer...
   // int is stored in `result` variable.
else
   // not a valid integer



回答3:


Use int.TryParse() to check if string contains integer value.




回答4:


textBox1.Text may not contain a valid string representation of an integer (or is just an empty string). To work around that, use Int32.TryParse().




回答5:


You can do:

Convert.ToInt32(input);

For a longer function using this you can look at: http://msdn.microsoft.com/en-us/library/bb397679.aspx

Basically it checks if the string is null, then it will call int.Parse. This will work under WindowsCE also, which doesn't have int.TryParse.




回答6:


To be specific as to why your code fails to compile it is because you are comparing a string (newItem) against the result of Convert.ToInt32, which is an integer, which it wont let you do. Also Convert.ToInt32 will raise an exception it the string passed in is not a number.

You can try using int.TryParse, or alternatively write a simple regular expression to validate your input:

int i;
bool isInteger = int.TryParse(textBox1.Text,out i);

or

bool isInteger = System.Text.RegularExpressions.Regex.IsMatch("^\d+$", textBox1.Text);



回答7:


Are you checking for an empty string?

int yourInteger;
string newItem;
newItem = textBox1.Text.Trim();

if(newItem != string.Empty)
{
   if ( newItem == Convert.ToInt32(textBox1.Text))
   {
      listBox1.Items.Add(newItem);
   }
}


来源:https://stackoverflow.com/questions/887586/convert-string-to-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!