调试登录代码

本文关键字:代码 登录 调试 | 更新日期: 2023-09-27 18:04:05

我有一些登录功能的代码。我无法在try语句中识别return语句,try语句似乎是放置return语句的唯一逻辑位置。return语句所在的位置允许用户登录,即使密码不正确。如果我把return语句放在其他任何地方,我收到错误"不是所有的代码路径返回一个值"。

    public static User selectUser(string userName, string password)
    {
       // code to validate user
        try
        {
                if (isUserValid)
                {
                    MessageBox.Show("Login successful!");
                }
                else
                {
                    MessageBox.Show("Login failed");
                }
            }
        }
        catch (Exception e)
        {
             MessageBox.Show(e.ToString());
        }
        return aUser;
    }

调试登录代码

您应该只在成功的场景中返回user对象,否则返回null。为了确定用户是否已经过身份验证,在使用selectUser方法时检查用户对象是否为null

User aUser = null;
try {
            while (dbReader.Read())
            {
                if (dbReader.HasRows)
                {
                    MessageBox.Show("Login successful!");
                    aUser = new User();
                }
                else
                {
                    MessageBox.Show("Login failed");
                }
            }
        }
catch (Exception e) {
                        MessageBox.Show(e.ToString());
                    }
finally {
          return aUser;
}

MSDN有很好的错误示例来解释你得到的错误代码- CS0161。

您的示例有多个"代码路径"-意味着基于if/try语句中的条件执行的命令序列。

以下是主要的可能路径:

  1. 用户是有效的,没有异常:尝试块,消息框1,功能结束
  2. 用户无效,没有例外:尝试块,消息框2,功能结束
  3. 验证过程中的异常:try block, catch block, end of function

如果您只从某些代码路径返回结果(即问题中的1和2)并从"函数结束"中删除return,则3个代码路径中只有2个将具有返回语句。

在您的情况下的修复可能是始终return null;在功能结束,并跳出return validatedUser;从地方你有有效的用户对象。


    public static User selectUser(string userName, string password)
    {
        User aUser = new User();
        if (sConnection.State == ConnectionState.Closed)
            sConnection.Open();
        OleDbCommand cmd = sConnection.CreateCommand();
        OleDbDataReader dbReader = null;
        string sql = "SELECT * FROM [User] WHERE ([userName]='" + userName + "' AND [Password]='" + password + "')";
                        cmd.CommandText = sql;
            dbReader = cmd.ExecuteReader();
        try
        {
            while (dbReader.Read())
            {
                if (dbReader.HasRows)
                {
                    MessageBox.Show("Login successful!");
                    aUser.UserName = username;
                    return aUser;
                }
                else
                {
                    MessageBox.Show("Login failed");
                    aUser.UserName =string.empty;
                }
            }
        }
        catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                aUser.UserName = string.empty;
            }
        return aUser;
    }

After you invoke this function, check the return IsNULLofEmpty(aUser.Username)

Yes C# will not allow you if there exist at least one flow that does not return a variable of the type User. Your code should work fine, as it does return a variable of the type User. What you should do is, dont display the message inside ur function implement it at the place from where u r calling it, just put a try catch block around the calling statement so that you know what is happening when the user does not exist (the object returned by the function must have some properties you can check from)

In that case u probably can check the function returned value in if else condition to display the message accordingly

Handle the exception in your view logic instead of in your business logic.

Something like this:

class View
{
    public void Login(string username, string password)
    {
        try
        {
            var user = _users.SelectUser(username, password);
            MessageBox.Show(
                string.Format("valid user: {0}", user.UserName));
        }
        catch (InvalidUserNameOrPasswordException)
        {
            MessageBox.Show("Invalid username or password");
        }
    }
}
public class Users
{
    public User SelectUser(string userName, string password)
    {
        if (!ValidUser)
        {
            throw InvalidUserNameOrPasswordException();
        }
        return new User();
    }
}

注意,这个解决方案不要求调用代码if-else方法的结果。

——update——

现在只捕获InvalidUserNameOrPasswordException

——update2——

我想说的是,基于异常的编程是建议和接受的解决方案。为了进一步的参考,请阅读这篇(讽刺的)文章,其中基于异常的编程以一种有趣而又有点讽刺的方式进行了解释。