简体   繁体   中英

Return a file location from a switch-statement

I'm pretty new to programming but I'm trying to have my method return a value that contains a file location which is dependent on a users desired choice. I've been fiddling with this for over a day now and am stuck on how to return the value correctly, I keep it keeps telling me not all code paths return a value. How do I fix this and have the code path be returned to the main

public static string fileLocation()
    {
        int fileRequest = 10;
        bool errorCheck = true;
        string filePath;

        while (errorCheck == true)
        {
            Console.Write(">Enter '1' through '9' to choose a hand.");
            Console.Write("Enter '0' for random.");
            fileRequest = Convert.ToInt16(Console.ReadLine());

            switch (fileRequest)
            {
                case 0:
                    Console.WriteLine(">Random selection loading.");
                    Random rnd = new Random();
                    fileRequest = rnd.Next(10);
                    errorCheck = true;
                    return (null);

                case 1:
                    Console.WriteLine(">Loading file one.");
                    filePath = Path.GetFullPath("Flush.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 2:
                    Console.WriteLine(">Loading file two.");
                    filePath = Path.GetFullPath("FourKind.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 3:
                    Console.WriteLine(">Loading file three.");
                    filePath = Path.GetFullPath("FullHouse.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 4:
                    Console.WriteLine(">Loading file four.");
                    filePath = Path.GetFullPath("Pair.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 5:
                    Console.WriteLine(">Loading file five.");
                    filePath = Path.GetFullPath("RoyalFlush.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 6:
                    Console.WriteLine(">Loading file six.");
                    filePath = Path.GetFullPath("Straight.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 7:
                    Console.WriteLine(">Loading file seven.");
                    filePath = Path.GetFullPath("StraightFlush.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 8:
                    Console.WriteLine(">Loading file eight.");
                    filePath = Path.GetFullPath("ThreeKind.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 9:
                    Console.WriteLine(">Loading file nine.");
                    filePath = Path.GetFullPath("TwoPair.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                default:
                    Console.WriteLine(">Invalid request.");
                    filePath = "Invalid";
                    errorCheck = true;
                    return (null);
            }
        }

I'm assuming what you're trying to do is take an integer between 0 and 9 as input. If it's 0, you want to treat it randomly as 1 through 9. If it's anything else, you want to ask for input again. This should do it (untested):

public static string FileLocation()
{
    while (true)
    {
        Console.Write(">Enter '1' through '9' to choose a hand.");
        Console.Write("Enter '0' for random.");
        int fileRequest = Convert.ToInt16(Console.ReadLine());
        if (fileRequest == 0)
            fileRequest = (new Random()).Next(1, 10);

        switch (fileRequest)
        {
            case 1:
                Console.WriteLine(">Loading file one.");
                return Path.GetFullPath("Flush.txt");

            case 2:
                Console.WriteLine(">Loading file two.");
                return Path.GetFullPath("FourKind.txt");

            case 3:
                Console.WriteLine(">Loading file three.");
                return Path.GetFullPath("FullHouse.txt");

            case 4:
                Console.WriteLine(">Loading file four.");
                return Path.GetFullPath("Pair.txt");

            case 5:
                Console.WriteLine(">Loading file five.");
                return Path.GetFullPath("RoyalFlush.txt");

            case 6:
                Console.WriteLine(">Loading file six.");
                return Path.GetFullPath("Straight.txt");

            case 7:
                Console.WriteLine(">Loading file seven.");
                return Path.GetFullPath("StraightFlush.txt");

            case 8:
                Console.WriteLine(">Loading file eight.");
                return Path.GetFullPath("ThreeKind.txt");

            case 9:
                Console.WriteLine(">Loading file nine.");
                return Path.GetFullPath("TwoPair.txt");

            default:
                Console.WriteLine("Invalid request.");
                break;
        }
    }
}

Well, you hit a case which the compiler doesn't understood right.

You are checking for errorCheck in a while loop and inside have a case. that case will always do a return, but as the compiler sees a probability that errorCheck got true without a return then it complains about the possibility of a execution path where no return exists.

First of all, you are doing a return in all the cases, so that while can be safely removed as it does nothing. Else, if you plan to not return always and really loop until the user selects a correct option ignore errorCheck, just do a while(true){ ... } as your switch will return when a correct option is selected.

An example written in VB, but it should show the logic. My variable came from a database variable filled on the OnLoad event, but other than that, it's pretty much what you're looking to do.

Private Sub ShowPDF()

    Dim mySelection As Integer = _myDb.MyHuntingArea
    'the path where the pdf files are located
    Dim filePath As String = MyMachine.AssemblyDirectory & "\Regulations\"
    'variable for the appropriate file name to get
    Dim fileName As String = ""
    Select Case mySelection

        Case 1 
            fileName = filePath & "Area1.pdf"
        Case 2 
            fileName = filePath & "Area2.pdf"
        Case 3  
            fileName = filePath & "Area3.pdf"
        Case Else
            MessageBox.Show("We cannot determine what area you are requesting regulations for.  Make sure it is set under Setup.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Select

    If Not fileName = "" Then
        Try
            System.Diagnostics.Process.Start(fileName)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM