2

The following code does not want to compile. I am getting an error message:

The best overloaded method match for 'System.Collections.Generic.List.Add(string[])has some invalid arguments

But I cannot see what is wrong. Any suggestions?

public static List<string[]> ReadFromDelimitedFile (string Path)
    {
        int i=0;
        List<string[]> AllContracts = new List<string[]>();
        using (TextFieldParser parser = new TextFieldParser(Path))
        {
            parser.Delimiters = new string[] { "\t" };
            while (true)
            {
                AllContracts.Add(i)= parser.ReadFields();
                if (AllContracts.Add(i) == null)
                {
                    break;
                }
                i++;

            }
        }

        return AllContracts;
    }
2
  • 1
    you're attempting to add the index... maybe you meant to use the index? AllContracts[i]?
    – myermian
    Commented Sep 26, 2012 at 16:40
  • the line if (AllContracts.Add(i) == null) will also cause a compiler error. Add returns void. Commented Sep 26, 2012 at 16:41

4 Answers 4

8

You want:

AllContracts.Add(parser.ReadFields());
2
  • Add temp variable to check result null
    – FLCL
    Commented Sep 26, 2012 at 16:41
  • +1. The type of objects in List is string[]. Hence you want to add string arrays and not integers. Commented Sep 26, 2012 at 16:42
6

You're trying to call Add(int) on a List<string[]> - and then assign the result a value. Basically, this line is broken in multiple ways:

AllContracts.Add(i)= parser.ReadFields();

Perhaps you really wanted:

string[] fields = parser.ReadFields();
if (fields == null)
{
    break;
}
AllContracts.Add(fields);

At this point, you don't need i at all - and you won't end up with a null entry at the end of the list (which would quite possibly have caused you problems later - it's certainly unusual to want a null-terminated list).

Also, I'd advise against using PascalCasing for local variable names.

1

The Add method is not an indexer. You call it with the data that you want to add, and you can't use it to read data from the collection.

You should put the values that you parse in a variable so that you can check it before adding it to the collection, so that you don't add the null reference to the collection:

while (true) {
  string[] fields = parser.ReadFields();
  if (fields == null) {
    break;
  }
  AllContracts.Add(fields);
}

You can also do the assignment and the check in the while:

string[] fields;
while ((fields = parser.ReadFields()) != null) {
  AllContracts.Add(fields);
}
1

Your while loop can be replaced with the following:

while(!parser.EndOfData)
{
    AllContracts.Add(parser.ReadFields());
}

As has been mentioned, the Add method doesn't take an index, it will figure out what the index of the next item should be all on its own; you just need to give it the actual item to add.

Additionally you can use parser.EndOfData to determine if you have more items to add rather than checking ReadFields() for null. It reads much easier.

1
  • Wow, some many great answers! Thank you all guys, I see my error now! Commented Sep 26, 2012 at 17:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.