I get this error message in the last line of OnPlayFabError method:
return output;
Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'
What is wrong with the line return 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:
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:
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;
}
}
return Task.FromResult(output);which returns completedTaskwithResult == output- Dmitry BychenkoOnPlayFabErrorso just return string, not Task<string>. The error is telling you that a string is not Task<string>, you could return Task.FromResult(output) but that is pointless because, again, you have no Tasks to run. - CrowcoderTask.FromResultit, or better yet, not return a task and mark your method asstring OnPlay...and return it - MindSwipe