I can not throw in the string array data from the file.

public partial class Settings : Form { public static string Server { get; set; } public static string DataBase { get; set; } public static string Login { get; set; } public static string Password { get; set; } public static MySqlSslMode SslMode { get; set; } public static int Port { get; set; } string[] str=new string[6]; public Settings() { InitializeComponent(); } private void Settings_Load(object sender, EventArgs e) { comboBox1.DataSource = Enum.GetValues(typeof(MySqlSslMode)); string[] str = new string[6]; FillArrStringAsync(); label7.Text = str[0]; label8.Text = str[1]; label12.Text = str[2]; label11.Text = str[3]; label10.Text = str[4]; label9.Text = str[5]; } public string FillArrString() { string[] str = new string[6]; using (StreamReader reader = new StreamReader(@"SettingsProgram.txt", Encoding.UTF8)) { int tempVar = 0; while (!reader.EndOfStream) { str[tempVar] = reader.ReadLine(); tempVar++; } } return String.Concat(str); } public async void FillArrStringAsync() { await Task.Factory.StartNew(FillArrString); } private void button2_Click(object sender, EventArgs e) { Close(); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "" && textBox2.Text != "" && textBox5.Text != "" && maskedTextBox1.Text != "" && comboBox1.Text != "") { Server = textBox1.Text; DataBase = textBox5.Text; Login = textBox2.Text; Password = textBox3.Text; SslMode = (MySqlSslMode)Enum.Parse(typeof(MySqlSslMode), comboBox1.Text); Port = Convert.ToInt32(maskedTextBox1.Text); using (StreamWriter writer = new StreamWriter(@"SettingsProgram.txt", false, Encoding.UTF8)) { writer.Write(Server + "\r\n"); writer.Write(DataBase + "\r\n"); writer.Write(Login + "\r\n"); writer.Write(Password + "\r\n"); writer.Write(SslMode.ToString() + "\r\n"); writer.Write(Port.ToString() + "\r\n"); writer.Close(); } } else { MessageBox.Show("Заполните все поля!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } 

When debugging, we get data from the file through FillArrStringAsync, everything works. But when I try to fill in labels with these values ​​when the form is loaded, it is not filled in, just an empty string. Who knows why this could be? I also noticed that when I use the FillArrString function through debug, all the values ​​skip over me, but the output I get is still an empty array.

  • And what kind of magic do you have with multiple array initialization? - Rootware
  • Obviously, you are trying to write data to labels from a local variable str , which you do not fill. And you fill in a completely different local variable in a different method, but just with the same name - tym32167

1 answer 1

Slightly rewrote your code.

 private async void Settings_Load(object sender, EventArgs e) { comboBox1.DataSource = Enum.GetValues(typeof(MySqlSslMode)); string[] str = await FillArrStringAsync(); label7.Text = str[0]; label8.Text = str[1]; label12.Text = str[2]; label11.Text = str[3]; label10.Text = str[4]; label9.Text = str[5]; } public Task<string[]> FillArrStringAsync() { return Task.Run(()=>File.ReadAllLines(@"SettingsProgram.txt", Encoding.UTF8)); } 

UPD

The problem was that you filled one variable and read from another

 private void Settings_Load(object sender, EventArgs e) { comboBox1.DataSource = Enum.GetValues(typeof(MySqlSslMode)); string[] str = new string[6]; //<<<<< это одна переменная ....... } public string FillArrString() { string[] str = new string[6]; // <<<< а это совсем другая переменная ........ } 

How to fix? The easiest way - you have a function that reads data from a file. Let it returns this data. I.e:

 private async void Settings_Load(object sender, EventArgs e) { comboBox1.DataSource = Enum.GetValues(typeof(MySqlSslMode)); string[] str =await FillArrStringAsync(); label7.Text = str[0]; label8.Text = str[1]; label12.Text = str[2]; label11.Text = str[3]; label10.Text = str[4]; label9.Text = str[5]; } public string[] FillArrString() { string[] str = new string[6]; using (StreamReader reader = new StreamReader(@"SettingsProgram.txt", Encoding.UTF8)) { int tempVar = 0; while (!reader.EndOfStream) { str[tempVar] = reader.ReadLine(); tempVar++; } } return str; // считанное значение возвращаем из функции. // Любой, кто функцию вызовет, получит в ответ считанное значение. } public Task<string[]> FillArrStringAsync() { return Task.Run(()=>FillArrString()); } 
  • It all worked. But tell me what was the actual problem? I had corrected the code, and left only the global str array and worked with it, and then tried to fill it with it. - Gnom Skull
  • @GnomSkull updated the answer - tym32167
  • one
    @GnomSkull global field would not suit you, you start reading in parallel, that is, even if you just made the variable global, it’s not the fact that the file was already read when the labels were filled. That is, you need to 1) Wait until the file is considered 2) Then fill in the labels. In my code, I am waiting using the operator await - tym32167