2013-03-05 174 views
3
class Myclass 
{ 
    static string[] user_Name = { "admin", "user1", "user2" }; 
    static string[] user_Password ={ "admin", "123", "789" }; 

    public static void Check_Method(string u_name, string u_password) 
    { 

     for (int i = 0; i < user_Name.Length; i++) 
     { 
      if (u_name == user_Name[i] && u_password == user_Password[i]) 
      { 
       MessageBox.Show("login successful"); 
       break; 
      } 
      else 
      { 
       if (i == (user_Name.Length - 1)) 
        MessageBox.Show("Badshow"); 
      } 
     } 
    } 
    public static void add_user(string name, string password) 
    { 
     i=user_Name.Length; 
     user_Name[i]=name; 
     user_Password[i]=password; 
     //here i want to add another user but im unable to find the way 
    } 
} 

但它給出了一個錯誤,它在數組的邊界之外。將字符串添加到字符串數組

什麼可能是最方便的方式來執行此操作?

+0

數組超出界限錯誤的原因是您正在使用用戶名的長度作爲您的user_Name數組的索引。這並不是真正的問題,但我認爲你應該知道。 – Odrade 2013-03-05 20:11:23

回答

7

如果您需要可變大小的存儲,請不要使用數組。

改爲使用List<string> - 它允許您將Add項目。


在你的情況,你的兩個陣列的選擇是有問題的,因爲每個用戶都有相應的密碼 - 永遠。這表明您應該有一個自定義類來保存用戶/密碼對。

有了這樣的班級(比如User),您可以持有List<User>並簡化您的代碼。

+2

不;使用'List ' – SLaks 2013-03-05 19:51:59

+0

@SLaks - 好點。 – Oded 2013-03-05 19:53:52

+0

thanx ..我會記住thix – 2013-03-05 19:57:59

2

嘗試使用List<>

class Myclass 
{ 
    static List<string> user_Name = new List<string>{ "admin", "user1", "user2" }; 
    static List<string> user_Password = new List<string>{ "admin", "123", "789" }; 

    public static void Check_Method(string u_name, string u_password) 
    { 

     for (int i = 0; i < user_Name.Length; i++) 
     { 
      if (u_name == user_Name[i] && u_password == user_Password[i]) 
      { 
       MessageBox.Show("login successful"); 
       break; 
      } 
      else 
      { 
       if (i == (user_Name.Length - 1)) 
        MessageBox.Show("Badshow"); 
      } 
     } 
    } 
    public static void add_user(string name, string password) 
    { 
     user_Name.Add(name); 
     user_Password.Add(password); 
    } 
} 

這裏有一個重構的版本:

用戶都包含在用戶類。

他們IEquatable<>與之相比他們的用戶名/密碼(你可能要考慮尋找到一個Guid,讓他們唯一的)。

輕鬆添加/刪除用戶。

public class User : IEquatable<User> 
{ 
    public User(string name, string password) 
    { 
     Name = name; 
     Password = password; 
    } 

    public string Name { get; set; } 
    public string Password { get; set; } 

    public bool Equals(User other) 
    { 
     if (other == null) return false; 

     return other.Name == Name && other.Password == Password; 
    } 
} 

public class AuthenticationManager 
{ 
    private List<User> LoggedInUsers = new List<User> 
    { new User("Admin", "admin"), new User ("user1", "123"), new User ("user2", "789") }; 

    public bool Authenticate(string userName, string password) 
    { 
     var user = new User(userName, password); 

     //if the user is in the list it will return false otherwise true. 
     return !LoggedInUsers.Any(u => user.Equals(user)); 
    } 

    public void Login(string name, string password) 
    { 
     LoggedInUsers.Add(new User(name, password)); 
    } 

    public void Logout(string name, string password) 
    { 
     LoggedInUsers.Remove(new User(name, password)); 
    } 
} 
+0

這會很快失去控制。我總是建議新手儘快掌握面向對象的概念。 – 2013-03-05 19:58:02

+0

o偉大的兄弟... thix ix也是一個更好的方法 – 2013-03-05 20:00:47

+0

@RummyKhan檢查我的編輯 – Romoku 2013-03-05 20:07:56

0

好吧我想你可能會想這是錯誤的方式。你使用數組的方式是尖叫出一個對象。

我會做出用戶喜歡這樣

public class User 
{ 
    public string UserName { get; set;} 
    public string Password { get; set;} 
} 

然後,我會保持用戶的列表,而不是一個對象。這樣您就不需要維護數組索引,並且可以輕鬆地將新用戶添加到列表中。

0

爲什麼不使用和List並應用DTO而不是倍數string[]

嘗試是這樣的:

1)創建一個DTO爲您的用戶:

public class UserDTO 
{ 
    public string UserName { get; set; } 
    public string Password { get; set; }  
} 

2)使用和List<DTO>

class Myclass 
{ 
    static List<UserDTO> users = new List<UserDTO>() 
    { 
     new UserDTO() { UserName= "admin", Password = "admin" } , 
     new UserDTO() { UserName= "user1", Password = "123" } , 
     new UserDTO() { UserName= "user2", Password = "789" } , 
    } 

    public static void Check_Method(string u_name, string u_password) 
    { 
     if (users.Exists(x => x.UserName == u_name && x.Password == u_password) 
     { 
       MessageBox.Show("login successful"); 
     } 
     else 
     { 
      MessageBox.Show("Badshow"); 
     } 
    } 
    public static void add_user(string name, string password) 
    { 
     users.Add(new UserDTO() { UserName= name, Password = password }); 
    } 
} 
+0

偉大的,和thanx爲我展示的方式 – 2013-03-05 19:59:11

0

嘗試使用List<string>類,而不是string[]

並使用將項添加到數組方法

+0

好吧,我會嘗試一下thanx以獲得您的意見 – 2013-03-05 20:01:56