gpt4 book ai didi

c# - 为什么第二个 IF 语句检测到无法访问的代码?

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:33 27 4
gpt4 key购买 nike

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;

}

这一行:

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

'if' 带有绿色下划线,表示检测到无法访问的代码。我该如何修复它以及为什么它是无法访问的代码?

也许我需要使用 result = 而不是 return ?但这似乎不是问题所在。

最佳答案

因为除此之外,您还有:

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

ifelse 为真,因此代码无论如何都会从该 block 返回,并且不会执行其下面的代码。

你有三个选项,所以你似乎在寻找一个 switch ,在这种情况下它是最易读的:

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";
}

或者使用字典将服务状态转换为您的应用程序状态,或者按照@Thiago 的建议使用if () else if () else .

关于c# - 为什么第二个 IF 语句检测到无法访问的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32398513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com