0

Basically I have a class and a form. In my class , I wrote the variables per below. These variables are where I saved my data in my form:

Save.cs

namespace CSV
{
  public partial class Save
  {
    public static string people = string.Empty;
    public static List<string> mileage_list = new List<string>();
  }
}

Then in my form, I made an event and called the event through FormClosed. Edited: Each time I click button, textbox1 will clear and I can add in new values. All these values are being saved in the list

Form1.cs

public void Savetocsv()
{
 Type s = typeof(Save);
 FieldInfo[] fields = s.GetFields(BindingFlags.Public | BindingFlags.Static);

 StringBuilder csvdata = new StringBuilder();

 string header = String.Join("|", fields.Select(f => f.Name).ToArray());
 csvdata.AppendLine(header);

 string first = Save.people;
 string newLine = string.Format("{0}", first);
 csvdata.AppendLine(newLine);

 string mileage = String.Join("|", Save.mileage_list.ToArray());
 csvdata.AppendLine(mileage);

 string filepath = @"C:\Vehicles.csv";
 File.WriteAllText(filepath, csvdata.ToString());
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
 Save.people = Combobox1.SelectedItem.ToString();
 Save.mileage_list.Add(Textbox1.Text);
 Savetocsv();
}


private void button1_Click(object sender, EventArgs e)
{
 Save.people = Combobox1.SelectedItem.ToString();
 Save.mileage_list.Add(Textbox1.Text);
 TBmileage.Text = "";
}

When I opened the Vehicles.csv file, the string from Save.people was correct but the string from Save.mileage_list was written as System.Collections.Generic.List1[System.String]. Hence, may I know how do I rewrite the code to export both the string values properly.

Edited Previously, the values are wrong but exported in their respective headers. See Image 1. However, with this code, the values are correct but not exported in their respective headers. Image 2. I know I might need to do a Dictionary or a for loop but I do not know how.

musa
  • 319
  • 1
  • 2
  • 9
  • We appear to be missing the part where you add any data to the `fields` array - what does `s.GetFields()` look like? But basically if you've done something like `mileage_list.ToString()` then that won't work, and will result in the output you're seeing. Short of any direct mapping between the two types (`FieldInfo[]` and `List`, you have to loop through each string item in the mileage_list and add each one individually to your CSV data – ADyson Feb 06 '19 at 15:26
  • Having said that, it's also not clear whether `mileage_list` will ever, in reality, contain more than one value. The only thing we can see is that you add one item to it when the form closes. So unless there are other times when data can be added into it, it will only ever contain one value. In that case, there is no purpose in defining it as a list, it could just be a single string the same as `people`. – ADyson Feb 06 '19 at 15:28
  • @ADyson GetFields returns all public fields of a type. To OP:You need to go through your list using a loop to get each value – Erik T. Feb 06 '19 at 15:38
  • Hi guys. I've edited the code based on the link given. The thing is, there will be only 1 value for `people` but multiple values for `mileage`. Now the values are exported but not in their respective headings. I feel that I need to do a dictionary or a for loop but I do not know how. Hope to get some help. Thanks. – musa Feb 06 '19 at 17:05
  • @musa: You call "AppendLine" twice, once for "people" and once for "mileage", so it's not surprising that it appears on two lines. Also I'm not sure about your design, you can have an arbitrary number of columns for "mileage" which is not standard for a CSV (same for the `|` delimiter) – Meta-Knight Feb 06 '19 at 17:21
  • @Meta-Knight Hai.Without using "AppendLine" twice, I do not know how to export two different types of string together, which is the main purpose of this question. When it comes to the number of columns, what I intend is for the final output to look like this [https://stackoverflow.com/questions/41399428/handling-multiple-values-in-one-cell-to-export-to-csv]. So the value for people would repeat and matched with the different value for mileage. Therefore, only 2 columns but many rows. As for the `|`, I feel it is easier to look at. – musa Feb 06 '19 at 17:40
  • @musa: That was really not clear from the question! Maybe write a new question focusing only on the part of code you have issues with, which details the data as input and the expected file structure as output. Also, you should look at libraries like CsvHelper which can make it easier to output to CSV. – Meta-Knight Feb 06 '19 at 18:43

0 Answers0