并非所有代码路径都具有返回值

Bul*_*vak 1 c#

这很奇怪,因为据我所知,该方法确实返回一个值或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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Aus*_*nen 9

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;
}
Run Code Online (Sandbox Code Playgroud)

所以你应该做这样的事情:

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;
Run Code Online (Sandbox Code Playgroud)