4 Replies - 969 Views - Last Post: 26 September 2011 - 06:03 PM Rate Topic: -----

#1 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 28-June 10

Not all code paths return a value

Posted 26 September 2011 - 02:07 PM

Hello Dream in Code!

I'm getting the error "Not all code paths return a value". I know what the error means but I am buggered if I can see why it is happening in my code - something is returned whether the IF statement evaluates to true or false.

private Server loadServer()
        {
            string[] filenames = Directory.GetFiles(Application.StartupPath + @"\XML\Server\", "*.xml");

            foreach (string filename in filenames)
            {
                if (filename.Contains(selectedBranch))
                {
                    return GlobalFunctions.LoadServer(filename);
                }
                else
                {
                    MessageBox.Show("Server Information Not Found!!");
                    return null;
                }

            }
        }



and for clarity, this is the method called by GlobalFunctions.LoadServer

  public static Server LoadServer(string filename)
        {
            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.CloseInput = true;

                XmlSerializer serialiser = new XmlSerializer(typeof(Server));
                XmlReader reader = XmlReader.Create(new FileStream(filename, FileMode.Open), settings);

                Server server = (Server)serialiser.Deserialize(reader);

                reader.Close();

                return server;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }



I added return null immediately after the foreach loop exits and the code now compiles

I'm thinking that it's possible for the string array to be empty, so the foreach loop will not run?

This post has been edited by AdoTheLimey: 26 September 2011 - 02:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Not all code paths return a value

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Not all code paths return a value

Posted 26 September 2011 - 02:08 PM

If there are no elements in your filenames then the neither path is ever run, and you have no return after the foreach() block.

Just put a return null; after the foreach block
Was This Post Helpful? 1
  • +
  • -

#3 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 28-June 10

Re: Not all code paths return a value

Posted 26 September 2011 - 02:10 PM

View PosttlhIn`toq, on 26 September 2011 - 02:08 PM, said:

If there are no elements in your filenames then the neither path is ever run, and you have no return after the foreach() block.

Just put a return null; after the foreach block



Thanks tlhIn`toq - I had a moment of enlightenment once I hit the post button and got it figured out - it had never occurred to me that the compiler assumes that there is a chance that a foreach loop would never run.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,455
  • Joined: 08-June 10

Re: Not all code paths return a value

Posted 26 September 2011 - 03:02 PM

You do realize that the loop will either run 0 or 1 times, and never more, right? Just making sure that's desired behavior.

Basically, it'll check the very first file in the list. If that file name doesn't contain what you're searching for, it'll return null.
Was This Post Helpful? 0
  • +
  • -

#5 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 28-June 10

Re: Not all code paths return a value

Posted 26 September 2011 - 06:03 PM

View PostCurtis Rutland, on 26 September 2011 - 03:02 PM, said:

You do realize that the loop will either run 0 or 1 times, and never more, right? Just making sure that's desired behavior.

Basically, it'll check the very first file in the list. If that file name doesn't contain what you're searching for, it'll return null.


Yup, that happened the first time I ran it, I fixed it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1