Re: [問題] 副程式裡面呼叫需要傳回值的副程式

看板C_Sharp (C#)作者 (測試)時間20年前 (2005/09/24 11:23), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串4/4 (看更多)
: 可是我現在在我的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

09/25 00:17, , 1F
對,宣告為全域變數!
09/25 00:17, 1F

09/25 07:34, , 2F
感謝...
09/25 07:34, 2F
文章代碼(AID): #13DCQqoP (C_Sharp)
文章代碼(AID): #13DCQqoP (C_Sharp)