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

贾斯汀·戴

因此,我对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();

            }

        }
    }

}

感谢您提供的任何帮助。

史蒂夫

静态字段初始化的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那时候仍然无效,因为它是从random初始化的

因此,您可以按如下所示翻转初始化顺序

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;

    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

“ [Modulename]”的类型初始值设定项引发了异常

来自分类Dev

“ System.Web.Http.GlobalConfiguration”的类型初始值设定项引发了异常

来自分类Dev

'System.ServiceModel.Diagnostics.TraceUtility'的类型初始值设定项引发了异常

来自分类Dev

'DotNetOpenAuth.Logger'的类型初始值设定项引发了异常

来自分类Dev

“ Google.Apis.Json.NewtonsoftJsonSerializer”的类型初始值设定项引发了异常

来自分类Dev

'<Module>'的类型初始值设定项引发了异常,算术运算导致溢出

来自分类Dev

'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner'的类型初始值设定项引发了异常

来自分类Dev

macOS中的dotnet核心:“'Crypto'的类型初始值设定项引发了异常”

来自分类Dev

“ Microsoft.DataTransformationServices.Project.SharedIcons”的类型初始值设定项引发了异常

来自分类Dev

“ LibSassNetProxy.SassCompilerProxy”的类型初始值设定项引发了异常

来自分类Dev

.net 4.8:“ <Module>”的类型初始值设定项引发了异常

来自分类Dev

'Npgsql.TypeMapping.GlobalTypeMapper'的类型初始值设定项引发了异常

来自分类Dev

Powershell错误:“ System.Net.ServicePointManager”的类型初始值设定项引发了异常

来自分类Dev

“'ServiceStack.Text.JsConfig'的类型初始值设定项引发了异常”

来自分类Dev

“ OSGeo.OSR.OsrPINVOKE”的类型初始值设定项引发了异常

来自分类Dev

错误“'PX.Data.PXCache' 的类型初始值设定项引发异常。” 登录 Acumatica 时

来自分类Dev

System.TypeInitializationException: 'CRM.Models.SiteSettings' 的类型初始值设定项引发异常。

来自分类Dev

“ System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发了Windows XP的异常

来自分类Dev

“ System.Data.Entity.Internal.AppConfig”的类型初始值设定项在子网站上引发异常

来自分类Dev

“ System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发了Windows XP的异常

来自分类Dev

Xamarin.Android System.TypeInitializationException:“ReactiveUI.ControlFetcherMixin”的类型初始值设定项引发异常

来自分类Dev

静态颜色类型初始值设定项扩展方法

来自分类Dev

PHP静态初始值设定项

来自分类Dev

无效的初始值设定项?

来自分类Dev

覆盖指定的初始值设定项

来自分类Dev

结构过多的初始值设定项

来自分类Dev

循环对象初始值设定项

来自分类Dev

无法使用类型为“(Int?)”的参数列表调用类型“String”的初始值设定项

来自分类Dev

在变量初始值设定项中获取空指针异常

Related 相关文章

  1. 1

    “ [Modulename]”的类型初始值设定项引发了异常

  2. 2

    “ System.Web.Http.GlobalConfiguration”的类型初始值设定项引发了异常

  3. 3

    'System.ServiceModel.Diagnostics.TraceUtility'的类型初始值设定项引发了异常

  4. 4

    'DotNetOpenAuth.Logger'的类型初始值设定项引发了异常

  5. 5

    “ Google.Apis.Json.NewtonsoftJsonSerializer”的类型初始值设定项引发了异常

  6. 6

    '<Module>'的类型初始值设定项引发了异常,算术运算导致溢出

  7. 7

    'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner'的类型初始值设定项引发了异常

  8. 8

    macOS中的dotnet核心:“'Crypto'的类型初始值设定项引发了异常”

  9. 9

    “ Microsoft.DataTransformationServices.Project.SharedIcons”的类型初始值设定项引发了异常

  10. 10

    “ LibSassNetProxy.SassCompilerProxy”的类型初始值设定项引发了异常

  11. 11

    .net 4.8:“ <Module>”的类型初始值设定项引发了异常

  12. 12

    'Npgsql.TypeMapping.GlobalTypeMapper'的类型初始值设定项引发了异常

  13. 13

    Powershell错误:“ System.Net.ServicePointManager”的类型初始值设定项引发了异常

  14. 14

    “'ServiceStack.Text.JsConfig'的类型初始值设定项引发了异常”

  15. 15

    “ OSGeo.OSR.OsrPINVOKE”的类型初始值设定项引发了异常

  16. 16

    错误“'PX.Data.PXCache' 的类型初始值设定项引发异常。” 登录 Acumatica 时

  17. 17

    System.TypeInitializationException: 'CRM.Models.SiteSettings' 的类型初始值设定项引发异常。

  18. 18

    “ System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发了Windows XP的异常

  19. 19

    “ System.Data.Entity.Internal.AppConfig”的类型初始值设定项在子网站上引发异常

  20. 20

    “ System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发了Windows XP的异常

  21. 21

    Xamarin.Android System.TypeInitializationException:“ReactiveUI.ControlFetcherMixin”的类型初始值设定项引发异常

  22. 22

    静态颜色类型初始值设定项扩展方法

  23. 23

    PHP静态初始值设定项

  24. 24

    无效的初始值设定项?

  25. 25

    覆盖指定的初始值设定项

  26. 26

    结构过多的初始值设定项

  27. 27

    循环对象初始值设定项

  28. 28

    无法使用类型为“(Int?)”的参数列表调用类型“String”的初始值设定项

  29. 29

    在变量初始值设定项中获取空指针异常

热门标签

归档