Reputation: 1380
I'm using this in an application where I am allowed to extend its functionality using my own code.
It's been coded in C#.
Result()
will return the answer it gets from my Node.js server (FinalResponse
) and I'm going to access it in the application like below, but I get the error in the title when I try to use EchoOut().
var returnedValue = Lib.Result();
App.EchoOut(returnedValue); // EchoOut allows you to test the function you wrote, it echoes string on the screen, but in this case, it gives an error.
Error
cannot implicitly convert type void to object
When I test the Lib.Result()
alone, I see the request is being made by the app, but I can't assign the returned value to a variable.
How can I assign the FinalResponse's
value to returnedValue
variable?
My code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace MainNameSpace
{
public class Lib
{
public void Result()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("192.168.1.101:8000/mytest");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string FinalResponse = stream.ReadToEnd();
Console.WriteLine(FinalResponse);
}
}
}
Upvotes: 6
Views: 48431
Reputation: 2500
You need to change the return type of the Result
method to string
and return the FinalResponse
instead of writing it to the console.
public string Result() // <-- "string" instead of "void"
{
var request = (HttpWebRequest)WebRequest.Create("192.168.1.101:8000/mytest");
using (var response = (HttpWebResponse)request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd(); // <-- return the result
}
}
It is also considered good practice to wrap the code using disposable objects (in this case, objects of type HttpWebResponse
, Stream
and StreamReader
) with using
blocks.
I also took the liberty of using var
instead of explicitly writing out the types of the variables because the types are already obvious from the right hand sides of the declarations.
Upvotes: 4
Reputation: 60584
The problem is that you're not returning anything from your function. Rewrite it to this and it will work:
public string Result()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("192.168.1.101:8000/mytest");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string FinalResponse = stream.ReadToEnd();
//Console.WriteLine(FinalResponse);
return FinalResponse;
}
Additionally, here's how to make the C# a little more idiomatic in terms of case conventions etc:
public string Result()
{
var request = (HttpWebRequest)WebRequest.Create("192.168.1.101:8000/mytest");
var response = request.GetResponse();
var stream = new StreamReader(response.GetResponseStream());
var finalResponse = stream.ReadToEnd();
return finalResponse;
}
Upvotes: 2
Reputation: 129
Your method Result() is of type void. But you are trying to assign a return value. Change the return type and use "return"
Upvotes: 3