0

I'm reading a local csv file which has data and I will eventually use to load into a database. My question is simple in nature but I can't seem to grasp the concept. Below is my code. Pretty simple stuff.

      static void loadTables() {
        int size = new int();
        string[] empid = new string[0];
       
        //string[] empid = new string();
        List<string[]> EmployeeName = new List<string[]>();
        List<string[]> EmployeeId = new List<string[]>();
        List<string[]> Group = new List<string[]>();
        List<string[]> Org = new List<string[]>();
        List<string[]> Fund = new List<string[]>();
        try {
            using (StreamReader readFile = new StreamReader("C:\\temp\\groupFundOrgReport.csv")) 
            {
             string line;
              string[] row;
              size = 0;
              while ((line = readFile.ReadLine()) != null)
              {
                  row = line.Split(',');
                  /*resize the array up by 1*/
                  Array.Resize(ref empid,empid.Length+1);
                  /*I'm using size as a counter to add to the slot on the array.*/
                  empid[size] = row[0].Remove(0,1);
                  // here I receive error (the best overload match of system generic list?...etc) 
                  EmployeeName.Add(row[0]);
                  size++;
                  
              }

             }
           }
    
        catch(Exception e){

            Console.WriteLine(e);
        
        }
    
    
    
    
    }

I have a list of string but any attempts to add a string to it gets me an error. In other words if I try to do this EmployeeName.Add(row[0].ToString); I get an error. However if I comment the line out I can use an old fashion array. I really like to use a list but I can't seem to be able to pass the value that I want. Can someone please point me in the right direction?

7
  • i have also tried EmployeeName.Add(row[0]); with no luck Commented Jun 29, 2012 at 1:58
  • Isn't row[0] a single string - not a string[]? Commented Jun 29, 2012 at 1:59
  • Error 1 The best overloaded method match for 'System.Collections.Generic.List<string[]>.Add(string[])' has some invalid arguments Commented Jun 29, 2012 at 2:01
  • empid should be a List<string> instead of a string[]. It's much easier to add an item to a List than an array, due to the differences in how those two types are designed. Commented Jun 29, 2012 at 2:03
  • yes row[0] is a single string that changes during the while statement. if i just do row it works but then thats not really what im looking for i like to get the first column into its own array. Commented Jun 29, 2012 at 2:04

4 Answers 4

2

I guess from your code that the employee name is the first field of the CSV file.

You have declared EmployeeName as a List of arrays of strings List<string[]>, not as a list of strings List<string>.

Row[0] is the first string in an array, so you are trying to add a string to a list that is expecting you to add an array of strings.

You should just declare EmployeeName as a List<string>, using a line like:

List<string> EmployeeName = new List<string>();

or

var EmployeeName = new List<string>();
Sign up to request clarification or add additional context in comments.

1 Comment

i get it now. i was using list of arrays when what i was trying to use is list of strings. I feel like a real big idiot. Forgive my noobness. Thanks for the response.
2

Your problem is the declaration of EmployeeName, it is a List of string arrays:

List<string[]> EmployeeName = new List<string[]>();

Change it to:

var EmployeeName = new List<string>();

Or, use the List<string[]> accordingly ...

Comments

2

EmployeeName is a List<string[]> so you have to write:

EmployeeName.Add(row);

To remove empty entries while splitting a string use:

row=line.Split(New String() {","},
                     StringSplitOptions.RemoveEmptyEntries);

Comments

1

All of them are List's of String Array's

List<string[]> EmployeeName = new List<string[]>(); 
List<string[]> EmployeeId = new List<string[]>(); 
List<string[]> Group = new List<string[]>(); 
List<string[]> Org = new List<string[]>(); 
List<string[]> Fund = new List<string[]>();

Only variable you can add would be like

//Define array of strings
var strArr = new string[] {"abc", "xyz"};

then you can call

EmployeeName.Add(strArr);

although changing the List generic type to string type will solve your problem

List<string> EmployeeName = new List<string>(); 
List<string> EmployeeId = new List<string>(); 
List<string> Group = new List<string>(); 
List<string> Org = new List<string>(); 
List<string> Fund = new List<string>();

var str = "My String";

EmployeeName.Add(str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.