看板 C_Sharp 關於我們 聯絡資訊
: 可是我現在在我的button按鈕下編寫下列程式碼 : private void button1_Click(object sender, System.EventArgs e) : { : string g1,h1; 妳將g1, h1宣告為function scope的區域變數(意思就是,只有在這個function中看的到) 這邊就是問題所在 : double ans=0; : h1 = textBox1.Text; : g1 = textBox5.Text; : CalculateGPA(); : textBox10.Text = ans.ToString(); : } : static void CalculateGPA() : { : int p=0,h=0; : p = MapToPoint(g1); : h = MapToIntHours(h1); 這邊直接叫用g1, h1自然就會看不到了 解決辦法是讓CalculateGPA這個sub設定兩個參數來傳值 or 將h1, g1宣告在函式以外的地方,這樣就成了class scope(有點類似C的全域變數) : } : 編譯的時候卻跟我說 : The name 'g1' does not exist in the class or namespace 'GPA.Form1' : The name 'h1' does not exist in the class or namespace 'GPA.Form1' : 怎麼會這樣呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.73.237.254
tomex:對,宣告為全域變數! 09/25 00:17
kingtw1978:感謝... 09/25 07:34