[Solved] Not all code paths return a value when parsing csv files [closed]


If your file pca_mk_addresslist.csv is empty, your foreach loop will never be entered (foreach object in an empty collection yields zero iterations). Thus, since you return only in the loop, the case of an empty file never returns anything. That’s not allowed.

if pca_mk_addresslist.csv has any lines
    return something
else
    ?

You need to tell it what your default value should be. It might well be null, but you have to tell it that. It isn’t the compiler’s job to assume what you want your program to do. Only to make it easier for you to write it.

As for your second question, it looks like your file is, in fact, empty. Either that or no fields match. You might want to add in some better default text, or step through the program to discover which it is.

Edit:

As per comment, what you want is something more like

private String GetAddress(String addressText)
{
    var strLength = File.ReadAllLines("pca_mk_addresslist.csv");

    var strLines = File.ReadLines("pca_mk_addresslist.csv");
    foreach (var line in strLines)
    {
        var fields = line.Split(',');

        foreach (var field in fields)
        {
            if (field == addressText)
                return field;
        }
    }

    return null;
}

3

solved Not all code paths return a value when parsing csv files [closed]