gpt4 book ai didi

c# - 并非所有代码路径都有返回值

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:13 31 4
gpt4 key购买 nike

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

最佳答案

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;

关于c# - 并非所有代码路径都有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11145368/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com