在公共静态类中使用公共静态变量时的异常

时间:2011-03-27 19:44:44

标签: c# .net-4.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;
}

4 个答案:

答案 0 :(得分:2)

修改

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),您必须更新/初始化它们。< / p>

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

<强>为什么

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

staticvariables.image_source

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

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

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

如何解决

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

所以下面你的“staticvariables”的实现应该给你一个想法...... 请注意我已将班级名称更改为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模式实现和静态类之间差异的答案。但是现在你应该选择简单的事情。

希望它有所帮助。

答案 3 :(得分:0)

我通过使用赋值更改静态变量来解决它:

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; }

我确实必须在运行时初始化值,但没有看到这是一个问题。