并非所有代码路径返回值 [英] Not All Code Path Return A Value

查看:100
本文介绍了并非所有代码路径返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我写了这样的代码来检查访问级别。

但我得到的错误是并非所有代码路径返回值

请建议相同的原因和解决方案



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;
}


这篇关于并非所有代码路径返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆