-2

I'm creating a basic MVC Copy File App and am returning a Not All Code Paths Return a Value error in my GetSource method. I've researched the issue and have tried a few things, but the error still generates. I believe the error is in the foreach loop somewhere, but I tried returning a null afterwards, which just threw up a different error. Any help is appreciated as I am a beginner. Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;


namespace April24
{
    public class Andrew
    {
        public string Copy()
        {
            return "Done Copying";
        }
        public string GetSource() //Error is here
        {
            copyFiles(10);
        }
        public static string production = @"C:\Users\test\Desktop\Production";
        public static string renameFolder = @"C:\Users\test\Desktop\RenameFolder\";

        static private void copyFiles(int numberOfFiles)
        {
            List<string> files = System.IO.Directory.GetFiles(production, "*").ToList();
            IEnumerable<string> filesToCopy = files.Where(file => file.Contains("Test_Name")).Take(10);

            foreach (string file in filesToCopy)
            {

                string destfile = renameFolder + System.IO.Path.GetFileName(file);

                System.IO.File.Copy(file, destfile, true);
            };
            /*tried return null; here but still threw error*/

        }
    }
}
AndrewC10
  • 357
  • 1
  • 4
  • 20
  • 1
    what do you want `GetSource` to return? – Daniel A. White Apr 26 '16 at 23:54
  • 4
    Because your not returning anything in `GetSource()` (you have specified it need to return `string`). And you `copyFiles()` is `void` (it does not return anything) –  Apr 26 '16 at 23:56
  • Thanks Daniel. It is supposed to copy files from 'production' into 'rename folder'. This code works as a Console Application, but threw the error when I copied it over to an MVC framework. – AndrewC10 Apr 26 '16 at 23:56
  • Yes, thank you both, just moved my return null; into GetSource. Works perfectly now!! – AndrewC10 Apr 26 '16 at 23:59
  • 2
    logically it makes no sense. if the function always returns null, declare its return type as void. Also note parameter numberOfFiles is never used – George Chen Apr 27 '16 at 00:01
  • 1
    Please consider to close/delete question as it does not bring any new information to the site (as there are plenty of exactly the same questions like [this](http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value)) and does not provide [MCVE]. – Alexei Levenkov Apr 27 '16 at 00:12
  • 1
    Note to upvoters: please add reasonable explanation why existing questions did not help OP. – Alexei Levenkov Apr 27 '16 at 00:12

1 Answers1

3

Your method signature says GetSource returns a string, but function body returns nothing, which is what compiler complains about

public string GetSource() //Error is here
{
      copyFiles(10);
      return  "A string here"
}
George Chen
  • 6,592
  • 5
  • 21
  • 23