Help importing csv

  • Thread starter Thread starter MFRASER
  • Start date Start date
M

MFRASER

Does anyone have a good way of importing a csv file into an object?

Entity1,123,22,red,blue
Entity2,123,22,red,blue
Entity3,123,22,red,blue
Entity4,123,22,red,blue
Entity5,123,22,red,blue

Currently I am spliting the data and then creating two for loops to loop
through the rows and then each column to assign the corresponding object
property.
 
What about this option:

Add a new constructor to your object that takes a string in like so:
public void MyObject(string PropertiesCSV)
{
string[] ListOfProperties = PropertiesCSV.Split(",")
for(int i=0;i<ListOfProperties.GetUpperBound(0);i++)
{
this.var1 = ListOfProperties;
...
this.var99 = ListOfProperties
}
}

Then all you need is one loop for each line in the CSV. I know this doesn't
exactly change your problem, it just shifts where the inner loop sits.

Hope this has been of some help.

Greg
 
Back
Top