作者kjoe33 (123)
看板C_Sharp
標題[問題] 廣域變數定義
時間Sat May 22 12:48:04 2010
我如果把變數定在public class form1下面
這種情況下我Buttom1_click的指令會把值再重新寫到變數裡面
這樣我下次運算的時候值會亂跳
要怎樣才可以避免這種情況~請各位大大幫忙
我超新手!!剛開始學!
namespace N_
{
public partial class Form1 : Form
{
public int N , c, a = 1, sum;
public Form1()
}
private void button1_Click(object sender, EventArgs e)
}
this.textBox3.Text = ("1");
N = Convert.ToInt32(this.textBox1.Text);
while (a <= N)
{
c = Convert.ToInt32(this.textBox3.Text);
sum = a * c;
this.textBox3.Text = (sum).ToString();
a++;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.228.253.86
1F:→ james732:你希望的正確結果是什麼? 05/22 13:01
2F:→ kjoe33:我把變數定義在Buttom_click下面就沒有問題 05/22 13:10
3F:→ kjoe33:如果我把變數定義成廣域變數之後就怪怪的 05/22 13:11
4F:→ kjoe33:廣域變數跟直接定義差在哪裡??我爬文翻書都沒找到@@ 05/22 13:11
5F:→ mcdstyle:你的a會有問題,載入form按一次button後,a就可能不是1了 05/23 02:01
6F:→ mcdstyle:當你要按第二次,a就不是從1開始遞增了 05/23 02:02