gpt4 book ai didi

c# - Form1 的类型初始值设定项抛出异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:42 25 4
gpt4 key购买 nike

所以我对 C# 编码有点陌生,异常处理不是我的强项之一。我正在尝试编写一个刽子手式的游戏,但在尝试执行该程序时遇到了问题。我收到一条未处理的异常消息,其文本为“(文件名)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();

}

}
}

}

感谢您提供的任何帮助。

最佳答案

Static Field Initializations 的 C# 引用说

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.

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

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

但我真的很想知道为什么你需要这些静态字段。您是否需要它们在您的表单的每个其他实例中全局可用?如果不记住,对于此代码的每个 future 读者来说,这种模式真的不清楚,包括你和我

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

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;

}
}

关于c# - Form1 的类型初始值设定项抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26323094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com