Reputation: 421
How can I pass an array of integer from a button to another one?
Here is more information (the following code is not exactly my original code, but it explains what I'm asking):
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
}
Now I want to access array2 in button2. How can I do that? What is the best solution?
Thanks in advance.
Upvotes: 1
Views: 1174
Reputation: 62544
Looks like whilst first button click you are prepare some data and whilst second button click you are going to use it some how.
You can share an array using class-level variable:
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 ...
}
}
}
Upvotes: 4
Reputation: 5294
Just declare the array outside of the code for the button_Click events, make it private so its only accessable within the class you are in, then you can access it from any method / event handler in that class
Upvotes: 0