2011-03-27 118 views
0

当我尝试使用静态变量之一时发生公共静态类有一些公共静态变量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; 
     } 
+1

你可以发布你的代码? – Oded 2011-03-27 19:45:58

+0

您可以发布您的代码,以帮助我们确定问题吗? – 2011-03-27 19:46:23

+0

重新标记为“C#”而不是“C”。 – 2011-03-27 19:50:55

回答

2

编辑:

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

貌似默认image_source是创建一个null位图 - 所以,当其他静态属性初始化并尝试访问Bitmap b异常被抛出 - 这是空:

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

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

1

初始化您的类(在字段或静态构造函数的初始值设定项中)引发异常的代码。

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

0

为什么

每当我们第一次使用一个静态类,该类被初始化。 所以,当你设置静态场

staticvariables.image_source 

设置前场,你的类“staticvariables”被初始化,在初始化它集“image_source”到“艾哈迈德”。

在下一行中,Bitmap类的构造函数将抛出“ArgumentException”,这就是为什么你的“staticvariables”类会停止自己指向的代码执行,而@BrokenGlass位图b的值不会为null声明。异常会停止代码执行。

在类初始化期间,如果发生任何异常,将创建“TypeInitializationException”,并将实际的异常设置为InnerException属性,在此情况下将为ArgumentException:“参数无效”。

如何解决

通过看你的bt_Browse_Click,看来你想要用户选择图像文件。并且当时可能只需要设置静态类的其他字段。

所以下面你的“静态变量”的实现应该给你一个想法... 请注意我已经将类名更改为帕斯卡的情况。

public static class StaticVariables 
{ 
    public static string _image_source = "ahmed"; 

    public static string image_source 
    { 
     get 
     { 
      return _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模式,乔恩斯基特已经给出答案,约Singleton模式的实施和静态类的区别。然而现在你应该用简单的东西去...

希望它有帮助...