如何在按钮单击事件中使用我在Form()函数中定义的变量?
public Form2(string a, string b)
{
int something=4;
int something=5;
}
public void hButton_Click(object sender, EventArgs e)
{
}
我想在gButton_Click事件中使用变量something和something2。我怎么能这样做?
答案 0 :(得分:1)
class Form2 {
int something, something;
public Form2(string a, string b) {
something=4;
something=5;
}
public void hButton_Click(object sender, EventArgs e) {
// example: MessageBox.Show(something.ToString());
}
}
答案 1 :(得分:0)
你不能使用你编写的代码,因为“something”变量只存在于form2()函数的范围内。如果将它们声明为类变量,那么您将能够在按钮单击功能中访问它们。像这样:
class form2
{
int something;
public Form2(string a, string b)
{
something=4;
something=5;
}
public void hButton_Click(object sender, EventArgs e)
{
//now you can access something in here
}
}