【问题标题】:Not all code paths have a return value并非所有代码路径都有返回值
【发布时间】:2012-06-21 19:26:57
【问题描述】:

这很奇怪,因为据我所知,该方法确实返回了一个值或 null...我之前使用 null 运行过它并且它有效...自从我在 if 语句中输入了这 2 个 if 语句,我收到错误“并非所有代码路径都有返回值”

    if (dt.Rows.Count != 0)
    {

        if (dt.Rows[0]["ReportID"].ToString().Length > 40)
        {

        string ReportID = dt.Rows[0]["ReportID"].ToString().Substring(0, 36);
        string ReportIDNumtwo = dt.Rows[0]["ReportID"].ToString().Substring(36, 36);
        MyGlobals1.versionDisplayTesting = ReportID;
        MyGlobals1.secondversionDisplayTesting = ReportIDNumtwo;
        return ReportID;

        }

        else if (dt.Rows[0]["ReportID"].ToString().Length < 39)
        {
            string ReportID = dt.Rows[0]["ReportID"].ToString();
            MyGlobals1.versionDisplayTesting = ReportID;
            return ReportID;
         }
    }
    else
    {
        return null;
    }
}

【问题讨论】:

  • else if 之后需要一个else
  • 报告ID为40怎么办?它不会返回...
  • 您在 3 分钟后得到 7 个答案 :-)
  • 永远不能是40,它们是预定的字符串长度
  • @Nadal 编译器不知道

标签: c#


【解决方案1】:
if (dt.Rows.Count != 0)
{
    ...

    else if (dt.Rows[0]["ReportID"].ToString().Length < 39)
    {
        string ReportID = dt.Rows[0]["ReportID"].ToString();
        MyGlobals1.versionDisplayTesting = ReportID;
        return ReportID;
     }

    // it's possible to get here without returning anything
}
else
{
    return null;
}

所以你应该这样做:

if (dt.Rows.Count != 0)
{
    ...

    else if (dt.Rows[0]["ReportID"].ToString().Length < 39)
    {
        string ReportID = dt.Rows[0]["ReportID"].ToString();
        MyGlobals1.versionDisplayTesting = ReportID;
        return ReportID;
     }
}

return null;

【讨论】:

  • 谢谢!我在想它会进入其中一个 if 语句,如果没有满足嵌套的 if 语句,它会转到外面的 else 语句,但我想那里有问题
【解决方案2】:

在第一个if 内,当两个条件都是false 时。例如,如果行数是 40。

【讨论】:

  • 我认为如果第一个 if 语句不为真,它会运行外部 else 语句,因为如果第一个 if 语句为真,那么嵌套的 if 语句之一也必须为真
【解决方案3】:

您在内部 if 语句中缺少 else

【讨论】:

    【解决方案4】:

    dt.Rows[0]["ReportID"].ToString().Length == 39dt.Rows[0]["ReportID"].ToString().Length == 40时没有返回值

    【讨论】:

      【解决方案5】:

      如果您删除最后的else 语句并保留return null;,它应该会消失。如果它到达一个内部的if 语句,它会返回一些东西,但如果它到达末尾,它将无法返回任何东西。

      if (dt.Rows.Count != 0){
          if (dt.Rows[0]["ReportID"].ToString().Length > 40)
          {
              string ReportID = dt.Rows[0]["ReportID"].ToString().Substring(0, 36);
              string ReportIDNumtwo = dt.Rows[0]["ReportID"].ToString().Substring(36, 36);
              MyGlobals1.versionDisplayTesting = ReportID;
              MyGlobals1.secondversionDisplayTesting = ReportIDNumtwo;
              return ReportID;
          }
          else if (dt.Rows[0]["ReportID"].ToString().Length < 39){
              string ReportID = dt.Rows[0]["ReportID"].ToString();
              MyGlobals1.versionDisplayTesting = ReportID;
              return ReportID;
          }
      }
      // If gets into if statement, but does not match inner conditional statements, it will end up here, if it were an else statement, return null will not get called, and a return will not be done
      return null;
      

      【讨论】:

        【解决方案6】:

        我会这样做:

        string ReportID = null;
        
        if (dt.Rows.Count != 0)
        {
        
            if (dt.Rows[0]["ReportID"].ToString().Length > 40)
            {
        
            ReportID = dt.Rows[0]["ReportID"].ToString().Substring(0, 36);
            string ReportIDNumtwo = dt.Rows[0]["ReportID"].ToString().Substring(36, 36);
            MyGlobals1.versionDisplayTesting = ReportID;
            MyGlobals1.secondversionDisplayTesting = ReportIDNumtwo;
        
            }
        
            else if (dt.Rows[0]["ReportID"].ToString().Length < 39)
            {
                ReportID = dt.Rows[0]["ReportID"].ToString();
                MyGlobals1.versionDisplayTesting = ReportID;
             }
        }
        
        return ReportID;
        

        原因:代码少,一个返回点。

        【讨论】:

          【解决方案7】:

          else if 之后,您还需要一个else

            if (dt.Rows.Count != 0)
              {
          
                  if (dt.Rows[0]["ReportID"].ToString().Length > 40)
                  {
          
                  string ReportID = dt.Rows[0]["ReportID"].ToString().Substring(0, 36);
                  string ReportIDNumtwo = dt.Rows[0]["ReportID"].ToString().Substring(36, 36);
                  MyGlobals1.versionDisplayTesting = ReportID;
                  MyGlobals1.secondversionDisplayTesting = ReportIDNumtwo;
                  return ReportID;
          
                  }
          
                  else if (dt.Rows[0]["ReportID"].ToString().Length < 39)
                  {
                      string ReportID = dt.Rows[0]["ReportID"].ToString();
                      MyGlobals1.versionDisplayTesting = ReportID;
                      return ReportID;
                  }
                  else
                  {
                      return null /* or something */
                  }
              }
              else
              {
                  return null;
              }
          }
          

          【讨论】:

          • 是多余的,但符合海报的编码风格。
          • 编码风格不对,这也是他一开始就有问题的原因。
          • '编码风格不对'...嗯?我认为它容易出错且冗长,但并没有错。
          • @0A0D 我记得有教授对这样的考试评分。我鄙视那些教授。
          • @0A0D 我知道这是多余的。我输入了else,以便@Nadal 清楚地知道还有另一种长度恰好等于40 的情况。这段代码显然需要重构,它超越了删除else 语句。
          猜你喜欢
          • 2011-12-17
          • 1970-01-01
          • 2021-11-08
          • 2013-10-06
          • 2016-02-14
          • 2014-04-02
          • 2019-05-26
          相关资源
          最近更新 更多