简体   繁体   English

类型初始化异常

[英]Type Initialization Exception

i use a class to declare a public static variables我使用一个类来声明一个公共静态变量

 class Variables
{

   public static string AncMotP = System.IO.File.ReadAllText(@"E:\Projects\hanout\WindowsFormsApplication1\mtp.txt");


    static public  float sommeInit = (float)Convert.ToDouble(System.IO.File.ReadAllText(@"E:\Projects\hanout\WindowsFormsApplication1\hanout.txt")), sommeAjout, rest;


}

} }

When excute the program it show this error My Problem执行程序时,它显示此错误我的问题

A type initialisation exception is thrown when you cannot create the instance of the Type associated with your class.当您无法创建与您的类关联的 Type 的实例时,将引发类型初始化异常。 In your case you are performing a file read to initialise your static variable.在您的情况下,您正在执行文件读取以初始化您的静态变量。

You could move the code to a static constructor wrap it in a try/catch block and set the variable to some meaningful default.您可以将代码移动到静态构造函数中,将其包装在 try/catch 块中,并将变量设置为某个有意义的默认值。

The catch block will also allow you to delve into the exception. catch 块还允许您深入研究异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM