Home > OS >  Why are these functions displayed as not returning a value?
Why are these functions displayed as not returning a value?

Time:12-08

the two functions

the errors that they return

I'm using Visual Studio 2019

I tried re-writing these functions in different places in the code, it changed nothing.

CodePudding user response:

You need to move the second return outside of the foreach - the way it's written - it will always return after the first element, and does not return anything if the collection is empty (which is the reason for the error).

If you paste the code as text instead of images, I can give the exact correct code.

CodePudding user response:

Please see the corrected code below:

    static Location locationMenu()
    {
        Console.WriteLine("Enter name of location \n");
        foreach (Location l in availableLocations)
        {
            Console.WriteLine(l.getName());
        }
        string choice = Console.ReadLine();
        foreach (Location l in availableLocations)
        {
            if (l.getName() == choice)
            {
                return l;
            }
        }
        return null;
    }

    static int numberComplete()
    {
        int count = 0;
        foreach (Location l in availableLocations)
        {
            if (l.getComplete())
            {
                count  ;
            }
        }
        return count;
    }
  • Related