2011-04-06 69 views
2

我設置了多種方法,不知道如何繼續一個變量(「頂」變量)傳遞給不同的方法。獲取;組;變量

主要方法:

public static void Main(string[] args) 
    { 
     int[] anArray = new int[5]; 
     int top = -1; 
     PushPeek(anArray); 

然後我需要通過頂部:

public static void PushPeek(int[] ar) 
    { 

     if (ar[ar.Length -1] == ar.Length -1) 
     { 
      //do nothing 
     } 
     else 
     { 
      top = top + 1; 
      Console.WriteLine(ar[top]); 
     } 
    }\ 

我知道這涉及到與得到的東西;組;但我不知道如何,有什麼幫助?

回答

2

傳遞它的參考:

public static void PushPeek(int[] ar, ref int top) 
{ 
    ... 
} 

int[] anArray = new int[5]; 
int top = -1; 
PushPeek(anArray, ref top); 
+0

這最終將與推動流行音樂和偷看的東西堆類,我認爲上面的變量將始終如一地發生變化,也不會要求我使用get;? – Madox 2011-04-06 22:31:26

+0

你叫什麼?「獲得;設置;」被稱爲財產。但在你的情況下,你只需要上面是你的類的字段(即類的變量) – 2011-04-06 22:45:32

+0

對,但我也需要能夠改變它在不同的方法,然後再回到第一種方法,並使用與更新的變量相同的變量。這會工作嗎? – Madox 2011-04-06 22:50:27