I am attempting to create a variable or string from “Convert.ToChar(b[i])” within the following context:
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
E.g.:
var str = Convert.ToChar(b[i]);
But the above does not work, as “i” is not defined within the current context
Is it just a case of the for loop not being given adequate scope? Try the following…
Note that if you didn’t include the curly braces, the for loop would only have scope of the first line beneath it, and so the
var = Convert.ToChar(b[i]);line would not be able to access theivariable in the loop scope.That’s why I always make sure I put the curly braces in the code for a loop, even if it is for a single line within the loop – it is easy to track the scope of the loop that way.