Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c语言中使用switch语句时遇到问题#_C#_Asp.net Identity - Fatal编程技术网

C# 在c语言中使用switch语句时遇到问题#

C# 在c语言中使用switch语句时遇到问题#,c#,asp.net-identity,C#,Asp.net Identity,您好,我需要c#中switch语句的帮助。我将根据用户角色显示一个元素。 我得到了userRole,因为我在另一个页面(角色)中使用了代码 位我无法将角色传递到switch语句 protected void Page_Load() { string userRole = string.Empty; try { // Get current logged in usename strin

您好,我需要c#中switch语句的帮助。我将根据用户角色显示一个元素。 我得到了userRole,因为我在另一个页面(角色)中使用了代码 位我无法将角色传递到switch语句

    protected void Page_Load()
    {
        string userRole = string.Empty;
        try
        {
            // Get current logged in usename
            string username = User.Identity.Name;
            if (string.IsNullOrEmpty(username))
            {
                userRole = "isanonymus";
            }
            else
            {
                Compras entity = new Compras();
                AspNetUser user = entity.AspNetUsers.Where(u => u.UserName.Equals(username)).FirstOrDefault();
                // get role of current logged in user
                userRole = user.AspNetRoles.First().Name.ToLower();
            }
        }
        catch
        {
            userRole = string.Empty;
        }
        if (!IsPostBack)
        {
            ToWhom(userRole);
        }

    }
    private void ToWhom(string userRole)
    {
        switch (userRole)
        {
            case "employee":
                return EmployeeView.Visible = true;
            case "supplier":
                return SupplierView.Visible = true;
            default:
                return GenericView.Visible = true;
        }
    }
这是我得到的错误:
错误CS0127由于“Manage.ToWhom(string)”返回void,因此返回关键字后面不能跟有对象表达式

您已使用void关键字将函数设置为不返回值,因此编译器会给出错误,因为您确实返回了值


您可以将函数从void更改为适当的类型并返回值,也可以通过引用传递参数并将其设置为switch语句中的值。

当前您正在尝试返回一个值,该值将是bool。您需要使用下面的方法中断switch case语句。
    protected void Page_Load()
    {
        string userRole = string.Empty;
        try
        {
            // Get current logged in usename
            string username = User.Identity.Name;
            if (string.IsNullOrEmpty(username))
            {
                userRole = "isanonymus";
            }
            else
            {
                Compras entity = new Compras();
                AspNetUser user = entity.AspNetUsers.Where(u => u.UserName.Equals(username)).FirstOrDefault();
                // get role of current logged in user
                userRole = user.AspNetRoles.First().Name.ToLower();
            }
        }
        catch
        {
            userRole = string.Empty;
        }
        if (!IsPostBack)
        {
            ToWhom(userRole);
        }

    }
    private void ToWhom(string userRole)
    {
        switch (userRole)
        {
            case "employee":
                return EmployeeView.Visible = true;
            case "supplier":
                return SupplierView.Visible = true;
            default:
                return GenericView.Visible = true;
        }
    }
你需要改变这个

private void ToWhom(string userRole)
{
    switch (userRole)
    {
        case "employee":
            return EmployeeView.Visible = true;
        case "supplier":
            return SupplierView.Visible = true;
        default:
            return GenericView.Visible = true;
    }
}
对这个

private void ToWhom(string userRole)
{
    switch (userRole)
    {
        case "employee":
            EmployeeView.Visible = true;
            break;
        case "supplier":
           SupplierView.Visible = true;
            break;
        default:
            GenericView.Visible = true;
            break;
    }
}

将您的功能更改为:

 private void ToWhom(string userRole)
    {
        switch (userRole)
        {
            case "employee":
                 EmployeeView.Visible = true;
                 break;
            case "supplier":
                SupplierView.Visible = true;
                break;
            default:
                GenericView.Visible = true;
                break;
        }
}

代码中有两处更改。首先,返回类型不会为void,因为您将bool类型返回给return。其次,你需要为每一个案例设置中断

private bool ToWhom(string userRole)
{
    switch (userRole)
    {
        case "employee":
            return EmployeeView.Visible = true;
            break;
        case "supplier":
            return SupplierView.Visible = true;
            break;
        default:
            return GenericView.Visible = true;
            break;
    }
}

如果希望函数为
void
,只需删除
return
关键字即可

使用break not return。您的默认大小写无效。@StillLearnin谢谢,我将断点复制到了错误的位置。我很确定您不能从void方法返回。@UnicornoMarley我从来没有说过您可以。你应该读我写的东西。