Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
not all code paths return a value visual studio c#. I have for each if else and try catch statements in a while loop. I have been looking for hours and cannot find what its talking about.


public static Player Load(out bool newP)
        {
            newP = true;
            Console.Clear();
            string[] paths = Directory.GetFiles("saves");
            List<Player> players = new List<Player>();
            int idCount = 0;

            BinaryFormatter binForm = new BinaryFormatter();
            foreach (string p in paths)
            {
                FileStream file= File.Open(p, FileMode.Open);
                Player player = (Player)binForm.Deserialize(file);
                file.Close();
                players.Add(player);
                
            }

            idCount = players.Count;

            while (true)
            {
                Console.Clear();
                Console.WriteLine("Chose a Saved player:");

                foreach (Player p in players)
                {
                    Console.WriteLine(p.id + ": " + p.name);
                    
                }
                

                Console.WriteLine("Please input player name or id(id:# or playername)");
                Console.WriteLine("'(C)reate' new save");
                string[] data = Console.ReadLine().ToLower().Split(':');

                try
                {
                    if (data[0] == "id")
                    {
                        if (int.TryParse(data[1], out int id))
                        {
                            foreach (Player p in players)
                            {
                                if (p.id == id)
                                {
                                    return p;
                                }
                                return p;

                            }
                            Console.WriteLine("ID number not found");
                            Console.ReadKey();

                            
                        }
                        else
                        {
                            Console.WriteLine("ID is not a valid number!");
                            Console.WriteLine("Any key to continue!");
                            Console.ReadKey();

                        }

                    }
                    else if (data[0] == "create" || data[0] == "c")
                    {
                        Player newPlayer = NewStart(idCount);
                        newP = true;
                        return newPlayer;

                    }
                    else
                    {
                        foreach (Player player in players)
                        {
                            if (player.name == data[0])
                            {
                                return player;
                            }
                        }
                        Console.WriteLine("Name not found");
                        Console.ReadKey();

                    }
                    if (data[0] == "e" || data[0] == "exit")
                    {
                        break;
                    }
                    else { Console.WriteLine("Invalid selection"); }

                }
                catch (IndexOutOfRangeException)
                {
                    Console.WriteLine("ID is not a valid number!");
                    Console.WriteLine("Any key to continue!");
                    Console.ReadKey();
                    
                }
                
            }
            
            
            
            
        }

    }
}


What I have tried:

placed a return just about anywhere it would let me tried changing it back to void but the returns are needed to escape.
Posted
Updated 27-Feb-23 3:15am
Comments
PIEBALDconsult 26-Feb-23 22:12pm    
How about at the end? Or add a throw to the catch?
dylan bryant 26-Feb-23 22:17pm    
i didnt know you could add a catch to a throw im still learning all this. but i stumbled upon the answer it was right in my face i appreciate it though

if (data[0] == "e" || data[0] == "exit")
                    {
                        break;
                    }
                    else { Console.WriteLine("Invalid selection"); }

this section was throwing off the whole load scene i deleted it and it works like normal
 
Share this answer
 
Are you sure about this code?
C#
if (int.TryParse(data[1], out int id))
{
    foreach (Player p in players)
    {
        if (p.id == id)
        {
            return p;
        }
        return p;

    }
If the user enters a valid number at all, it will always return the first player in the collection ...
 
Share this answer
 
What if all loops and conditions fall through, you're not returning anything at the end?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900