1

It has been a few years since i have messed with C# or programming for that matter. I am attempting to read a CSV file to multiple integers. The CSV file is formatted as follows

box1,3
box2,6
box3,10
...
box160,1  

So far for my code i have the below. The TextFieldParser i got off of another question. I am not sure if that is even what i need. I do not need to keep the information before the "," on the CSV file i just need the information after the "," to correspond with the correct int in my code. Any help or pointers would be much appreciated.

int box1;
int box2;
int box3;
... 
int box160;

using (TextFieldParser parser = new TextFieldParser(@"C:\SprinklerTimer\DailySprinklerTimer.csv"))
{
     parser.TextFieldType = FieldType.Delimited;
     parser.SetDelimiters(",");
     while (!parser.EndOfData)
     {
           //Processing row
          string[] fields = parser.ReadFields();
          foreach (string field in fields)
          {
               //TODO: Process field
          }
     }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
AWilker
  • 21
  • 2
  • What is your specific _question_? Does the code you have work? If so, I don't understand what the question is about. If it doesn't work, you need to provide a good [mcve] that reliably reproduces whatever problem you're having, and explain _precisely_ what that problem is. As far as pulling numbers from strings, comma-delimited or otherwise, Stack Overflow is chock full of such questions already, many of which directly address your specific scenario. Did you look for any? What did you find? What about the answers to those questions did you have trouble understanding? – Peter Duniho Sep 21 '16 at 23:52

1 Answers1

-1

For starters manually making 160 box integers is very inefficient. Try using an array.

int[] box; // declare numbers as an int array of any size
box = new int[160]; //New box array for 0-159

and you can either do the same for the value, I.e. a value[] array or what I would is make box a struct instead of int, where the struct has two integers.

struct SimpleStruct
{
int name, value;
}

SimpleStruct[] box; // declare numbers as an int array of any size
box = new SimpleStruct[160]; //New box array for 0-159

And to go on to your actual question, I would recommend this question which will help if you implement my above changes.

Read CSV files to two arrays c#

Community
  • 1
  • 1
Anthony Drury
  • 128
  • 2
  • 12
  • Thanks for the tip on arrays. Like I said it has been a few years. Thanks for the other article on reading the cvs file into two arrays i didn't seem to find it before. – AWilker Sep 22 '16 at 22:11