Form1的类型初始值设定项引发异常

本文关键字:异常 类型 Form1 | 更新日期: 2023-09-27 18:26:18

所以我对C#编码有些陌生,异常处理不是我的强项之一。我正试图编写一款刽子手风格的游戏,但在执行程序时遇到了问题。我收到一条未处理的异常消息,其文本为"(filename)Form1的类型初始值设定项引发了异常。"除了在Program.cs文件中收到消息外,我真的不知道如何找到错误的来源。我在这里找过类似的问题,但答案都是针对个别问题的。在我的情况下,我的代码如下:

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();
            }
        }
    }
}

谢谢你提供的任何帮助。

Form1的类型初始值设定项引发异常

静态字段初始化的C#引用表示

类的静态字段变量初始值设定项对应于中按文本顺序执行的分配序列它们出现在类声明中。如果静态构造函数(第10.11节)存在于类中,执行静态字段初始化程序发生在执行该静态之前构造函数。否则,静态字段初始化程序将在第一次使用静态之前与实现相关的时间该类的字段。

所以在你的代码中,你有这些静态字段

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是从currentAnswer初始化的,但此时currentAnswer仍然无效,因为它是从randomAnswers初始化的,仍然没有初始化

因此,您可以按照的方式翻转初始化顺序

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();

但我真的很想知道你为什么需要这些静态场。你是否需要它们在全球范围内适用于你表单的其他实例?如果不记住,这种模式对于这个代码的每个未来读者来说都是不清楚的,你和我都包括

根据上面的部分上下文,您的代码也可以在不使用任何静态变量的情况下编写,只使用标准的全局实例级变量。

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;
    }
}