简体   繁体   English

错误 CS0029:无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> '</string>

[英]Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'

I get this error message in the last line of OnPlayFabError method:我在 OnPlayFabError 方法的最后一行收到此错误消息:

return output;

Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'错误 CS0029:无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”

What is wrong with the line return output;?线路返回 output; 有什么问题? I want to get the value of the variable "output" in this line:我想在这一行中获取变量“输出”的值:

error = errormessage.Result;

What is wrong with my code?我的代码有什么问题?

MainClass.cs: MainClass.cs:

string Countryname = string.Empty;
string Cityname = string.Empty;
string Errormessage = string.Empty;
var GotNames = await Extensionmethods.GetPlayerCountryData(PlayerPlayFabID, "Country", "City");
Countryname = GotNames.countryname;
Cityname = GotNames.cityname;
Errormessage = GotNames.error;

Extensionmethods.cs:扩展方法.cs:

public static class Extensionmethods
{
    public static async Task<(string error, string countryname, string cityname)> GetPlayerCountryData(this string playerplayfabid, string country, string city)
    {
        string error = string.Empty;
        string countryname = string.Empty;
        string cityname = string.Empty;

        var resultprofile = await PlayFabClientAPI.GetUserDataAsync(new PlayFab.ClientModels.GetUserDataRequest()
        {
            PlayFabId = playerplayfabid,
            Keys = null
        });

        if (resultprofile.Error != null)
        {
            // Handle error if any
            var errormessage = OnPlayFabError(resultprofile.Error);
            error = errormessage.Result;
        }
        else
        {
            if (resultprofile.Result.Data == null || !resultprofile.Result.Data.ContainsKey(country) || !resultprofile.Result.Data.ContainsKey(city))
                Console.WriteLine("No Country/City");
            else
            {
                countryname = resultprofile.Result.Data[country].Value;
                cityname = resultprofile.Result.Data[city].Value;
            }
        }

        return (error, countryname, cityname);
    }

    public static Task<string> OnPlayFabError(this PlayFabError obj)
    {
        string output = string.Empty;
        switch (obj.Error)
        {
            case PlayFabErrorCode.AccountBanned:
                output = "Account is banned." + "Hier die geladenen Daten aus der Textdatei anzeigen.";
            break;
            case PlayFabErrorCode.EmailAddressNotAvailable:
                output = "E-mail address is already in use. Please choose another e-mail address.";
            break;
            case PlayFabErrorCode.InvalidParams:
                output = "Please fill out all the text fields.";
            break;
            case PlayFabErrorCode.InvalidUsernameOrPassword:
                output = "Wrong username or password.";
            break;
            case PlayFabErrorCode.UsernameNotAvailable:
                output = "Username not available. Please choose another username.";
            break;
            case PlayFabErrorCode.ConnectionError:
                output = "Connection error. Please check your network connection.";
            break;
            default:
                output = "Unknown error!";
            break;
        }

        return output;
    }
}

A method returning a task should almost always be an asynchronous method.返回任务的方法应该几乎总是异步方法。 If the method isn't async, then just return the type you want, no need to wrap it in a task.如果方法不是异步的,那么只需返回您想要的类型,无需将其包装在任务中。

That being said, all you need to do is change the return type of your OnPlayFabErrorAsync to string OnPlayFabErrorAsync(..) instead of Task<string> OnPlayFabErrorAsync(..) .话虽如此,您需要做的就是将OnPlayFabErrorAsync的返回类型更改为string OnPlayFabErrorAsync(..)而不是Task<string> OnPlayFabErrorAsync(..) So this is your new method:所以这是你的新方法:

public static string OnPlayFabError(this PlayFabError obj)
{
    // ...
}

A good rule of thumb is that if your function returns a Task , then the function name should end with Async .一个好的经验法则是,如果您的 function 返回一个Task ,那么 function 名称应该以Async结尾。 This then reminds you that you need to await that function.这会提醒您,您需要await function。

OnPlayFabError(this PlayFabError obj) becomes OnPlayFabErrorAsync(this PlayFabError obj) which then reminds you to await it: OnPlayFabError(this PlayFabError obj)变为OnPlayFabErrorAsync(this PlayFabError obj)然后提醒您等待它:

var errormessage = await OnPlayFabErrorAsync(resultprofile.Error);

In your case, there is no need for OnPlayFabError to be async (because it currently does no I/O), so it might be easier to just make it return a string instead of a Task<string> .在您的情况下,不需要OnPlayFabErrorasync的(因为它当前没有 I/O),所以让它返回一个string而不是一个Task<string>可能会更容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' C#错误:无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.Control&#39;(CS0029) - c# Error: Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029) 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) CS0029 C#无法将类型隐式转换为&#39;string []&#39; - CS0029 C# Cannot implicitly convert type to 'string[]' 无法隐式转换类型&#39;System.Threading.Tasks.Task <String> “串” - Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> &#39; - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> ' 在 c#</string> - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM