This question sort of encompasses two problems (which may very well be inter-related) that I have been having in C#. I really hope this hasn't been clearly answered elsewhere, as I am unsure of what search terms to use and so far haven't come across anything that has satisfactorily explained it to me. I have a function called isHighScore()
in my C# word game. Basically what's happening is that no matter the different ways I have tried formatting this, removing the usings, the try/catch's, I seem to be unable to write the variable to the file. I have edited the code down significantly for anyone reading this for the first time in order illustrate what appears to be (for me) more of an issue with scope.
private void isHighScore() //Having problems...
{
string strHighScore;
try
{
StreamReader readHighScore = new StreamReader(strPath + "WAHS.txt")
strHighScore = readHighScore.ReadLine();
}
catch (Exception e)
{
MessageBox.Show(Convert.ToString(e.Message));
}
...
Next I would insert a similar block using StreamWriter and my comparison for userScore being greater than strHighScore doesn't seem to evaluate true. Is it because my variable goes out of scope? How can I read the changed value outside of the scope of that try-catch, or for that matter, a using statement with StreamReader (which I know is the preferred method).