Passing arrays to a method inside event C#
<p>Im trying to create a section of a program, where a user presses a button and a image is placed in one of 9 pictureboxes. Each time the button is clicked, a different picturebox should be selected. However, before i get to that, i'm having trouble getting my method to see the arrays i am trying to pass it.</p>

<p>I have 2 arrays, Slots and SlotsUsed and i am trying to initalise them when the program starts. However, when i try to pass them to the method "randomBox" which is called within "Button1" visual studio says they do not exist. How can i make these arrays visible throughout my code?</p>

<p>Many thanks
Anthony</p>

<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pin_program
{
    public partial class Mainscreen : Form
    {
        //Sets where users files are to be stored (for later use)
        string activeDir = @"C:\Users\Tony\Downloads\Programs\pin program\Users";

        public Mainscreen()
        {
            InitializeComponent();
        }

        //method to generate random number
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public void randomBox(int pictureVal, PictureBox[] Slots, bool[] SlotsUsed)
        {
            //generate random number
            int j = RandomNumber(0, 9);

            if (SlotsUsed[j] == false)
            {
                // Create image, assign it and set slots value to used
                Image newImage = Image.FromFile(@"C:\Users\Tony\Downloads\Programs\pin program\pin program\pin program\Images\" + pictureVal + ".jpg");
                Slots[j].Image = newImage;
                SlotsUsed[j] = true;
            }
            else
                do
                {
                    j = RandomNumber(0, 9);
                } while (SlotsUsed[j] == false);

            return;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //for use later
            string userName = textBox1.Text;
            //for use later
            label1.Visible = true;

            //test call of method.
            randomBox(1, Slots, SlotsUsed);
        }

        public void Mainscreen_Load(object sender, EventArgs e)
        {
            //array for slots
            PictureBox[] Slots = new PictureBox[9];
            Slots[0] = pictureBox1;
            Slots[1] = pictureBox2;
            Slots[2] = pictureBox3;
            Slots[3] = pictureBox4;
            Slots[4] = pictureBox5;
            Slots[5] = pictureBox6;
            Slots[6] = pictureBox7;
            Slots[7] = pictureBox8;
            Slots[8] = pictureBox9;

            //array for used slots
            bool[] SlotsUsed = new bool[9];
            for (int i = 0; i != (SlotsUsed.Length); i++)
            {
                SlotsUsed[i] = false;
            }
        }
    }
}
</code></pre>

<p>EDIT:
I dont seem to be able to post comments for some reason, so i'll just ask here. How would i declare my arrays as instance variables instead of local?  Does instance variable have another name i might know it by?</p>

<p>CHeers</p>
