PriceCheaperton
PriceCheaperton

Reputation: 5349

How to get a value from an Array via 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 TheArray from my form. I want the loop to iterate based on the times I click button5 and then I want to click my buttons programmatically based on what my array returns. On my form I have 4 buttons called, button1,button2,button3 and button4.

Once I click on button5 my code needs to click the button based on the array each time it loops through TheArray

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
        }
    
    }
}

Upvotes: 1

Views: 96

Answers (2)

davidsbro
davidsbro

Reputation: 2758

To click on the buttons based on the array (I'm assuming 0 in the array corresponds to button1, etc, you could try something like this:

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();
     }
}

As a side note, as @ThunderGr pointed out in a comment, you'd have to create an instance of your game or else make it static.

Upvotes: 2

ThunderGr
ThunderGr

Reputation: 2367

In the form declaration define Game MyGameClass=new Game();.

You code has lots of gaps, though. In order to get the last int, you need to also define a public int CurrentInt=0; in the Game Class and do a CurrentInt++; from within Game().

Now, you can do a int theInt=MyGameClass.TheArray[MyGameClass.CurrentInt]; from within the button5 event and implement a switch statement to find which button to click.

Of course, this is still inefficient. Perhaps it would be better if you declared a public int GetLastInt() in Game that would return TheArray[CurrentInt];.

Upvotes: 1

Related Questions