Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add strings to string array

Tags:

c#

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?

like image 838
Rehan Manzoor Avatar asked Jun 05 '26 11:06

Rehan Manzoor


2 Answers

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.

like image 76
Oded Avatar answered Jun 07 '26 23:06

Oded


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));
    }
}
like image 33
Dustin Kingen Avatar answered Jun 07 '26 22:06

Dustin Kingen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!