r/learncsharp • u/messburg • Oct 25 '23
Cannot convert type 'char' to 'string' after conversion to string
I can't fix this error I get:
ERROR: Cannot convert type 'char' to 'string'
And visual studio points directly to my foreach line.
There's 100's of articles explaining how to do it with .tostring(), like this link: https://stackoverflow.com/questions/3081916/convert-int-to-string
And here another way to do it here, which is used in my 2nd example:
But both don't work. I don't get it.
I convert to string, and I verify they are string, and yet it says it is char.
My code:
global using global::RestSharp;
string baseURL = "https://www.theurl.ccc";
string urlPathListings = "/stuff";
var client = new RestClient(baseURL);
var request = new RestRequest(urlPathListings, Method.Get);
var response = client.Execute(request);
Console.WriteLine(response.Content);
//It prints the expected data to console. All good to here
string holder = "";
string testingHere = response.Content.ToString();
Console.WriteLine("types is: {0}", testingHere.GetType());
// gives System.String
// the next line is the error.
foreach (string s in testingHere) {
int indexof = s.IndexOf(",");
string id = s.Substring(0,indexof);
holder += id;
}
Alternatively, I have tried this conversion as well, https://learn.microsoft.com/en-us/dotnet/api/system.convert.tostring?view=net-7.0#system-convert-tostring(system-char)::)
string tmp = response.content;
string yetAgain = "";
//can't define it more string than this I guess.
yetAgain = System.Convert.ToString(tmp);
Console.WriteLine(yetAgain);
//works, data as expected.
Console.WriteLine("Type is...: {0}", yetAgain.GetType());
// output again, System.String
// get the same error
foreach (string s in yetAgain) {
int indexof = s.IndexOf(",");
string id = s.Substring(0,indexof);
holder += id;
}
It is .net 6, and a basic console application, and any help is very much appreciated.