简体   繁体   English

向字符串数组添加字符串

[英]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
    }
}

But it gives an error that it is outside the boundary of an array. 但是它给出了一个错误,它超出了数组的边界。

What could be the most convenient way to perform this action? 什么是最方便的方法来执行此操作?

Don't use arrays if you need variable sized storage. 如果需要可变大小的存储,请不要使用数组。

Use List<string> instead - it allows you to Add items. 改为使用List<string> - 它允许您Add项目。


In your case, your choice of two arrays is questionable, as each user has a corresponding password - always. 在您的情况下,您选择的两个阵列是有问题的,因为每个用户都有相应的密码 - 总是如此。 This suggests that you should have a custom class to hold a user/password pair. 这表明您应该有一个自定义类来保存用户/密码对。

With such a class (say User ), you would hold a List<User> and simplify your code. 使用这样的类(比如User ),您可以保存List<User>并简化代码。

Try using a List<> . 尝试使用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);
    }
}

Here's a refactored version: 这是一个重构版本:

Users are contained in a user class. 用户包含在用户类中。

They are IEquatable<> which compares their username/passwords (you might want to consider looking into a Guid to keep them unique). 它们是IEquatable<> ,用于比较用户名/密码(您可能需要考虑查看Guid以保持其唯一性)。

Easily add/remove users. 轻松添加/删除用户。

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));
    }
}

Okay I think you might be thinking about this the wrong way. 好吧,我认为你可能会以错误的方式思考这个问题。 The way in which you're using arrays is screaming out for an object. 你使用数组的方式是尖叫出一个对象。

I would made User an object like so 我会让User成为一个像这样的对象

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

I would then maintain a list of Users instead. 然后我会维护一个用户列表。 That way you won't need to maintain array indexes and you can easily add new users into the list. 这样您就不需要维护数组索引,并且可以轻松地将新用户添加到列表中。

Why don't you use and List and apply a DTO instead of multiples string[] ? 为什么不使用和List并应用DTO而不是倍数string[]

Try something like this: 尝试这样的事情:

1) Create a DTO for your Users: 1)为您的用户创建DTO:

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

2) Use and List<DTO> 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 });
    }
}

Try use List<string> class instead of string[] 尝试使用List<string>类而不是string[]

and add items to array using object.Add() method 并使用object.Add()方法将项添加到数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM