Reputation: 133
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;
}
}
}
}
Upvotes: 0
Views: 1217
Reputation: 41338
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.
Upvotes: 2
Reputation: 117330
This is caused by a static constructor. Look at static field initialization too.
The code you posted does not indicate any such construct. Look elsewhere in your project.
Upvotes: 0