Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串转换为整数_C#_.net_String_Integer - Fatal编程技术网

C# 将字符串转换为整数

C# 将字符串转换为整数,c#,.net,string,integer,C#,.net,String,Integer,我需要代码方面的帮助。我只想在我的文本框中写数字/整数,并想在我的列表框中显示它 下面的代码是否正确?这似乎是一个错误 int yourInteger; string newItem; newItem = textBox1.Text.Trim(); if (newItem == Convert.ToInt32(textBox1.Text)) { listBox1.Items.Add(newItem); } ==== 更新: 这

我需要代码方面的帮助。我只想在我的文本框中写数字/整数,并想在我的列表框中显示它

下面的代码是否正确?这似乎是一个错误

    int yourInteger;
    string newItem;

    newItem = textBox1.Text.Trim();

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

这就是我的代码现在的样子。我的问题是,listBox可以处理数据类型“long”吗?因为当我输入20000000这个数字时,我只得到了一个20分钟的小时玻璃杯。但当我试着用这个控制台时,我得到了答案。所以我不确定哪种元素可以处理数据类型“long”

字符串newItem;
newItem=textBox1.Text.Trim();
Int64 num=0;
if(Int64.TryParse(textBox1.Text,out num))
{
对于(长i=2;i
如果要检查有效性:

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
使用int.TryParse()检查字符串是否包含整数值。

使用此选项:

    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";
    }

这假设您有一个customValidator。

是否检查空字符串

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

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

textBox1.Text
可能不包含整数的有效字符串表示形式(或仅为空字符串)。若要解决此问题,请使用
Int32.TryParse()
您可以执行以下操作:

Convert.ToInt32(input);
有关使用此功能的更长时间的功能,您可以查看:


基本上,它会检查字符串是否为null,然后调用int.Parse。这也会在WindowsCE下工作,WindowsCE没有int.TryParse。

具体来说,代码之所以无法编译,是因为您正在比较字符串(newItem)Convert.ToInt32是一个整数,它不允许您这样做。此外,如果传入的字符串不是数字,Convert.ToInt32将引发异常

您可以尝试使用
int.TryParse
,或者编写一个简单的正则表达式来验证输入:

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


谢谢Pat,我尝试了这个,但我得到了一个错误:“不能将运算符“==”应用于'string'和'int'类型的操作数。这很有效。但问题是:如果我键入字符,它不会给我任何消息,或者没有错误。这也可以在这里集成“try and catch”吗"?这是一种方法吗?它看起来如何?嗨,Mehrdad,我不知道如何在我的代码中做到这一点。也许你可以帮助我。ThanksIt不是long的问题。你正在做一个非常耗时和内存消耗的操作。写入控制台不需要更新包含大量元素的GUI对象。基本上,你在一个lis中显示了数百万个元素tbox没有任何实际用途(谁能滚动浏览这么长的列表?),并且消耗大量资源。嗨,Mehrdad,这只是一个学习的测试程序。谢谢你的建议
Convert.ToInt32(input);
int i;
bool isInteger = int.TryParse(textBox1.Text,out i);
bool isInteger = System.Text.RegularExpressions.Regex.IsMatch("^\d+$", textBox1.Text);