2016-04-14 9 views
-6
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; 

Hata, kullanıcı hesapları olarak ulaşılamaz kod görüntülüyor burada değişken kullanıcı 6 kullanıcı hesaplarını detaylandırma hesapları ve LoggedInUsername için sınıf var olanOturum açma ekranı için bir if deyimi içinde ulaşılamaz kod algılandı mı?

  //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."); 
      } 
     } 
    } 
} 

}

+3

Bu bir yöntem mi? Önceki satırda 'dönmek 'diyorsun. – Lee

+2

Kodun tam ortasına bu gibi bir metin koymak yardım istemek için iyi bir fikir değildir. Bunun bir tek yöntem olduğunu ve Lee'nin doğru olduğuna eminim. –

cevap

0
Return 

fonksiyonunun yerine getirilmesini durdurulur ve verilen değeri döndürülür anlamına gelir işe yaramadı eğer yürütmek, hangi dönüşten sonra bir hata atmak. Bir yöntemi çalıştırdığınız için, return'a ihtiyacınız yoktur.

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** 
1

Burada denemek catch gerekmez iadenin başarılı bir şekilde yürütüldüğü durumlarda yürütme devam etmeyeceğinden. Sadece dönüş

İlgili konular