C# 在登录屏幕的if语句中检测到无法访问的代码?

C# 在登录屏幕的if语句中检测到无法访问的代码?,c#,C#,错误是用户帐户显示为无法访问的代码,请为用户帐户创建一个类,详细说明6个用户帐户和LoggedInUsername变量 public void Login() { //Admin login temp if (txtusername.Text == "admin" && txtpassword.Text == "admin123") { Form mainmenu = new frmmainmenu

错误是用户帐户显示为无法访问的代码,请为用户帐户创建一个类,详细说明6个用户帐户和LoggedInUsername变量

public void Login()
    {

        //Admin login temp
        if (txtusername.Text == "admin" && txtpassword.Text == "admin123")
        {
            Form mainmenu = new frmmainmenu();
            Useraccounts.LoggedInUsername = txtusername.Text;
            this.Hide();
            mainmenu.Show();
            return;
//创建useraccounts类的新对象以进行登录
**Useraccounts**LoginAttent=new Useraccounts();
//检索要运行循环适当次数的帐户数
int NoOfAccounts=loginattent.Username.Length;

对于(int i=0;i这里不需要try-catch,因为如果返回成功执行,执行将不会继续。只需在返回后抛出一个错误,如果返回不起作用,将执行该错误

            //Make new object of useraccounts class for logging in
            **Useraccounts** Loginattempt = new Useraccounts();


            //Retrieve the number of accounts there are to run the loop the appropriate number of times
            int NoOfAccounts = Loginattempt.Username.Length;

            for (int i = 0; i <= NoOfAccounts; i++)
            {
                try
                {
                    //Checking the username against the class & check if password matches
                    if (txtusername.Text == Loginattempt.Username[i])
                    {
                        if (txtpassword.Text == Loginattempt.Password[i])
                        {
                            Form Mainmenu = new frmmainmenu();
                            Useraccounts.LoggedInUsername = txtusername.Text;
                            this.Hide();
                            Mainmenu.Show();
                            return;
                        }
                    }
                }
                catch
                {
                    //If the username cannot be found do nothing.
                }
            }

            {
                MessageBox.Show("Incorrect username or password.");
            }
        }
    }
}
表示停止执行函数并返回给定值。 因为您正在运行一个方法,所以不需要返回

Return 
public void Login()
{
//管理员登录临时
if(txtexername.Text==“admin”&&txtpassword.Text==“admin123”)
{
Form Main Menu=新建frmmainmenu();
Useraccounts.LoggedInUsername=txtusername.Text;
this.Hide();
main menu.Show();

这是所有的方法吗?你在前一行中调用<代码>返回;< /代码>。当你请求帮助时,把这样的文本放在你的代码中间不是一个好主意。我几乎可以肯定这是一种方法,而李是正确的。
public void Login()
    {
    //Admin login temp
    if (txtusername.Text == "admin" && txtpassword.Text == "admin123")
    {
        Form mainmenu = new frmmainmenu();
        Useraccounts.LoggedInUsername = txtusername.Text;
        this.Hide();
        mainmenu.Show();
        return; <----**delete this return**