Skip to main content
 首页 » 编程设计

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

2024年02月23日11telwanggs

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

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

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 
} 

现在我想访问 button2 中的 array2。我怎样才能做到这一点?什么是最佳解决方案?

提前致谢。

请您参考如下方法:

看起来当第一次单击按钮时您正在准备一些数据,而当第二次单击按钮时您将以某种方式使用它。

您可以使用类级变量共享一个数组:

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 ... 
    } 
  } 
}