如何获取参数或调用在其他大括号中声明的函数?

时间:2014-10-30 18:31:43

标签: c# invoke

我是C#的初学者,很难解决我们的一些问题。所以我希望我的术语无所谓。这是我的问题。假设我有以下代码:

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //code starts
            //...
            //if(...) {
            //...
            //string parameter = abc.ToString();
            //}
            //code ends

        }//Form1 ends

        private void button1_Click(object sender, EventArgs e)
        {
            //code here
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = parameter;
            button1.Perform(); 
        }
    }
}

我在这里遇到困难。

如何在Form1内使用名为parameter的{​​{1}}中声明的字符串? button2_Click不起作用。

1 个答案:

答案 0 :(得分:2)

使用成员变量。

public partial class Form1 : Form
{
  private string parameter = null;

  public Form1()
  {
    InitializeComponent();

    // ...
    parameter = abc.ToString();
  }