[英]function return value, not all code paths return
嗨,我有这样的代码
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
kalimatSql = kalimatSql;
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows)
while (Global.reader.Read())
{
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
Global.dbCon.Close();
foreach (int id in idQuestions)
messageBox.Show(id.ToString());
我尝试将其编辑为函数(类),这样我可以多次调用它
private int sqlReader(string kalimatSql)
{
int sqlReader(string kalimatSql){
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows)
while (Global.reader.Read())
{
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
Global.dbCon.Close();
foreach (int id in idQuestions)
return id;
}
但是它不起作用,因为不是所有的代码路径都返回一个值...我想知道正确的方法是什么?
代码的最后一部分存在两个问题:
foreach (int id in idQuestions) {
return id;
}
}
这里的主要问题是,如果idQuestions
为空,则不会返回任何内容(这就是为什么收到此错误的原因)。 但是,即使它不为空,您的代码也会在您第一次输入foreach
的主体时返回,因此仅返回id列表中的第一个元素。
我假设您想返回id的整个列表,这很方便,因为您已经将它们全部放在List<>
。 只需将您的代码更改为此:
return idQuestions;
}
完整的方法应如下所示(注意方法签名):
private List<int> sqlReader(string kalimatSql) {
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
//return the ids
return idQuestions;
}
发生此错误的原因是,当您的最后一个“ foreach”循环运行零时间时会发生(或返回)什么?换句话说,当您的“ idQuestions”长度为零时,代码应返回什么值?
为了摆脱这种情况,您可以将此代码更改为
if(idQuestions.Count > 0)
{
foreach (int id in idQuestions) {
return id;
}
}
else
return -1;
如果idQuestions
没有任何元素idQuestions
。 foreach将永远不会被执行。
我想你正在从ur函数返回单个整数值
所以在foreach之后返回默认int。 您的算法完全错误。 如果您实现第二个代码版本,则仅返回第一个ID
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
int deft=0;
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach (int id in idQuestions) {
return id;
}
return deft;
最后一行:
foreach (int id in idQuestions) {
return id;
}
等于说if (idQuestions.Count > 0) { return idQuestions[0]; }
if (idQuestions.Count > 0) { return idQuestions[0]; }
。
该错误是因为您没有在代码流的末尾返回任何值。 有多种方法可以解决此问题,但是您需要弄清楚“默认”值是什么(例如null
或-1
。
您可以执行以下操作:
return (idQuestions.Count > 0 ? idQuestions[0] : -1)`
如果-1
是有效的返回值,或者如果您打算返回整个数组(如果无效,则返回null),则可以这样:
return (idQuestions.Count > 0 ? idQuestions : null)
希望能有所帮助
将return置于foreach循环内似乎很错误。
如果有多个结果,则可以返回IEnumerable
foreach(int id in idQuestions)
yield return id;
从您的代码看来,您正在尝试从数据存储中获取一些ID。 在这种情况下,根据您的代码,它将仅返回从您的数据存储区返回的第一个元素的ID。 如果我正确理解了您的问题,则代码应如下所示:
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
return idQuestions;
问题是您的唯一返回语句位于foreach循环内,如果您枚举的列表为空,则该循环的内容将永远不会执行。 如果无论如何要返回列表中的第一个值,为什么总有一个循环?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.