作者liunate (测试)
看板C_Sharp
标题Re: [问题] 副程式里面呼叫需要传回值的副程式
时间Sat Sep 24 11:25:46 2005
: 可是我现在在我的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
1F:推 tomex:对,宣告为全域变数! 09/25 00:17
2F:推 kingtw1978:感谢... 09/25 07:34