2011-03-27 140 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模式的實施和靜態類的區別。然而現在你應該用簡單的東西去...

希望它有幫助...