只读错误

时间:2016-05-30 11:21:11

标签: c#

尝试通过另一个类中的对象访问常量类中的只读字段时遇到问题

常数类:

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'

1 个答案:

答案 0 :(得分:1)

您的方法construct_SU_framesstatic,但您声明c实例成员。

c声明为静态,它应该有效:

class iec104_class
{
    // make it STATIC
    static iec_104_data_constants c = new iec_104_data_constants();

    //...
}