How to get value from an array through a class in Winforms using C #

I have a class called Game.cs, and in the class I have the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simon
{
    class Game
    {
        public int[] TheArray = new int[1000];

        private bool m_Play;
        public bool Play
        {
            set { m_Play = value; }
            get { return m_Play; }
        }

        public Game()
        {
            Random rnd = new Random();

            for (int i = 0; i < 8; i++)
            {
                TheArray[i] = rnd.Next(0, 4); // between 0 and 3
            }

        }


    }
}

I want to be able to call TheArrayfrom my form. I want the loop to repeat depending on the time I click button5, and then I want to programmatically click on my buttons based on the returned array. In my form I have 4 buttons: button1, button2, button3and button4.

As soon as I click on button5, my code needs to click on the button based on the array, every time it goes throughTheArray

So far I have this:

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 Simon
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Game m_game;

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box2");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box3");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box4");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // Determine which button to click based on TheArray
        }

    }
}
+3
source share
2 answers

( , 0 button1 .., - :

private void button5_Click(object sender, EventArgs e)
{
     Button[] button = { button1, button2, button3, button4 };
     for (int i = 0; i < m_game.TheArray.Length; i++)
     {
         button[m_game.TheArray[i]].PerformClick();
     }
}

, @ThunderGr , .

+2

Game MyGameClass=new Game();.

. int, public int CurrentInt=0; CurrentInt++; Game().

int theInt=MyGameClass.TheArray[MyGameClass.CurrentInt]; button5 switch, , .

, . , , public int GetLastInt() , return TheArray[CurrentInt];.

+1

All Articles