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 7528959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:31:40+00:00 2026-05-30T04:31:40+00:00

I’ve been trying to get this program to work, but when I try to

  • 0

I’ve been trying to get this program to work, but when I try to run it, nothing happens. I attempted debugging, and it told me that I had a TypeInitializationException, so I looked online for a fix, but I wasn’t able to find anything that could help me. Here’s my code; the program is a GUI, where the computer randomly selects one of three letters (A, B, or C) ten times, and the user tries to guess which letter was selected. The high schore is saved to a file, and is read from that file and displayed.

Any help at all would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HighScore
{

    public partial class Form1 : Form
{

    const string FILENAME = @"C:\\Guess\\Data.txt";
    static FileStream outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);
    StreamWriter writer = new StreamWriter(outFile);
    static FileStream file = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(file);

    string answer;
    string input;
    int writenum;
    string num;





    public Form1()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        Random ranNumberGenerator = new Random();
        int randomNumber;
        int x;

        num = reader.ReadLine();
        writenum = Convert.ToInt32(num);
        label1.Text = "Score: " + writenum;
        label1.Visible = true;




        randomNumber = ranNumberGenerator.Next(1, 3);
        if (randomNumber == 1)
        {
            answer = "a";
        }
        if (randomNumber == 2)
        {
            answer = "b";
        }
        if (randomNumber == 3)
        {
            answer = "c";
        }
        if (textBox1.Text == "a")
        {
            input = "a";
        }
        if (textBox1.Text == "b")
        {
            input = "b";
        }
        if (textBox1.Text == "c")
        {
            input = "c";
        }

        if (answer == input)
        {
            label2.Text = "Correct! Computer guessed " + answer + " and you guessed " + input;
            label2.Visible = true;

            num = reader.ReadLine();
            writenum = Convert.ToInt32(num);
            writenum = writenum + 1;
            num = writenum.ToString();


        }


        for (x = 0; x < 10; ++x)
        {
            button1.Enabled = false;
        }
    }
}

}

  • 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-05-30T04:31:42+00:00Added an answer on May 30, 2026 at 4:31 am

    Given you are getting a TypeInitializationException, it implies the error is occurring either inside a static constructor (which this class doesn’t have), or during the initialization of static fields (which it does).

    In your code above, the two static fields are both FileStream, so the exception must be happening during initialization of these; before any of your other code runs.

    I can’t help noticing you are creating a stream for reading and a stream for writing, both pointing to the same file – is this intentional?

    The reason for the exception is that you open a file for writing – by default this won’t share with another file stream. You then open a second file stream for reading, on the same file – which is throwing an exception. If it really is your intention to read and write from/to the same file, you’ll need to use other constructor overloads that let you specify the sharing semantics:

    FileStream outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write, FileShare.Read);
    FileStream file = new FileStream(FILENAME, FileMode.Open, FileAccess.Read, FileShare.Write);
    

    Generally speaking though – I’d avoid having these as static fields. Make them instance properties, initialize them in the constructor. It’ll be far easier to do some barrier checking, and to debug when it goes wrong.

    As a note: TypeInitializationException is “special”; once you’ve had this exception once and the CLR has failed to initialize the type, you will always get it every time you try and access the type – it is a “fail once, fail always” situation.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.