Arraylists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santa101
    New Member
    • Jun 2012
    • 1

    Arraylists

    I have created a class and have four properties and I am reading the values from a CSV file and then assign them to the class properties and then trying to add the object to Arraylist.

    But after the loop, the array list has got 5 items, but all items look the same!

    Code:
    // declaring the array list
                ArrayList objClaimData = new ArrayList();
    
                // creating an instance of the class
                clsClaimData datClaims = new clsClaimData();
    
                // declaring the variable for streamreader
                string file_name = "D:\\Bhagavan\\My Documents\\Visual Studio 2010\\Projects\\ClaimsReserving\\ClaimsReserving\\claims.csv";
                string textLine = "";
                string[] colval = null;
    
                // opening the file using the streamreader
                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(file_name);
    
                // looping thru the text file until all lines are read and stored in the arraylist.            
                while ((textLine = objReader.ReadLine()) != null)
                    {
                        colval = textLine.Split(',');
    
                        datClaims.Product = Convert.ToString(colval[0]);
                        datClaims.OrginYear = Convert.ToInt32(colval[1]);
                        datClaims.DevYear = Convert.ToInt32(colval[2]);
                        datClaims.IncrementValue = Convert.ToDouble(colval[3]);
                        
                        objClaimData.Add(datClaims);
                        
                        Console.WriteLine(textLine);
                    }
    What is wrong with the above ? Please help. I am new to C#.
    Last edited by PsychoCoder; Jun 3 '12, 04:14 AM. Reason: Code tags added
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    Hey there, the problem I believe is that you need to create an instance of clsClaimData for each line, this should help:

    Code:
     // declaring the array list
     ArrayList objClaimData = new ArrayList();
     
     // declaring the variable for streamreader
     string file_name = "D:\\Bhagavan\\My Documents\\Visual Studio 2010\\Projects\\ClaimsReserving\\ClaimsReserving\\claims.csv";
     
     foreach(string line in File.ReadAllLines(file_name))
     {
        string colval = line.Split(',');
        
         // creating an instance of the class (per line).
        clsClaimData datClaims = new clsClaimData();
        
        datClaims.Product = colval[0];
        datClaims.OrginYear = int.Parse(colval[1]);
        datClaims.DevYear = int.Parse(colval[2]);
        datClaims.IncrementValue = double.Parse(colval[3]);
        
        objClaimData.Add(datClaims);
        Console.WriteLine(line);
     }
    All the best!

    Aimee

    Comment

    Working...