1
TaskCompletionSource<bool> sy;
        public string SendResponse(HttpListenerRequest request)
        {
            string result = "";
            string key = request.QueryString.GetKey(0);
            if (key == "cmd")
            {
                if (request.QueryString[0] == "upload status")
                {
                    if (Youtube_Uploader.fileuploadstatus == "uploading file")
                    {
                        Youtube_Uploader.fileuploadstatus = "";
                        return "uploading";
                    }
                    else
                    {
                        return "upload unknown state";
                    }

                    if (Youtube_Uploader.fileuploadstatus == "file uploaded successfully")
                    {
                        Youtube_Uploader.fileuploadstatus = "";
                        return "upload completed";
                    }
                    else
                    {
                        return "upload unknown state";
                    }
                }
                if (request.QueryString[0] == "nothing")
                {
                    return "Connection Success";
                }
                if (request.QueryString[0] == "start")
                {
                    StartRecrod();
                    result = "Recording started";
                }

                if (request.QueryString[0] == "stop")
                {
                    dirchanged = false;
                    StartRecrod();
                    result = "Recording stopped and preparing the file to be shared on youtube";
                    sy = new TaskCompletionSource<bool>();
                    WatchDirectory();
                    sy.Task.Wait();
                    Youtube_Uploader youtubeupload = new Youtube_Uploader(fileforupload);

                }
            }
            else
            {
                result = "Nothing have been done";
            }

            return result;

        }

This line:

if (Youtube_Uploader.fileuploadstatus == "file uploaded successfully")

The 'if' is with green underline and say Unreachable code detected. How can i fix it and why it's Unreachable code ?

Maybe i need to use result = and not return ? But that's not seems to be the problem.

Harim Abdu
  • 23
  • 6

3 Answers3

10

Because above that, you have:

if (...)
{
    return "uploading";
}
else
{
    return "upload unknown state";
}

Either the if or the else is true, so the code will return from that block anyway, and the code below it won't be executed.

You have three options, so you seem to be looking for a switch which is the most readable in this case:

switch (Youtube_Uploader.fileuploadstatus)
{   
    case "uploading file":
        Youtube_Uploader.fileuploadstatus = "";
        return "uploading";

    case "file uploaded successfully":
        Youtube_Uploader.fileuploadstatus = "";
        return "upload completed";

    default:
        return "upload unknown state";
}

Or use a dictionary to translate the service statuses to your application statuses, or use if () else if () else as suggested by @Thiago.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • CodeCaster it's working great. Just one small thing when the status is "upload completed" i want to reset it back to default to "upload unknown state" since i'm checking the status every second i want that once the status is "upload completed" then right after it reset it back to "upload unknown state" i mean make once the return "upload completed" but then right after it reset to default. – Harim Abdu Sep 04 '15 at 13:45
  • @Harim I'm not sure what you're asking. You could split this out into a `GetStatusMessage(string uploadStatus)` and `SetStatusMessage(string status)` method, then call those from where you want to (re)set the status. – CodeCaster Sep 04 '15 at 13:54
  • @Harim I think I get what you mean. I removed the `Youtube_Uploader.fileuploadstatus = "";` for brevity. I have now re-added them under their respective `case`s. – CodeCaster Sep 04 '15 at 17:31
5

because of your return statement. It will stop the if / else flux. Maybe you're searching for this:

if (Youtube_Uploader.fileuploadstatus == "uploading file")
                    {
                        Youtube_Uploader.fileuploadstatus = "";
                        return "uploading";
                    }
                    else if (Youtube_Uploader.fileuploadstatus == "file uploaded successfully")
                    {
                        Youtube_Uploader.fileuploadstatus = "";
                        return "upload completed";
                    }                   
                    else
                    {
                        return "upload unknown state";
                    }
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0
if (Youtube_Uploader.fileuploadstatus == "uploading file")
                {
                    Youtube_Uploader.fileuploadstatus = "";
                    return "uploading";
                    //return may happen here case 1
                }
                else
                {
                    return "upload unknown state";
                    // or here case 2
                }

Only two cases are possible.

                if (Youtube_Uploader.fileuploadstatus == "file uploaded successfully")

Transform your code to the following:

if (Youtube_Uploader.fileuploadstatus == "uploading file")
                {
                    Youtube_Uploader.fileuploadstatus = "";
                    return "uploading";
                    //return can happen here. Case 1
                }
                else if(some another condition) // case 2
                {
                    return "upload unknown state";
                    // or here case 2
                }
                //now this code is reachable 
                if (Youtube_Uploader.fileuploadstatus == "file uploaded successfully")
Yuriy Zaletskyy
  • 4,983
  • 5
  • 34
  • 54