我试图通过另一个类的对象访问常量类中的只读字段时遇到问题
常量类:
class iec_104_data_constants
{
public readonly byte[] STARTDTcon= {0x68, 0x04, 0x0b, 0x00, 0x00, 0x00};
public readonly byte[] STOPDTcon = {0x68, 0x04, 0x23, 0x00, 0x00, 0x00};
public readonly byte[] TESTFRcon = {0x68, 0x04, 0x83, 0x00, 0x00, 0x00};
public readonly byte[] nothing = {};
}
class iec104_class
{
iec_104_data_constants c = new iec_104_data_constants();
public static byte[] construct_SU_frames(byte[] dequeud_frame)
{
if (dequeud_frame[2] == 0x07) // STARTDTact
return c.STARTDTcon;
if (dequeud_frame[2] == 0x13) //STOPDTact
return c.STOPDTcon;
if (dequeud_frame[2] == 0x43) //STOPDTact
return c.STOPDTcon;
else
return c.Nothing;
}
错误:
错误CS0120非静态字段,方法或属性'iec104_class.c'需要对象引用
您的方法construct_SU_frames
是static
,但是您声明c
是实例成员。
声明c
为静态,它应该可以工作:
class iec104_class
{
// make it STATIC
static iec_104_data_constants c = new iec_104_data_constants();
//...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句