0

I am having issues with the following code. I am retrieving a JSON object as a string and then wish to return this from my method so it can be used elsewhere. When I do however I get the message:

'filmsGlossary.searchQueries.returnJson(object); returns void, a return keyword must not be followed by an object expression'

public async void returnJson(object term)
    {
        //Set Variables
        var searchFormat = "&format=json";
        var termValue = term;         

        var httpClient = new HttpClient(); 

        try
        {                     
            //Set web service URL format
            string baseURI = "http://localhost/filmgloss/webService/web-service.php?termName=";
            string userURI = baseURI + termValue + searchFormat;

            //Send URL to web service and retrieve response code. 
            var response = await httpClient.GetAsync(userURI);
            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();
            return content.ToString(); 

        }
        catch (HttpRequestException hre)
        {               

        }
        catch (Exception ex)
        {

        }
}

Originally I was only returning

return content;

However after reading it seemed that the issue might be that I needed to change this to:

return content.ToString();

However this did not help. I also read that I could change it to a synchronous, rather than asynchronous method and change the method to be 'public string' however I am only just learning c# and don't yet fully understand the implications of this (or how to do it).

Is it possible to resolve this error within the code I already have?

I would also like to understand the cause of the issue rather than just know the solution.

Thanks.

1 Answer 1

2

You really should paste the error messages that you are getting.

Why does your function declaration return void? It should return Task<string>.

public async Task<string> returnJson(object term)

Also in the body you should return the Task, like this:

await response.Content.ReadAsStringAsync();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Mihai, that is the full error message. The only information I'm missing from the message are the line numbers which I didn't think relevant as I haven't posted the full code. As for why it declares void I created this method based on a MSFT MSDN tutorial for Windows 8 app development. The code shown is the first part of the original method as I am trying to split it into two methods. The original method (in which I don't need to return any values) works perfectly.
Also, I will have to read into Task<string> - I am completely unfamiliar with it having just started.
It looks like you have a lot of way to go so good luck with it. The main problem in your code was that in the function declaration you declared the return type as void (nothing) while in the body you were trying to return something. The function declaration must match what you are expecting it to return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.