如何解决“并非所有的代码路径都返回值”?

yyybbbfff 2010-01-14 02:41:17
提示我不是“并非所有的代码路径都返回值”?该怎么解决啊?

DataTable dt = dbo.RunSqlDataTable("select * from PageUrlSet where id = '" + System.Web.HttpContext.Current.Request.QueryString["ID"] + "'");
string[] strTemp = dt.Rows[0][10].ToString().Split(new char[] { ',' }); //拆分字符串
for (int i = 0; i < strTemp.Length; i++)
{
DataTable dt1 = dbo.RunSqlDataTable("select * from userlist where id = '" + System.Web.HttpContext.Current.Session["ID"].ToString() + "'");
string UserUnit = dt.Rows[0][5].ToString();

if (strTemp[i] == UserUnit)
{
return null;

}
else
{
string URL = "<script>parent.location='LoadPage.apsx';</script>";
return URL ;
//System.Web.HttpContext.Current.Response.Write("<script>parent.location='LoadPage.apsx';</script>");

}
}
...全文
1134 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
-汪帆- 2010-01-14
  • 打赏
  • 举报
回复
当strTemp.Length = 0 for循环不执行的时候,程序就没有返回点了。
建议在函数的开始处写一个返回变量,在函数的结尾返回它。
 
string returnValue;
.......
for (int i = 0; i < strTemp.Length; i++)
{
// 其实if这里可以省略了
if (strTemp[i] == UserUnit)
{
returnValue = null;
}
else
{
string URL = " <script>parent.location='LoadPage.apsx'; </script>";
returnValue = URL ;
}
}
return returnValue;
yyybbbfff 2010-01-14
  • 打赏
  • 举报
回复
问题已解决,结贴,谢谢大家。
houyizhinv 2010-01-14
  • 打赏
  • 举报
回复

public string cc(int a)
{
if (a == 4)
{
return null;
}
else
{
string URL = " <script>parent.location='LoadPage.apsx'; </script>";
return URL;

}
}

你看我这个 和你的返回是差不多的吧 没有报错
houyizhinv 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yyybbbfff 的回复:]
如果在最外面加return null的话,所有的判断都不起作用了
[/Quote]
有用的

但是 为什么我写的和你的差不多 我的可以呢 奇怪
yyybbbfff 2010-01-14
  • 打赏
  • 举报
回复
如果在最外面加return null的话,所有的判断都不起作用了
  • 打赏
  • 举报
回复
其实这个消息已经明确说明了问题所在,你将所有的逻辑走一下,就知道在什么情况下,没有返回值了。
wzp144650 2010-01-14
  • 打赏
  • 举报
回复
少个return
yankunlin 2010-01-14
  • 打赏
  • 举报
回复
gz
cnfcnf 2010-01-14
  • 打赏
  • 举报
回复
去掉Else
hhc123 2010-01-14
  • 打赏
  • 举报
回复

else
{
string URL = " <script>parent.location='LoadPage.apsx'; </script>";
return URL ;
//System.Web.HttpContext.Current.Response.Write(" <script>parent.location='LoadPage.apsx'; </script>");

}
return ...

houyizhinv 2010-01-14
  • 打赏
  • 举报
回复
好像不是 再看看
houyizhinv 2010-01-14
  • 打赏
  • 举报
回复

else
{
string URL = " <script>parent.location='LoadPage.apsx'; </script>";
return URL ;
//System.Web.HttpContext.Current.Response.Write(" <script>parent.location='LoadPage.apsx'; </script>");

}

你这个句子里面没有返回值
xray2005 2010-01-14
  • 打赏
  • 举报
回复
方法结束前, 加一句 return null;
龙翔飞雪 2010-01-14
  • 打赏
  • 举报
回复
方法结束前, 加一句 return null;

慎用!!! 请确定你的程序流程符合业务逻辑
wazdoyang 2010-01-14
  • 打赏
  • 举报
回复
你最外面定義個變量,把return 變量 也放在最外面 for 裏 給變量賦值就行了

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧