C# 在公共静态类中使用公共静态变量时发生异常
在我创建了一个公共静态类之后,当我尝试使用其中一个静态变量时,发现了一个异常,“EM_Image.staticvariables”的类型初始值设定项引发了一个异常 为什么??我怎样才能解决它C# 在公共静态类中使用公共静态变量时发生异常,c#,.net-4.0,C#,.net 4.0,在我创建了一个公共静态类之后,当我尝试使用其中一个静态变量时,发现了一个异常,“EM_Image.staticvariables”的类型初始值设定项引发了一个异常 为什么??我怎样才能解决它 public static class StaticVariables { public static string image_source = "ahmed"; public static Bitmap b = new Bitmap(image_source);
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;
}
初始化类的代码(在字段的初始值设定项或静态构造函数中)引发了异常
您可以在
InnerException
属性中看到实际的异常,或者告诉调试器在Debug、Exceptions中抛出异常时中断异常。初始化类的代码(在字段的初始值设定项中或在静态构造函数中)抛出异常
您可以在InnerException
属性中查看实际异常,或者在调试中抛出异常时通知调试器中断,异常。编辑:
public static string image_source="ahmed" ;
public static Bitmap b=new Bitmap(image_source);
看起来您的默认image\u源代码正在创建null
位图-因此当初始化其他静态属性并尝试访问Bitmap b
时会引发异常,该位图为null:
public static Color[,] RGB_num = new Color[b.Width, b.Height];//orginal colors
您当前的设计并不能真正满足您的需要—看起来您需要一个单实例而不是一个静态属性集合。话虽如此,您可以使用null
(所有那些Color
变量,即)初始化所有变量,一旦您有了有效的输入,即image\u source
),您就必须更新/初始化所有变量。编辑:
public static string image_source="ahmed" ;
public static Bitmap b=new Bitmap(image_source);
看起来您的默认image\u源代码正在创建null
位图-因此当初始化其他静态属性并尝试访问Bitmap b
时会引发异常,该位图为null:
public static Color[,] RGB_num = new Color[b.Width, b.Height];//orginal colors
您当前的设计并不能真正满足您的需要—看起来您需要一个单实例而不是一个静态属性集合。话虽如此,您可以使用null
(所有那些Color
变量,即)初始化所有变量,一旦您有了有效的输入,即image\u source
),您就必须更新/初始化所有变量。为什么
每当我们第一次使用静态类时,该类就会被初始化。
所以当你设置静态场的时候
staticvariables.image_source
在设置字段之前,您的类“staticvariables”将被初始化,而初始化时将“image_source”设置为“ahmed”
在下一行中,位图类的构造函数将抛出“ArgumentException”,这就是为什么“staticvariables”类将停止指向自身的代码执行,并且@BrokenGlass位图b的值对于其他语句将不会为null。异常将停止代码执行
在类初始化期间,如果发生任何异常,将创建“TypeInitializationException”,并将实际异常设置为InnerException属性,在这种情况下,该属性将为ArgumentException:“参数无效”
如何解决
通过查看您的bt_Browse_单击,您似乎希望用户选择图像文件。并且可能在那个时候只需要设置静态类的其他字段
所以下面的“staticvariables”的实现应该给你一个想法。。。
请注意,我已将类名更改为Pascal case
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
}
}
关于是否使用单例模式,Jon Skeet已经给出了答案,关于单例模式实现和静态类之间的区别。但是现在你应该做一些简单的事情
希望有帮助。为什么
每当我们第一次使用静态类时,该类就会被初始化。
所以当你设置静态场的时候
staticvariables.image_source
在设置字段之前,您的类“staticvariables”将被初始化,而初始化时将“image_source”设置为“ahmed”
在下一行中,位图类的构造函数将抛出“ArgumentException”,这就是为什么“staticvariables”类将停止指向自身的代码执行,并且@BrokenGlass位图b的值对于其他语句将不会为null。异常将停止代码执行
在类初始化期间,如果发生任何异常,将创建“TypeInitializationException”,并将实际异常设置为InnerException属性,在这种情况下,该属性将为ArgumentException:“参数无效”
如何解决
通过查看您的bt_Browse_单击,您似乎希望用户选择图像文件。并且可能在那个时候只需要设置静态类的其他字段
所以下面的“staticvariables”的实现应该给你一个想法。。。
请注意,我已将类名更改为Pascal case
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
}
}
关于是否使用单例模式,Jon Skeet已经给出了答案,关于单例模式实现和静态类之间的区别。但是现在你应该做一些简单的事情
希望有帮助。我通过使用赋值更改静态变量解决了这个问题:
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; }
我确实必须在运行时初始化这些值,但我不认为这是一个问题。我通过使用赋值更改静态变量来解决这个问题:
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; }
我确实必须在运行时初始化这些值,但我不认为这是一个问题。你能发布你的代码来帮助我们识别问题吗?重新标记为C
而不是C
。你能发布你的代码来帮助我们识别问题吗?重新标记为C
,而不是C
。