【问题标题】:cannot implicity convert string to int [duplicate]无法将字符串隐式转换为 int [重复]
【发布时间】:2014-10-03 18:46:00
【问题描述】:
        TextBox1.Enabled = false;
        finalpricebox.Items.Clear();
        namebox.Items.Clear();
        int current = 0;
        pricebox.Items.Clear();
        if (CheckBox1.Checked == true)
        {
            request.Navigate("http:----------" + TextBox1.Text);
        }
        else if (CheckBox1.Checked == false)
        {
            request.Navigate("http://----" + TextBox1.Text);
        }
        namebox.Focus();
        while (!(request.ReadyState == WebBrowserReadyState.Complete))
        {
            Application.DoEvents();
        }
        WebClient tClient = new WebClient();

        int resultnr = request.Document.GetElementById("searchResults_total").OuterText;
        if (resultnr > 30)
        {
            resultnr = 30;
        }

它说不能将字符串隐式转换为 int。就行了

int resultnr = request.Document.GetElementById("searchResults_total").OuterText; 如果(结果> 30)

为什么会出现这个错误,希望有人能帮帮我

【问题讨论】:

  • 具体在哪一行?

标签: c# string int converter


【解决方案1】:
int resultnr = Convert.ToInt32(request.Document.GetElementById("searchResults_total").OuterText);

【讨论】:

  • 感谢您的快速答复:D
  • 为了将来参考(对于遇到这篇文章的其他人),您能否添加一些上下文和解释代码的作用?
【解决方案2】:

为了安全起见,推荐使用Int32.TryParse

  int resultnr =0;
  if(int.TryParse(request.Document.GetElementById("searchResults_total").OuterText,out resultnr )
   {
        if (resultnr > 30)
        {
            resultnr = 30;
        }
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多