1

I have this part of code that takes a file and puts it in an ArrayList. The file that will be entered will be a CSV (the current CSV that I use has headers at the first line, so I don't need that line) and the second line has to be put in an ArrayList.

I use ArrayList because the file can be dynamic, so I am not sure what will be the length of the second line. I tested (with a file that has 7 comma-separated values on the second line) this code and it prints that the ArrayList has a length (fileList.Count) = 1.

What is wrong ?

ArrayList fileList2 = new ArrayList();
private void button3_Click(object sender, EventArgs e)
{
    string filename = "";
    DialogResult result = openFileDialog2.ShowDialog();
    if (result == DialogResult.OK)
    {
        filename = openFileDialog2.FileName;
        textBox3.Text = filename;
        string line2;
        System.IO.StreamReader file2 = new System.IO.StreamReader(textBox3.Text);  //reads file from textbox 
        stringforData = file2.ReadLine();      // this reads the first line that I dont need 
        while ((line2 = file2.ReadLine()) != null)     //read the lines 
        {
            // puts elements into array
            fileList2.Add(line2.Split(';'));//split the line and put it in the arraylist
        }
        file2.Close();
        if (true)    // this is for testind what is happening 
        {
            this.textBox2.Clear();
            textBox3.Text = Convert.ToString(fileList2.Count);
        }
    }
}
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
george mano
  • 5,948
  • 6
  • 33
  • 43
  • 4
    Why on earth are you using arraylist in 2011? It's been deprecated for years now, and if Baszz is correct your example shows exactly why – Dyppl Oct 07 '11 at 14:02
  • To add to what Dyppl said, see [this question](http://stackoverflow.com/questions/5063156/why-isnt-arraylist-marked-obsolete). – Roman Oct 07 '11 at 14:05
  • @Dyppl What should I use and how ? – george mano Oct 07 '11 at 14:05
  • @georgemano: you should use generis `List`: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx, I suppose in your case it will be `List`. `ArrayList` was there before generics, now there is no point to use it in new code. – Dyppl Oct 07 '11 at 14:06
  • You would be better using a List and splitting after each comma, not semi-colon – Bali C Oct 07 '11 at 14:07
  • 1
    Also keep in mind that getting data out of CSV files is a [solved](http://www.codeproject.com/KB/database/CsvReader.aspx) [problem](http://commonlibrarynet.codeplex.com/SourceControl/changeset/view/67698#480402). Please don't re-invent the wheel, just use an existing library that will do it for you. – Roman Oct 07 '11 at 14:11

2 Answers2

5

Don't you want to use fileList2.AddRange() instead of fileList2.Add() ? It seems to me that you are adding one item to the fileList now. That item is an array that contains all items you actually wanted to add to the list. If you get that array first and than use the addRange method, It should be fine.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
  • +1 looks like the error to me too.. anyway, I think that the question is not correctly formulates, since filelist2 is never define inside this, and the OP is telling us that filelist count is 1... – gbianchi Oct 07 '11 at 14:03
  • 1
    also: he talks about a comma-separated line whereas he is splitting with ; – mtijn Oct 07 '11 at 14:03
  • @mtijn: it's most likely okay, CSV files often come with semicolon separator in some locales (like russian) but they are still called CSV – Dyppl Oct 07 '11 at 14:05
0

First off, you should probably be using AddRange(), not Add(). Second, if this is a CSV file, then why are you passing a semi-colon to the split() method?

MGZero
  • 5,812
  • 5
  • 29
  • 46