2016-05-30 49 views
0

我想通過一個對象從另一個類有錯誤的痛苦與只讀

恆類訪問類中的常數的只讀字段時的一個問題:

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

錯誤:

Error CS0120 An object reference is required for the non-static field, method, or property 'iec104_class.c'

+1

'C'必須是靜態的。 –

+1

使提交'c'靜態:'私有靜態iec_104_data_constants c =新iec_104_data_constants();' –

+1

靜態方法不能訪問任何靜態成員。 –

回答

1

您的方法construct_SU_framesstatic,但您聲明c實例個成員。

申報c靜態和它應該工作:

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

    //... 
}