skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search