2016-04-13 60 views
-3

存在我試圖爲int []傳遞到方法,用另一個陣列串聯,並將其返回到主控制檯打印。 這裏是代碼:INT []不在當前上下文中

//where preTemp is another array derived in previous method 
public static int[] sesLayer(int[] preTemp) 
{ 
    //set two arrays for rtr and r1r0 
    int[] r1r0 = new int[2] { 0, 0 }; 
    int[] RTR = new int[1] { 0 }; 

    //add r1r0 to the preTemp int array 
    //set length of the new array to accomodate temp + r1r0 
    var length = new int[preTemp.Length + r1r0.Length]; 
    r1r0.CopyTo(length, 0); 
    preTemp.CopyTo(length, length.Length); 

    //add RTR to the packet 

    return preTemp; 
} 
public static int[] preLayer(int tempData) 
{ 
      string binaryTemp = Convert.ToString(tempData, 2); 
      int DLC = binaryTemp.Length; 
      binaryTemp = binaryTemp.PadLeft(64, '0'); 

      string binaryDLC = Convert.ToString(DLC, 2); 
      binaryDLC = binaryDLC.PadLeft(4, '0'); 

      string prePacket = binaryDLC + binaryTemp; 

      //convert string to int[] 
      int[] preTemp = prePacket.Select(c =>  int.Parse(c.ToString())).ToArray(); 
      return preTemp; 
} 
static void Main(string[] args) 
{ 
    int[] sesTemp = sesLayer(preTemp); //**error crops up here** 
    Console.Write(sesTemp); 
    Console.ReadLine(); 
} 

and int tempData = 58; 任何幫助表示讚賞。

+3

你在哪裏聲明並初始化'preTemp'? –

+2

這就是問題,OP不是。 –

+0

你能多解釋一下嗎? –

回答

4

錯誤是相當清楚的。你正在調用一個不存在的變量。

int[] sesTemp = sesLayer(preTemp); 

你沒有你的Main()方法的範圍內命名preTemp變量。你只用它作爲你的方法的參數。你將不得不創建一個新的變量。

int[] preTemp = new int[] { /* your values */ }; 
int[] sesTemp = sesLayer(preTemp); 
+0

就我而言,'preLayer'返回一個長度爲64的int []數組。 因此,我認爲變量確實存在。 – tomek

+0

在方法中初始化的任何變量都包含在該方法中。在Main()方法中創建一個,並嘗試在'preLayer'中使用它。這是您可以重現錯誤的另一種方式。 –

+0

Got it!謝謝你提請:) – tomek

相關問題