0

I want to add a string from a textbox to an array, but when I tried to add it to the array it gives me an error (Cannot convert char[] to string[]) what is it that I'm doing wrong, and is it maybe a better way to do it?

    public string[] users = { "username" };
    public string[] passwords = { "password" };

    string[] users = textBox1.Text.ToArray();
    string[] passwords= textBox2.Text.ToArray();
5
  • 1
    Looks like you're trying to do string[] user = { textBox1.Text }; but why? Commented Aug 9, 2019 at 23:10
  • Why are you declaring string arrays for something that is a single string? You should probably make your user and password variables just strings as it doesn't seem like you are actually using an array, just single values. Then you can just assign directly from the Text property. Commented Aug 9, 2019 at 23:11
  • 1
    It really would help if you explained what you will be doing with these string arrays. Commented Aug 9, 2019 at 23:17
  • I want to add both a username and password to, later on, and be able to log in on a simple login screen Commented Aug 9, 2019 at 23:19
  • If by “a username and password” you mean one username and one password, you should be using strings, not string arrays. And if the password is for the username you should create a class that contains both. Commented Aug 9, 2019 at 23:28

3 Answers 3

1

You want to use char array, not string array. This is because textBox1.Text returns a string type, not a string array type. Calling ToArray() on a string results in a char array type.

    char[] user = textBox1.Text.ToArray();
    char[] password= textBox2.Text.ToArray();

You should also consider changing string[] user and string[] password to string type instead, since I'm assuming you are only storing one username in user. If you are trying to store a collection of users, then you should name your variable correctly, such as string[] users, to not cause confusion.

Sign up to request clarification or add additional context in comments.

Comments

0

user and password have been allocated as single element arrays. Arrays are immutable and cannot be appended to, so you cannot do this:

public string[] user = { "username" };
public string[] password = { "password" };
//...
user.Append(textBox1.Text);
password.Append(textBox2.Text);

But you could reallocate the user and password arrays with something like this:

public string[] user = { "username" };
public string[] password = { "password" };
//...
user = new[] { user[0], textBox1.Text };
password = new[] { password[0], textBox2.Text };

This is rather clumsy, though. You would probably be better off defining user and password as List<String>, i.e.:

public List<string> user = new List<string>() { "username" };
public List<string> password = new List<string>() { "password" };
//...
user.Add(textBox1.Text);
password.Add(textBox2.Text);

Comments

0

You should use char arrays instead of string array :

public string[] users = { "username" };
public string[] passwords = { "password" };

-

char[] users = textBox1.Text.ToArray();
char[] passwords= textBox2.Text.ToArray();

And instead of string arrays you can use List<string> for users and passwords.

Tip : It's better to rename your textBoxes something like txtUserName for clearify and better code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.