0

I'm sure this is really simple to do but i'm struggling.

private void button1_Click(object sender, EventArgs e)
    {
        SaveBtn();


        void SaveBtn()
        {

            string savetext = textBox1.Text;
            string savetext2 = textBox2.Text;


            File.AppendAllText(@"C:\Riot Games\AccountSwitcher.txt", savetext + Environment.NewLine + savetext2 + Environment.NewLine + Environment.NewLine);
            MessageBox.Show("Your ID: " + savetext + " and you PWD: " + savetext2 + " has been saved.");


        }

    }

As you can see i have 2 textbox and when i'm clicking the button "save" both input are saved into a file.txt. This code works like a charm but i'd rather save these 2 inputs into an array so i could use them individually.

Thanks your help, i'm pretty noob as you can see so please keep it simple :D <3

5
  • 1
    An array of what? char? strings? what would be the separator of the array? You gotta be more specific and demonstrate at least one try when making questions Commented Aug 16, 2017 at 21:17
  • You can already use them individually..? Commented Aug 16, 2017 at 21:28
  • string[] saved = new string[] { textBox1.Text, textBox2.Text }; Commented Aug 16, 2017 at 21:31
  • Yes thank you guys ! I had this idea in mind but i didn't know how to write it ! It was so simple. How can i mark this question as "answered"? Commented Aug 16, 2017 at 21:34
  • Welcome to Stack Overflow! You can accept one of the answers by clicking the checkmark next to it. (You can change it later if a better answer comes along.) See stackoverflow.com/help/someone-answers for more. Also, feel free to edit the question if you can make it more relevant to others. Thanks! Commented Aug 17, 2017 at 1:15

3 Answers 3

1

Use:

string[] savetexts = new string[]{ savetext , savetext2 };
Sign up to request clarification or add additional context in comments.

1 Comment

I was so close ! Thank you !!
0

Alternatively, you could convert the entire string and save it in a char array.

char[] savetext = savetext.ToCharArray();
char[] savetext2 = savetext2.ToCharArray();

Hope this helps.!

Comments

0

P.S It is much easier to use a List instead of hard-coded arrays like above.

List<String> myStrings = new List<String>();
myStrings.add(savetext);
myStrings.add(saveText2);

.....etc

then to get them back you iterate over myStrings

foreach(String s in myStrings){
  Console.writeline(s);
}

Or you can access them directly

String text1 = myStrings[0];
String text2 = myString[1];

This is a bit more than what you are asking, but using List becomes much easier in the long run. Best of luck.

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.