C# 将数组从一个按钮传递到另一个按钮

C# 将数组从一个按钮传递到另一个按钮,c#,C#,如何将整数数组从一个按钮传递到另一个按钮 以下是更多信息以下代码并不完全是我的原始代码,但它解释了我的问题: private void button1_Click(object sender, EventArgs e) { int[,] array1 = new int[pictureBox1.Height, pictureBox1.Width]; int[,] array2 = new int[pictureBox1.Height, pictureBox1.Width];

如何将整数数组从一个按钮传递到另一个按钮

以下是更多信息以下代码并不完全是我的原始代码,但它解释了我的问题:

private void button1_Click(object sender, EventArgs e)
{
    int[,] array1 = new int[pictureBox1.Height, pictureBox1.Width];
    int[,] array2 = new int[pictureBox1.Height, pictureBox1.Width];

    array2 = binary(array1);//binary is a function
}

private void button2_Click(object sender, EventArgs e)
{
   //I need array2 here
}
现在我想访问按钮2中的array2。我该怎么做?最好的解决方案是什么


提前感谢。

只需在button\u Click事件的代码之外声明数组,将其设置为私有,使其仅可在您所在的类中访问,然后您就可以从该类中的任何方法/事件处理程序中访问它

只需在button\u Click事件的代码之外声明数组,将其设置为私有,这样它只能在您所在的类中访问,然后您可以从该类中的任何方法/事件处理程序访问它

第一次单击按钮时,您正在准备一些数据,第二次单击按钮时,您将如何使用它

可以使用类级别变量共享数组:

class YourClass
{
  private int[,] data;

  private void button1_Click(object sender, EventArgs e) 
  {
    this.data = new ...
  }

  private void button2_Click(object sender, EventArgs e)
  {
    // process a data
    if (this.data != null)
    {
       this.data ...
    }
  }
}

看起来,当第一次点击按钮时,您准备了一些数据,而当第二次点击按钮时,您将如何使用它

可以使用类级别变量共享数组:

class YourClass
{
  private int[,] data;

  private void button1_Click(object sender, EventArgs e) 
  {
    this.data = new ...
  }

  private void button2_Click(object sender, EventArgs e)
  {
    // process a data
    if (this.data != null)
    {
       this.data ...
    }
  }
}
看一看