【问题标题】:exception when using public static variables in public static class在公共静态类中使用公共静态变量时出现异常
【发布时间】:2011-03-27 19:44:43
【问题描述】:

在我创建公共静态类后,当我尝试使用其中一个静态变量时,VS 出现了异常“EM_Image.staticvariables”的类型初始化程序引发了异常。

为什么?以及如何解决?

public static class StaticVariables
{
    public static string image_source = "ahmed";
    public static Bitmap b            = new Bitmap(image_source);
    public static int K_numcolors     = 0;
    public static int M_leastbits     = 0;
    public static BitmapImage bi      = null;

    public static Color[,] RGB_num         = new Color[b.Width, b.Height];     // orginal colors
    public static Color[,] new_RGB_byte    = new Color[b.Width, b.Height];     // colors after compression 1
    public static string[,,] RGB_Bits      = new string[b.Width, b.Height, 3]; // original images
    public static string[,,] new1_RGB_Bits = new string[b.Width, b.Height, 3]; // after compression 1
}
private void bt_Browse_Click(object sender, System.Windows.RoutedEventArgs e)
{
    browse.ShowDialog();
    direction_text.Text = browse.FileName;
    staticvariables.image_source = browse.FileName;
    ImageSource imageSource = new BitmapImage(new Uri(browse.FileName));
    pic_origin.Source = imageSource;
}

【问题讨论】:

  • 您能否发布您的代码,以帮助我们找出问题?
  • 重新标记为 C# 而不是 C

标签: c# .net-4.0


【解决方案1】:

编辑:

public static string image_source="ahmed" ;
public static Bitmap b=new Bitmap(image_source);

看起来您的默认 image_source 正在创建一个 null 位图 - 因此在初始化其他静态属性并尝试访问 Bitmap b 时引发异常 - 这是 null:

public static Color[,] RGB_num = new Color[b.Width, b.Height];//orginal colors

您当前的设计并不能真正满足您的需求 - 看起来您需要一个单例实例,而不是一组静态属性。话虽如此,您只需使用null 初始化所有变量(即所有Color 变量),一旦您获得有效输入,即image_source),您必须全部更新/初始化它们。

【讨论】:

    【解决方案2】:

    初始化您的类的代码(在字段的初始化程序中或在静态构造函数中)引发了异常。

    您可以在 InnerException 属性中查看实际异常,或者通过在 Debug, Exceptions 中告诉调试器在抛出异常时中断。

    【讨论】:

      【解决方案3】:

      我通过使用分配更改我的静态变量来解决它:

      public static string image_source = "ahmed" ;
      public static Bitmap b=new Bitmap(image_source);
      

      使用属性:

      public static string image_source { get; set; }
      public static Bitmap b=new Bitmap { get; set; }
      

      我确实必须在运行时初始化这些值,但不认为这是一个问题。

      【讨论】:

        【解决方案4】:

        为什么

        每当我们第一次使用静态类时,该类都会被初始化。 所以当你设置静态字段时

        staticvariables.image_source
        

        在设置字段之前,您的类“staticvariables”会被初始化,而在初始化它时会将“image_source”设置为“ahmed”。

        在下一行中,Bitmap 类的构造函数将抛出“ArgumentException”,这就是为什么您的“staticvariables”类将停止指向自身的代码执行,并且@BrokenGlass Bitmap b 的值对于其他语句不会为空.该异常将停止代码执行。

        在类初始化期间,如果发生任何异常,将创建“TypeInitializationException”并将实际异常设置为 InnerException 属性,在这种情况下将为 ArgumentException: “Parameter is not valid”。

        如何解决

        通过查看您的 bt_Browse_Click,您似乎希望用户选择图像文件。并且可能那时只需要设置静态类的其他字段。

        所以下面你的“静态变量”的实现应该给你一个想法...... 请注意,我已将类名更改为 Pascal 大小写。

        public static class StaticVariables
        {
            public static string _image_source = "ahmed";
        
            public static string image_source 
            {
                get => _image_source;
                set 
                {
                    if (!File.Exists(value))
                    {
                        throw new FileNotFoundException();
                    }
                    _image_source = value;
                    SetImageData();
                }
            }
        
            public static Bitmap b = null;
            public static int K_numcolors = 0;
            public static int M_leastbits = 0;
            public static BitmapImage bi = null;
            public static Color[,] RGB_num = null;//orginal colors
            public static Color[,] new_RGB_byte = null;// colors after compression 1
            public static string[, ,] RGB_Bits = null;//original images
            public static string[, ,] new1_RGB_Bits = null;//after compression 1
        
            private static void SetImageData()
            {
                b = new Bitmap(_image_source);
                RGB_num = new Color[b.Width, b.Height];//orginal colors
                new_RGB_byte = new Color[b.Width, b.Height];// colors after compression 1
                RGB_Bits = new string[b.Width, b.Height, 3];//original images
                new1_RGB_Bits = new string[b.Width, b.Height, 3];//after compression 1
            }
        }
        

        关于是否使用Singleton模式,Jon Skeet已经给出了答案,关于Singleton模式实现和静态类的区别。但是现在你应该做一些简单的事情。

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 2016-10-25
          • 2010-10-19
          • 2011-09-23
          • 1970-01-01
          • 2014-05-24
          • 2015-03-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多