Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:34:33+00:00 2026-06-14T11:34:33+00:00

This question sort of encompasses two problems (which may very well be inter-related) that

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:34:35+00:00Added an answer on June 14, 2026 at 11:34 am

    I’d strongly urge you to get rid of the manual calls to Close entirely. Use using statements instead. In fact, you don’t need the StreamReader at all, if you use convenience methods provided by File:

    string fileName = Path.Combine(path, "WAHS.txt");
    string highScoreLine = File.ReadLines(fileName).First();
    
    string beforeSpace = highScoreLine.Split(" ")[0];
    int highScore = int.Parse(beforeSpace);
    
    int userScore = int.Parse(txtScore.Text);
    if (userScore > highScore)
    {
        MessageBox.Show("Congratulations! You set a new high score!", "High Score");
        File.WriteAllText(fileName, userScore.ToString());
    }
    

    Any exceptions thrown by the above code should probably be caught and handled elsewhere, higher up the stack.

    (Note that your current code would just write the existing high score back to the file, not the new one.)

    If you’re confident the file would only contain that line of text, you could just use File.ReadAllText instead.

    Addressing the fundamental issue behind your confusion is possibly more important though. Do you understand why you can’t access readHighScore in the place you’re trying to close it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

related (sort of) to this question. I have written a script that will loop
I know that this sort of question has been asked here before, but still
This question is probably for MySQL experts and admins that have done this sort
I have found this question in the one site which was about counting sort
ok ... sort of have been dithering on posting this question but here goes:
This question is sort of a sequel to that question. When we want to
This is sort of a continuation of this question . I have a file
This question expands upon the one at abstract-class-numberformat-very-confused-about-getinstance . I feel that this question
There are a handful of questions that sort of skirt around this question, but
This question is related to this other one . In my program (which uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.