这是非常奇怪的,因为据我所知,该方法确实返回一个值或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;
}
}
答案 0 :(得分: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;
}
所以你应该这样做:
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;
答案 1 :(得分:4)
在第一个if
内,当两个条件都是false
时。例如,如果行数是40。
答案 2 :(得分:2)
您在内部else
声明中错过了if
答案 3 :(得分:1)
如果您在结尾删除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;
答案 4 :(得分:1)
dt.Rows[0]["ReportID"].ToString().Length == 39
或dt.Rows[0]["ReportID"].ToString().Length == 40
答案 5 :(得分:1)
我会这样做:
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;
原因:代码少,返回点少。
答案 6 :(得分:0)
else
之后还需要一个else 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 /* or something */
}
}
else
{
return null;
}
}