【发布时间】:2014-07-18 15:07:29
【问题描述】:
由于某种原因,我似乎无法使用 Get/Set 从类 manageWords 中更改类 checkSpell 中属性 NoMoreWords 的值。
完整的代码在下面,但重要的一点在这里。
在 checkSpell 中有一个名为 NoMoreWords 的 bool 属性。
然后我在manageWords 中创建checkSpell 对象的实例并将NoMoreWords 属性更改为true。我的监视窗口证明manageWords 中的值已更改,但一旦我回到checkSpell,NoMoreWords 仍然为假。
您能发现问题并指出正确的方向吗?谢谢。
这是checkSpell
class checkSpell
{
public bool finished = false;
public bool NoMoreWords { get; set; }
public void getWord()
{
manageWords mngit = new manageWords();
speakIt spk = new speakIt();
do
{
string word = mngit.readFile(); // Open the file and gets the word
if (NoMoreWords == true)
{
Console.WriteLine("NoMoreWords is TRUE");
break;
}
//if (word == string.Empty) break;
Console.WriteLine();
Console.WriteLine("Write the word: {0}", word.ToUpper());
//spk.sayThis("Write the word: " + word);
Console.Write(">");
char[] a = word.ToCharArray(); //Converts string into chars
getLetter(a); // METHOD
// if (finished == true) break;
System.Threading.Thread.Sleep(30);
//spk.sayThis("Correct!");
Console.WriteLine("");
} while (!finished);
Console.WriteLine("XXXX Bye Bye");
spk.sayThis("Bye Bye");
}
/************************************************************************************************************************
* Goes through the word (as a char[]) and compare with the typed letters *
************************************************************************************************************************/
public void getLetter(char[] a)
{
int b = 0;
ConsoleKeyInfo k;
ConsoleColor orig = Console.ForegroundColor;
int CL = Console.CursorLeft;
int CT = Console.CursorTop;
string capOut = "";
for (int i = b; i < a.Length; i = +b)
{
k = Console.ReadKey(true);
if (k.Key == ConsoleKey.Escape)
{
finished = true;
break;
}
if (k.KeyChar == a[i])
{
Console.ForegroundColor = orig;
capOut = k.KeyChar.ToString().ToUpper();
Console.Write(k.KeyChar.ToString().ToUpper());
b++;
CL = Console.CursorLeft; //Get the cursor position of the last correct letter
CT = Console.CursorTop;
}
else if (k.Key == ConsoleKey.Backspace)
{
if (Console.CursorLeft > CL) Console.SetCursorPosition(Console.CursorLeft - 1, CT);
ConsoleColor bg = Console.BackgroundColor;
Console.ForegroundColor = bg;
Console.Write(" ");
Console.CursorLeft = Console.CursorLeft - 1;
}
else if (k.Key == ConsoleKey.Enter)
{
// i don't want to change line
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(k.KeyChar.ToString().ToUpper());
Console.ForegroundColor = orig;
}
}
}
现在manageWords
class manageWords // Check file, open and edit it
{
public int lineNumber;
//Check if file exists
public string readFile()
{
string CurrentLine = "";
int count = 0;
checkSpell ckt = new checkSpell();
try
{
START:
count = File.ReadLines(Path.Combine(Environment.CurrentDirectory, "words.txt")).Count(); // Gets number of lines
var lines = File.ReadLines(Path.Combine(Environment.CurrentDirectory, "words.txt"));
if (lineNumber < count)
{
CurrentLine = lines.Skip(lineNumber).First();
lineNumber++;
}
else
{
ckt.NoMoreWords = true;
Console.WriteLine("");
Console.WriteLine("Do you want to play it again? Yes/No: ");
if (Console.ReadLine() == "yes")
{
lineNumber = 0;
goto START;
}
else ckt.NoMoreWords = true;
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
// Console.ReadLine();
}
return CurrentLine;
}
}
【问题讨论】:
-
您能否提供一个较小的最小重现问题,其中不包含与重现您的问题无关的大量代码?
-
你是什么意思“一旦我回到 checkSpell...” - 你在 checkSpell 的实例中和刚刚设置的实例相同吗?
-
@Servy 因为我不知道是什么导致了问题,所以我认为从代码中删除任何内容实际上可以消除可能的原因。但如果我问另一个问题,我会记住这一点。
-
@Tim 我在使用 Step Into 进行调试并观察属性值时是认真的。
-
@user3853326 这就是为什么您需要在删除多余的代码后实际运行程序,以确保它确实复制了问题,而不仅仅是猜测。实验。删除代码,运行程序,看看会发生什么。
标签: c# properties get set