[英]Add strings to string array
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
}
}
但是它給出了一個錯誤,它超出了數組的邊界。
什么是最方便的方法來執行此操作?
如果需要可變大小的存儲,請不要使用數組。
改為使用List<string>
- 它允許您Add
項目。
在您的情況下,您選擇的兩個陣列是有問題的,因為每個用戶都有相應的密碼 - 總是如此。 這表明您應該有一個自定義類來保存用戶/密碼對。
使用這樣的類(比如User
),您可以保存List<User>
並簡化代碼。
嘗試使用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));
}
}
好吧,我認為你可能會以錯誤的方式思考這個問題。 你使用數組的方式是尖叫出一個對象。
我會讓User成為一個像這樣的對象
public class User
{
public string UserName { get; set;}
public string Password { get; set;}
}
然后我會維護一個用戶列表。 這樣您就不需要維護數組索引,並且可以輕松地將新用戶添加到列表中。
為什么不使用和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 });
}
}
嘗試使用List<string>
類而不是string[]
並使用object.Add()
方法將項添加到數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.