-1

I have declared a method in another class and it has an error "not all code paths return a value"

I would like it to return a true or false value to the main program.

But when I declare my method, public static void, another error produces, a return keyword must not be followed by an object expression.

public class FileSearch
{
    public static Boolean SearchFiles(string path1, string path2)
    {
        bool isIdentical = false;
        string content1 = null;
        string content2 = null;

        DirectoryInfo d1 = new DirectoryInfo(path1);
        DirectoryInfo d2 = new DirectoryInfo(path2);

        foreach (FileInfo f1 in d1.GetFiles("*.txt", SearchOption.AllDirectories))
        {
            foreach (FileInfo f2 in d2.GetFiles("*.txt", SearchOption.AllDirectories))
            {
                content1 = (File.ReadAllText(f1.DirectoryName + "\\" + f1));
                content2 = (File.ReadAllText(f2.DirectoryName + "\\" + f2));

                isIdentical = content1.Equals(content2, StringComparison.Ordinal);

                if (isIdentical == false)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • Please explain exactly what you want to achieve with this method. As it is now, you return a value after your first comparison. Do you want to get all identical files? – germi Nov 11 '13 at 10:26
  • What is the purpose of your `SearchFiles` method? My gut feeling is that you want to return `false` as soon as you find two different files, and `true` if all files (but shoudn't you compare files with the same name only?) are identical. If so, the `return false` is correctly placed, but the `return true` should be placed after the loops. – Alexis Pigeon Nov 11 '13 at 10:43
  • 2
    Possible duplicate of [c# returning error "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value) – Liam Jun 20 '18 at 09:15

6 Answers6

13

Your method SearchFiles only returns a value if isIdentical is false. If it's true, the method never returns.

To remove this error, write something like this:

public static Boolean SearchFiles(string path1, string path2)
{
    // do some work to assign a value to isIdentical
    // note that it would be idiomatic to just write "return isIdentical;" in this case
    // I keep it explicitly here for demonstration purposes 
    if (isIdentical == false)
    {
        return false;
    }
    else
    {
        return true;
    }
}

To your second question: If you declare your method as public static void you must not return any value. void means that the method will not give you anything back.

You might want to have a look at this: Methods (C# Programming Guide), especially the part about return values.

Edit: Since you have your if / else in a foreach loop, you need something like this:

public static Boolean SearchFiles(string path1, string path2)
{
    foreach(var item in collection)
    {
        // do some work to assign a value to isIdentical
        if (isIdentical == false)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    // in case the collection is empty, you need to return something
    return false;
}
germi
  • 4,628
  • 1
  • 21
  • 38
9

You need to return something if the if condition is not true

you can try this

return isIdentical 

and remove your if statement

so that it looks like this

public class FileSearch
{
        public static Boolean SearchFiles(string path1, string path2)
        {
            //Do your other work here
            return isIdentical ;
        }
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
2

you can try this code....i think it will help you.

public class FileSearch
    {
        public static Boolean SearchFiles(string path1, string path2)
        {
            bool isIdentical = false;
            string content1 = null;
            string content2 = null;
bool result=false;

            DirectoryInfo d1 = new DirectoryInfo(path1);
            DirectoryInfo d2 = new DirectoryInfo(path2);

            foreach (FileInfo f1 in d1.GetFiles("*.txt", SearchOption.AllDirectories))
            {
                foreach (FileInfo f2 in d2.GetFiles("*.txt", SearchOption.AllDirectories))
                {
                    content1 = (File.ReadAllText(f1.DirectoryName + "\\" + f1));
                    content2 = (File.ReadAllText(f2.DirectoryName + "\\" + f2));

                    isIdentical = content1.Equals(content2, StringComparison.Ordinal);

                    if (isIdentical == false)
                    {
                       break;
                    }
                    else
                    {
                        result=true;break;

                    }
break;
            }
return result;
        }

    }
doppelgreener
  • 4,809
  • 10
  • 46
  • 63
1
public static Boolean SearchFiles(string path1, string path2)
{
    if (isIdentical == false)
    {
        return false;
    }
    return true or false;
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Anand
  • 411
  • 3
  • 13
1
public static Boolean SearchFiles(string path1, string path2)
{

    if(isIdentical == false)
    {
        return false;
    }
    else
    {
        return true;
    }

}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
0

put this at the end of method

return isIdentical;

u are not returning any value. This may work