【发布时间】:2011-07-14 02:40:02
【问题描述】:
考虑一下这个 while 循环,一旦它完成,它就会一直将我的值归零。
counterm 字符串现在在程序结束时为空。我们如何从 while 循环中携带值?
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();
}
【问题讨论】:
-
是的,如果 counterm 是在 while 循环之外定义的,那么你不应该在它的前面有“string”。 (我认为其他人在下面提到了这一点,只是把它放在首位......)
-
我不明白
sr.test();是什么。你的类是sr以及 StreamReader 实例吗? -
嗨,您可以忽略 sr.test 我正在尝试其他方法 iv 删除了起始字符串替换了所有提及但仍然无法正常工作的所有代码。
标签: c# while-loop