So I am somewhat new to coding C# and exception handling is not one of my strengths. I am trying to write a hangman-style game, but am running into an issue when trying to execute the program. I receive an unhandled exception message with the text "The type initializer for (filename)Form1 threw an exception." I'm not really sure how to find where the error is originating from, other than I receive the message in my Program.cs file. I have looked around on here for similar problems, but the answers are very specific to the individual problem. In my case, my code is as follows:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//set label to blank
answerLabel.Text = "";
//loop through each element in the array
for (int i = 0; i < guessThis.Length; i++)
{
//get each element as a question mark
string unknown = "?";
//add each element as a "?" to the label
answerLabel.Text += unknown;
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------*/
//initialize array from currentAnswer string
public static char[] guessThis = currentAnswer.ToCharArray();
//create array of strings for answers
public static string[] randomAnswers = new string[10]{"beach", "sword", "sushi", "rat", "joy", "summer", "animal", "baseball", "toyota", "red"};
//set up random
public static Random rand1 = new Random();
//pick a random word from the answers array
public static string currentAnswer = randomAnswers[rand1.Next(0, randomAnswers.Length)];
/*----------------------------------------------------------------------------------------------------------------------------------------------*/
//guess button
private void button1_Click(object sender, EventArgs e)
{
//set a bool for if the user input contains only letters
bool containsLetter = textBox1.Text.Any(x => char.IsLetter(x));
//checks if textbox length is not 1 character
if (textBox1.Text.Length != 1)
{
//display error
MessageBox.Show("Please enter one letter", "Error");
}
//if user input is not a letter
else if (containsLetter != true)
{
//display error
MessageBox.Show("Please enter only letters", "Error");
}
//if all conditions satisfied
else
{
//check if char array contains the user input
if (guessThis.Contains(Convert.ToChar(textBox1.Text)))
{
//get index of any element that contains the userinput
var getIndex = Array.FindIndex(guessThis, row => row.Equals(textBox1.Text));
//set up another array with the values from the label
char[] unknownAnswer = answerLabel.Text.ToCharArray();
//insert user input into the proper index of the char array
unknownAnswer[getIndex] = Convert.ToChar(textBox1.Text);
//update the label
answerLabel.Text = unknownAnswer.ToString();
}
}
}
}
Thanks for any help offered.
The C# references of Static Field Initializations says
The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (Section 10.11) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.
So in your code you have these statics fields
public static char[] guessThis = currentAnswer.ToCharArray();
public static string[] randomAnswers = new string[10]{"beach", "sword", "sushi", "rat", "joy", "summer", "animal", "baseball", "toyota", "red"};
public static Random rand1 = new Random();
public static string currentAnswer = randomAnswers[rand1.Next(0, randomAnswers.Length)];
guessThis
is intialized from currentAnswer
, but at that point, currentAnswer
is still not valid because it is initialized from randomAnswers still not initialized
So you could just flip the order of initialization as follow
public static Random rand1 = new Random();
public static string[] randomAnswers = new string[10]{"beach", "sword", "sushi", "rat", "joy", "summer", "animal", "baseball", "toyota", "red"};
public static string currentAnswer = randomAnswers[rand1.Next(0, randomAnswers.Length)];
public static char[] guessThis = currentAnswer.ToCharArray();
But I am really wondering why you need these static fields. Do you need them globally available for every other instance of your form? If not keep in mind that this kind of pattern is really unclear for every future reader of this code, you and me included
From the partial context above, your code could also written without using any static variable, just standard global instance level variables.
private Random rand1 = new Random();
private string[] randomAnswers = new string[10]{"beach", "sword", "sushi", "rat", "joy", "summer", "animal", "baseball", "toyota", "red"};
private char[] guessThis;
private string currentAnswer;
public Form1()
{
InitializeComponent();
currentAnswer = randomAnswers[rand1.Next(0, randomAnswers.Length)];
guessThis = currentAnswer.ToCharArray();
//set label to blank
answerLabel.Text = "";
//loop through each element in the array
for (int i = 0; i < guessThis.Length; i++)
{
//get each element as a question mark
string unknown = "?";
//add each element as a "?" to the label
answerLabel.Text += unknown;
}
}