并非所有代码路径返回值

编程入门 行业动态 更新时间:2024-10-27 16:30:59
本文介绍了并非所有代码路径返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

亲爱的朋友们, 我写了这样的代码来检查访问级别。 但我得到的错误是并非所有代码路径返回值 请建议相同的原因和解决方案

Dear Friends, I have written a code like this to check the access level. But I am getting the error as "Not All Code Path Return A Value" Kindly suggest the reason and solution for the same

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (isAccessLevel() == "YES") { Show_My_Data() } else { Hide_My_Data() } } } public string isAccessLevel() { if (ACC_YN == "YES") { if (STA_YN == "RELEASE") { return "YES"; } else { return "NO"; } } }

推荐答案

isAccessLevel未从所有分支返回值。把它改成这样, isAccessLevel is not returning the value in from all branches. Change it to like this, public string isAccessLevel() { if (ACC_YN == "YES") { if (STA_YN == "RELEASE") { return "YES"; } else { return "NO"; } } else { return "NO"; } }

你应该在if else循环之外有返回变量。想想会发生什么,如果ACC_YN = false。它会返回什么? You should have return variable outside of if else loop. think what will happen,if ACC_YN=false. what it will return? public string isAccessLevel() { string returnvalue="No"; if (ACC_YN == "YES") { if (STA_YN == "RELEASE") { returnvalue="Yes"; } else { returnvalue="No"; } } return returnvalue; }

上面的推理是合理的,我建议使用以下代码片段。 The reasoning is sound above, I would suggest the following code snippets. public string isAccessLevel() { accessLevel = "NO"; if (ACC_YN == "YES" && STA_YN == "RELEASE") accessLevel = "YES"; return accessLevel; }

更多推荐

并非所有代码路径返回值

本文发布于:2023-11-10 01:21:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573975.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   返回值   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!