函数并非所有代码路径都返回值

时间:2017-05-30 11:09:17

标签: c#

我有以下功能:

OnCheckedChange

这里我收到一条错误消息,指出并非所有代码路径都返回一个值。我错过了什么和哪里?

6 个答案:

答案 0 :(得分:4)

抛出错误是因为在您没有输入foreach语句的情况下,您的函数没有返回任何内容。当您的函数返回long值时,您需要在函数末尾返回任何值。我认为问题在于你错放了第二次返回,这应该超出foreach循环。

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId;

    var users = _userManager.Users;
    foreach (var user in users)
    {
        if (user.Email == currentUserId)
        {
            var idForStakeholder = user.Id;

            var stakeholders = _context.Stakeholders;
            foreach (var stakeholder in stakeholders)
            {
                if (stakeholder.IdentityId == idForStakeholder)
                {
                    stakeholderId=stakeholder.StakeholderId;
                    return stakeholderId;

                }
                else
                {
                    return 0;
                }

            }

        }
    }
    return 0; // 0 will be returned in any case if you reach the end of the function
}

您可能也必须小心当前的逻辑。如果您的用户的第一个利益相关者不是好用户,则直接返回0,而不是检查其他用户。如果没有任何利益相关方ID与您要查找的利益相关方ID相对应,您可能只想放置return 0。把它放在那里很好,因为你不再需要检查其他用户,并且可以直接退出该功能。

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId;

    var users = _userManager.Users;
    foreach (var user in users)
    {
        if (user.Email == currentUserId)
        {
            var idForStakeholder = user.Id;

            var stakeholders = _context.Stakeholders;
            foreach (var stakeholder in stakeholders)
            {
                if (stakeholder.IdentityId == idForStakeholder)
                {
                    stakeholderId=stakeholder.StakeholderId;
                    return stakeholderId;

                }
            }

            return 0; // If none of the stakeholder ID correspond to the one you are looking for, it means it doesn't exist. You can return 0 directly there.
        }
    }
    return 0; // This part of code will be reached if the user is not found.
}

答案 1 :(得分:2)

将最外面的返回移出循环。如果用户列表为空,则永远不会输入循环。然后没有回报被击中。

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId;

    var users = _userManager.Users;
    foreach (var user in users)
    {
        if (user.Email == currentUserId)
        {
            var idForStakeholder = user.Id;

            var stakeholders = _context.Stakeholders;
            foreach (var stakeholder in stakeholders)
            {
                if (stakeholder.IdentityId == idForStakeholder)
                {
                    stakeholderId=stakeholder.StakeholderId;
                    return stakeholderId;

                }
                else
                {
                    return 0;
                }

            }

        }

    }

  return 0;
}

答案 2 :(得分:2)

它可能无法进入for循环,因此请将其更改为:

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId;

    var users = _userManager.Users;
    foreach (var user in users)
    {
        if (user.Email == currentUserId)
        {
            var idForStakeholder = user.Id;

            var stakeholders = _context.Stakeholders;
            foreach (var stakeholder in stakeholders)
            {
                if (stakeholder.IdentityId == idForStakeholder)
                {
                    stakeholderId=stakeholder.StakeholderId;
                    return stakeholderId;

                }
                else
                {
                    return 0;
                }

            }

        }
        return 0;
    } 
    //Missing RETURN
    return 0;
}

答案 3 :(得分:2)

正如其他人所提到的,错误是因为在没有用户的情况下你错过了一个返回(所以它没有进入foreach循环)。但是,您的函数还有其他错误:

  • if (user.Email == currentUserId) { } return 0;
    • 在此强制要求循环中的第一个用户拥有您要查找的电子邮件,否则它将返回0
  • if (stakeholder.IdentityId == idForStakeholder) { } else return 0;
    • 您强制要求第一个利益相关方拥有您正在寻找的ID,否则它将返回0

使用LinQ,您可以将整个方法简化为:

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId;

    var user = _userManager.Users
        .Where(u => u.Email == currentUserId)
        .FirstOrDefault(); // get only the wanted user 
    if (user == null)
    {
        return 0;
    }

    var stakeholder = _context.Stakeholders
        .Where(s => s.StakeholderId == user.IdentityId)
        .FirstOrDefault(); // get only the wanted stakeholder from the database

    return stakeholder == null ? 0 : stakeholder.StakeholderId;
}

您还应该注意,在foreach属性上使用DbSet意味着您正在对数据库执行select * from table。您应该使用LinQ过滤数据库中的集合,而不是在in-memoy中进行过滤。

答案 4 :(得分:2)

使用相同修复程序的许多快速答案,但我注意到在给定代码中存在逻辑问题(不确定您的方案是否存在问题)。变量stakeholders中的值只是循环执行一次。这意味着如果条件stakeholder.IdentityId == idForStakeholder满足该方法将返回相应的id,否则它将返回0,因此它跳过执行stakeholders中的其余值(如果你是好的,跳过这些值)。如果您希望继续检查集合中的所有项目,则可以使用以下代码:

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId = 0;

    var users = _userManager.Users;
    foreach (var user in users)
    {
        if (user.Email == currentUserId)
        {
            var idForStakeholder = user.Id;    
            var stakeholders = _context.Stakeholders;
            foreach (var stakeholder in stakeholders)
            {
                if (stakeholder.IdentityId == idForStakeholder)
                {
                    stakeholderId=stakeholder.StakeholderId;
                    return stakeholderId;    
                }
            }

        }
    }    
  return stakeholderId;
}

现在,此方法将检查所有用户的所有利益相关者值,并返回满足条件(stakeholderId)的stakeholder.IdentityId == idForStakeholder(如果有),否则它将返回0之后所有循环。现在你也摆脱了这个错误。在开始循环之前,请不要忘记使用stakeholderId初始化0;

答案 5 :(得分:1)

你应该返回一个值,如错误所示,返回循环外的值,

private long GetStakeholderId()
{
    string currentUserId = _userManager.GetUserId(User);
    long stakeholderId;

    var users = _userManager.Users;
    foreach (var user in users)
    {
        if (user.Email == currentUserId)
        {
            var idForStakeholder = user.Id;

            var stakeholders = _context.Stakeholders;
            foreach (var stakeholder in stakeholders)
            {
                if (stakeholder.IdentityId == idForStakeholder)
                {
                    stakeholderId=stakeholder.StakeholderId;
                    return stakeholderId;

                }
                else
                {
                    return 0;
                }

            }

        }

    }

  return 0;
}