Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<DetectLanguage.DetectResult[]>' to 'DetectLanguage.DetectResult[]'

I am trying to extend a program with a language code detection but I get this Error when i try to implement it.

Error CS0029 Cannot implicitly convert type ‘System.Threading.Tasks.Task<DetectLanguage.DetectResult[]>’ to ‘DetectLanguage.DetectResult[]’

private static Translation Translate(string sourceText, string lang)
        {
            DetectLanguageClient client = new DetectLanguageClient("API KEY");
            DetectResult[] results = client.DetectAsync(sourceText);

[...]

using the https://detectlanguage.com/ API

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The API that you’re calling returns a Task<T>, which is essentially an async operation – not the result of such.

To get the actual result of the async operation, you need to use await. Since await can only be used inside an async function, you also need to declare your Translate function as such;

private static async Task<Translation> Translate(string sourceText, string lang)
        {
            DetectLanguageClient client = new DetectLanguageClient("API KEY");
            DetectResult[] results = await client.DetectAsync(sourceText);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading