2
Answers

Class Array as a property

Tobie Horswill

Tobie Horswill

16y
2.1k
1
Hi,

I have a multi user game where I'd like to be able to access player information across multiple forms. To achieve this I've created a Game class accessible to all forms in which I am exposing a public static array of another class called Player as a property. This seems to compile ok but for some reason I can't access the fields inside the array items through the property.

My code is as follows :

namespace Intro
{
  class Game
  {
   public static string NewPlayerName;
   private const int Counter = 81;
   public static Player[] pl = new Player[Counter];

   public static Player[] PlayerList
   {
     get { return pl; }
     set { pl = value;}
   }

   public class Player
   {
    string Name;
    uint TTL;
    bool Active;
    bool?[] AnswersA;
    bool?[] AnswersB;
    public Player(string _Name)
   {
     Name = _Name;
     TTL = 240;
     Active = true;
     AnswersA = new bool?[10];
     AnswersB = new bool?[10];
     for (int i = 0; i < 10; i++)
     {
       AnswersA[i] = null;
       AnswersB[i] = null;
      }
    }
  }
 }
}

and in one of my forms I'd like to do something like this :

namespace Intro
{
  public partial class Intro_Form : Form
  {
  public Intro_Form()
  {
    InitializeComponent();
  }
  private void button1_MouseDown(object sender, MouseEventArgs e)
  {
    Game.PlayerList[0].Name = "something";
  }
}

Unfortunately none of the Playerlist[x] subitems are accessible...

Any help appreciated, thanks in advance!

-- tohox
Answers (2)