c#

我写了这样一个函数:
private bool CheckError()
{
 bool temp=false;
if(Convert.ToInt32(txt1.Text.Trim())<0)
{
temp=true;
return temp;
}
}
然后一运行,为什么总是有这样的错误呢:
并非所有的代码路径都返回值?急!

解决方案 »

  1.   

    private bool CheckError()
    {
     bool temp=false;
    if(Convert.ToInt32(txt1.Text.Trim())<0)
    {
    temp=true;
                                }
                                else
                                {
    return temp;
    }
    }
      

  2.   

    也就是说,如果if条件不满足时,就没有返因的数据了.所以你应该在if 外面写返回
    private bool CheckError()
    {
     bool temp=false;
    if(Convert.ToInt32(txt1.Text.Trim())<0)
    {
    temp=true;
    }
    return temp; }///*************************///
    这两年来慢慢习惯了光说不练
    ///*************************///
      

  3.   

    sorry
    private bool CheckError()
    {
    if(Convert.ToInt32(txt1.Text.Trim())<0)
    {
    return true;
    }
                                else
                                {
                                 return false;
                                 }
    }
      

  4.   

    建议写成private bool CheckError()
    {
    if(Convert.ToInt32(txt1.Text.Trim())<0)
    {
        return true;
    }
                               return false;
    }
    函数在遇到第一个return语句时就会返回了,没必要写else了.
      

  5.   

    private bool CheckError()
    {
     bool temp=false;
    if(Convert.ToInt32(txt1.Text.Trim())<0)
    {
    temp=true;
    }
    return temp;//这一句应该放在if语句的外面,否则当if的判断条件为false时,没有返回值了。。也就是报错信息说的。。并非所有的代码路径都返回值 }