Consider this while loop that keeps nulling my values once it has finished.
The counterm string is now null at the end of the program. How can we carry the value from the while loop?
void somefunction() {
try {
using (StreamReader sr = new StreamReader("Counter.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
string countern = line + "_1";
string counterm = line + "_2";
int counter = Convert.ToInt32(line);
sr.test(counterm);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
Thread.Sleep(60000);
}
}
public static void test(counterm)
{
Console.WriteLine(counterm);
Console.ReadLine();
}
countermis a variable local to your loop. Also your code does not compile because it’s incomplete but my suggestion would be something like this: