473,668 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arraylists

1 New Member
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!

Expand|Select|Wrap|Line Numbers
  1. // declaring the array list
  2.             ArrayList objClaimData = new ArrayList();
  3.  
  4.             // creating an instance of the class
  5.             clsClaimData datClaims = new clsClaimData();
  6.  
  7.             // declaring the variable for streamreader
  8.             string file_name = "D:\\Bhagavan\\My Documents\\Visual Studio 2010\\Projects\\ClaimsReserving\\ClaimsReserving\\claims.csv";
  9.             string textLine = "";
  10.             string[] colval = null;
  11.  
  12.             // opening the file using the streamreader
  13.             System.IO.StreamReader objReader;
  14.             objReader = new System.IO.StreamReader(file_name);
  15.  
  16.             // looping thru the text file until all lines are read and stored in the arraylist.            
  17.             while ((textLine = objReader.ReadLine()) != null)
  18.                 {
  19.                     colval = textLine.Split(',');
  20.  
  21.                     datClaims.Product = Convert.ToString(colval[0]);
  22.                     datClaims.OrginYear = Convert.ToInt32(colval[1]);
  23.                     datClaims.DevYear = Convert.ToInt32(colval[2]);
  24.                     datClaims.IncrementValue = Convert.ToDouble(colval[3]);
  25.  
  26.                     objClaimData.Add(datClaims);
  27.  
  28.                     Console.WriteLine(textLine);
  29.                 }
  30.  
What is wrong with the above ? Please help. I am new to C#.
Jun 3 '12 #1
1 2381
Aimee Bailey
197 Recognized Expert New Member
Hey there, the problem I believe is that you need to create an instance of clsClaimData for each line, this should help:

Expand|Select|Wrap|Line Numbers
  1.  // declaring the array list
  2.  ArrayList objClaimData = new ArrayList();
  3.  
  4.  // declaring the variable for streamreader
  5.  string file_name = "D:\\Bhagavan\\My Documents\\Visual Studio 2010\\Projects\\ClaimsReserving\\ClaimsReserving\\claims.csv";
  6.  
  7.  foreach(string line in File.ReadAllLines(file_name))
  8.  {
  9.     string colval = line.Split(',');
  10.  
  11.      // creating an instance of the class (per line).
  12.     clsClaimData datClaims = new clsClaimData();
  13.  
  14.     datClaims.Product = colval[0];
  15.     datClaims.OrginYear = int.Parse(colval[1]);
  16.     datClaims.DevYear = int.Parse(colval[2]);
  17.     datClaims.IncrementValue = double.Parse(colval[3]);
  18.  
  19.     objClaimData.Add(datClaims);
  20.     Console.WriteLine(line);
  21.  }
  22.  
All the best!

Aimee
Jun 4 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1091
by: Brandon Potter | last post by:
Trying to find the best way to find common entries in an x number of ArrayLists or arrays of integers. Curious if there is a method already available in .NET to do just this very thing. Situation is that each array contains search results for a given criteria, but the end result should only give the search results that are common to all arrays of results. i.e., SearchArrayOne = 100
0
1679
by: James | last post by:
I see that variations on this question have appeared before but I'm still completely stumped. I'm developing an application with a fairly robust graphics component for 3D rendering. I've written all the 3D rendering scripts, but what's really hanging me up is how to I pass information from an one-dimensional arraylist to a two-dimensional array. Here's what I'm trying to do: 1) My program generates an arraylist called "mpoints" of 2D...
1
2595
by: godsella | last post by:
First i have two stored procedures, i have passed the values of each one into two different arraylists of dates. how can i compare the two arraylists of dates? Thanks in advance
5
2387
by: drdave | last post by:
I would like to have ten arraylists created within a loop.. is there a conversion or something I can do to acheive this.. pseudo: Dim counter As Integer = 0 Dim ArrName As ArrayList '******** LOOP OVER THE VALUES ********************
0
1236
by: steve | last post by:
I'm looking for a code example how to compare the values in a given record in different arraylists two arraylists, two fields in each record, both defined as string I'm thinking that it's something like If ((fieldname)array1(x)) == ((fieldname)array2(x)) Then do something, where x is an integer used for the index/record or possibly
3
3210
by: steve | last post by:
I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist I have this bit of code working for arrays but cant get it working for arraylists The secone argument here (1) represents the second field in the row, with arraylists I get a message saying to many arguments. Can I do this with arraylists or do I need to copy the arraylists to arrays? Thanks For q As Integer = 0 To 9 For j...
4
1684
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of Arraylists, meaning, I don't know how many I should create until runtime. I will be reading a table, and 0,1,2, or more fields have to be put into individual ArrayLists. How do I create an unknown number of ArrayLists? Right now I put all the values into one ArrayList. I know how many fields are in the ArrayList so I then know that every one, or every other one, or
2
4009
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of ArrayLists, that is I don't know that until runtime. Of course I can do this if I knew I needed two: ArrayList IntervalArray1 = new ArrayList(); ArrayList IntervalArray2 = new ArrayList(); but what if I don't know how many I need at build time?
1
1761
by: Newbie19 | last post by:
I'm just learning java arrays/arraylists and was wondering what are the best books for learning java arrays/arraylists? I know practice is the best way to learn, but I have a hard time remembering the little items on arrays/arraylists. Thanks, Newbie
3
2053
by: =?Utf-8?B?Sm9zaFA=?= | last post by:
Hi All, I am attempting to compare values in two arraylists to make sure all the values are the same. I am running into trouble with my code if both arraylists compare okay up until a point and I am looping using the length of the primary arraylist. For example: oApproved (3 elements here): Red Green Yellow
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8371
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2782
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.