1

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.

1

2 Answers 2

4

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 ...
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.