简体   繁体   中英

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.


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.

Try using a 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).

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

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[] ?

Try something like this:

1) Create a DTO for your Users:

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

2) Use and 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[]

and add items to array using object.Add() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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