字段初始值设定项不能引用非静态字段、方法或 属性 5
A field initializer cannot reference the non-static field, method, or property 5
我想定义以下数组以在我的代码中使用它们。这是我想要定义它们的方式:
namespace LIDAR_v2
{
public partial class frm_principal : Form
{
System.Windows.Forms.Label[] numLabel = { lbl_prog_M1, lbl_prog_M2, lbl_prog_M3, lbl_prog_M4, lbl_prog_M5, lbl_prog_M6, lbl_prog_M7, lbl_prog_M8, lbl_prog_M9, lbl_prog_M10, lbl_prog_M11, lbl_prog_M12 };
System.Windows.Forms.Label[] iniLabel = { label_ini_1, label_ini_2, label_ini_3, label_ini_4, label_ini_5, label_ini_6, label_ini_7, label_ini_8, label_ini_9, label_ini_10, label_ini_11, label_ini_12 };
System.Windows.Forms.Label[] finLabel = { label_fin_1, label_fin_2, label_fin_3, label_fin_4, label_fin_5, label_fin_6, label_fin_7, label_fin_8, label_fin_9, label_fin_10, label_fin_11, label_fin_12 };
System.Windows.Forms.DateTimePicker[] iniTimeLabel = { dt_prog_ini_M1, dt_prog_ini_M2, dt_prog_ini_M3, dt_prog_ini_M4, dt_prog_ini_M5, dt_prog_ini_M6, dt_prog_ini_M7, dt_prog_ini_M8, dt_prog_ini_M9, dt_prog_ini_M10, dt_prog_ini_M11, dt_prog_ini_M12 };
System.Windows.Forms.DateTimePicker[] finTimeLabel = { dt_prog_fin_M1, dt_prog_fin_M2, dt_prog_fin_M3, dt_prog_fin_M4, dt_prog_fin_M5, dt_prog_fin_M6, dt_prog_fin_M7, dt_prog_fin_M8, dt_prog_fin_M9, dt_prog_fin_M10, dt_prog_fin_M11, dt_prog_fin_M12 };
System.Windows.Forms.Label[] intLabel = { label_int_1, label_int_2, label_int_3, label_int_4, label_int_5, label_int_6, label_int_7, label_int_8, label_int_9, label_int_10, label_int_11, label_int_12 };
System.Windows.Forms.NumericUpDown[] intValue = { nm_prog_interv_M1, nm_prog_interv_M2, nm_prog_interv_M3, nm_prog_interv_M4, nm_prog_interv_M5, nm_prog_interv_M6, nm_prog_interv_M7, nm_prog_interv_M8, nm_prog_interv_M9, nm_prog_interv_M10, nm_prog_interv_M11, nm_prog_interv_M12 };
System.Windows.Forms.CheckBox[] diario = { ck_prog_diario_M1, ck_prog_diario_M2, ck_prog_diario_M3, ck_prog_diario_M4, ck_prog_diario_M5, ck_prog_diario_M6, ck_prog_diario_M7, ck_prog_diario_M8, ck_prog_diario_M9, ck_prog_diario_M10, ck_prog_diario_M11, ck_prog_diario_M12 };
}
}
但这会产生错误
"a field initializer cannot reference the non-static field"
。有什么解决办法吗?
谢谢!!
您必须在 class 的构造函数中初始化这些字段(如 numLabel
),因为它们引用了其他非静态字段。
例如:
public partial class frm_principal : Form
{
System.Windows.Forms.Label[] numLabel = null;
public frm_principal()
{
InitializeComponent();
numLabel = new System.Windows.Forms.Label[] { lbl_prog_M1, lbl_prog_M2, lbl_prog_M3, lbl_prog_M4, lbl_prog_M5, lbl_prog_M6, lbl_prog_M7, lbl_prog_M8, lbl_prog_M9, lbl_prog_M10, lbl_prog_M11, lbl_prog_M12 };
...
我想定义以下数组以在我的代码中使用它们。这是我想要定义它们的方式:
namespace LIDAR_v2
{
public partial class frm_principal : Form
{
System.Windows.Forms.Label[] numLabel = { lbl_prog_M1, lbl_prog_M2, lbl_prog_M3, lbl_prog_M4, lbl_prog_M5, lbl_prog_M6, lbl_prog_M7, lbl_prog_M8, lbl_prog_M9, lbl_prog_M10, lbl_prog_M11, lbl_prog_M12 };
System.Windows.Forms.Label[] iniLabel = { label_ini_1, label_ini_2, label_ini_3, label_ini_4, label_ini_5, label_ini_6, label_ini_7, label_ini_8, label_ini_9, label_ini_10, label_ini_11, label_ini_12 };
System.Windows.Forms.Label[] finLabel = { label_fin_1, label_fin_2, label_fin_3, label_fin_4, label_fin_5, label_fin_6, label_fin_7, label_fin_8, label_fin_9, label_fin_10, label_fin_11, label_fin_12 };
System.Windows.Forms.DateTimePicker[] iniTimeLabel = { dt_prog_ini_M1, dt_prog_ini_M2, dt_prog_ini_M3, dt_prog_ini_M4, dt_prog_ini_M5, dt_prog_ini_M6, dt_prog_ini_M7, dt_prog_ini_M8, dt_prog_ini_M9, dt_prog_ini_M10, dt_prog_ini_M11, dt_prog_ini_M12 };
System.Windows.Forms.DateTimePicker[] finTimeLabel = { dt_prog_fin_M1, dt_prog_fin_M2, dt_prog_fin_M3, dt_prog_fin_M4, dt_prog_fin_M5, dt_prog_fin_M6, dt_prog_fin_M7, dt_prog_fin_M8, dt_prog_fin_M9, dt_prog_fin_M10, dt_prog_fin_M11, dt_prog_fin_M12 };
System.Windows.Forms.Label[] intLabel = { label_int_1, label_int_2, label_int_3, label_int_4, label_int_5, label_int_6, label_int_7, label_int_8, label_int_9, label_int_10, label_int_11, label_int_12 };
System.Windows.Forms.NumericUpDown[] intValue = { nm_prog_interv_M1, nm_prog_interv_M2, nm_prog_interv_M3, nm_prog_interv_M4, nm_prog_interv_M5, nm_prog_interv_M6, nm_prog_interv_M7, nm_prog_interv_M8, nm_prog_interv_M9, nm_prog_interv_M10, nm_prog_interv_M11, nm_prog_interv_M12 };
System.Windows.Forms.CheckBox[] diario = { ck_prog_diario_M1, ck_prog_diario_M2, ck_prog_diario_M3, ck_prog_diario_M4, ck_prog_diario_M5, ck_prog_diario_M6, ck_prog_diario_M7, ck_prog_diario_M8, ck_prog_diario_M9, ck_prog_diario_M10, ck_prog_diario_M11, ck_prog_diario_M12 };
}
}
但这会产生错误
"a field initializer cannot reference the non-static field"
。有什么解决办法吗?
谢谢!!
您必须在 class 的构造函数中初始化这些字段(如 numLabel
),因为它们引用了其他非静态字段。
例如:
public partial class frm_principal : Form
{
System.Windows.Forms.Label[] numLabel = null;
public frm_principal()
{
InitializeComponent();
numLabel = new System.Windows.Forms.Label[] { lbl_prog_M1, lbl_prog_M2, lbl_prog_M3, lbl_prog_M4, lbl_prog_M5, lbl_prog_M6, lbl_prog_M7, lbl_prog_M8, lbl_prog_M9, lbl_prog_M10, lbl_prog_M11, lbl_prog_M12 };
...