How do I access my array in another windows forms button.

Hey,

I need help building this GUI for a game I'm building. Problem is I have built an array of objects "gameArray" in button1_Click and want to be able to access it button2_Click, How is this possible?

Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Game
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();  
        }
 
        public void button1_Click(object sender, EventArgs e)
        {
            
 
            //Get values from textboxes.
            string valx = textBox1.Text;
            int height = int.Parse(valx);
            
 
            string valy = textBox2.Text;
            int width = int.Parse(valy);
            
 
            string valiter = textBox3.Text;
            int iter = int.Parse(valiter);
            
 
            string valcell = textBox4.Text;
            int cellsize = int.Parse(valcell);
       
            Cell[,] gameArray = new Cell[height, width];
            Random random = new Random();
            int original = 180;
            int pointx = original;
            int pointy = 50;
            int numDead = 0;
            int numAlive = 0;
 
 
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Cell temp = new Cell(); // creating cell object
 
                    // setting cell properties
 
                    double ran = random.Next(2);
                    if (ran < 0.5)
                    {
                        temp.currentState = false;
                    }
                    else
                    {
                        temp.currentState = true;
                    }
 
                    gameArray[i, j] = temp;
 
                   
                    
 
                }
 
            }
 
 public void button2_Click(object sender, EventArgs e)
        {
              //Need to use gameArray[i,j] here.
        }

Open in new window

LVL 1
galoobAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

williamcampbellCommented:
Move the Cell line to the position shown
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();  
        }
        
        private Cell[,] gameArray = new Cell[height, width];

Open in new window

0
williamcampbellCommented:
Sorry pressed the button too quick
public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();  
        }
        
        private Cell[,] gameArray;
 
        public void button1_Click(object sender, EventArgs e)
        {
             gameArray = new Cell[height, width];
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
galoobAuthor Commented:
Ok i added it there but I got errors saying height and width didnt exist in current context. I added them above it as below but now i get the error code  "A field initializer cannot reference the non-static field, method, or property 'GameOfLifeGUI.Form1.height'"

Any ideas?
public Form1()
        {
            InitializeComponent();  
        }
 
        int height;
        int width;
        private Cell[,] gameArray = new Cell[height, width];
        public void button2_Click(object sender, EventArgs e)
        {

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

veruthandaiCommented:
The problem is that your gameArray is out of scope. You can learn more about scope here :
http://www.blackwasp.co.uk/CSharpVariableScopes.aspx
 
You should move the gameArray declaration outside of the Button1_click event handler and keep it private - or you could declare a Property for it.
 
public Cell[,] GameArray
{ get; set; }
 
this would let you access the GameArray from anywhere , then your code would be able to use it between button passes.
0
galoobAuthor Commented:
Sorry replayed to your first comment. Yup that works perfect. Thanks very much. Im new to this now so do I just click Accept as Solution?

Thanks
0
veruthandaiCommented:
In regards to the width and height, you need to declare them.
int height; // this is 'null' right now
int width; // this is also null
private Cell[,] gameArray = new Cell[height, width];
// this is the equivalen of saying new Cell[ null, null ]; and would throw an error.
You need to initailize the size of height and width before they are used. I would put your gameArray initialization somewhere in your click event...

public Form1()
        {
            InitializeComponent();  
        }
 
        int height;
        int width;
        private Cell[,] gameArray;
         public void button1_Click(object sender, EventArgs e)
        {
               height = 0; width = 0;
               gameArray = new Cell[height, width];
        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.