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
Loading
Tobie HorswillPosted Oct 13, 2008, 3:39 PM
I've declared all the fields in my Player class as public and I can now access them through my PlayerList property.
Many thanks!
--
Tobie
AlanPosted Oct 13, 2008, 1:54 PM
Although you've created a Player array with 81 elements, I can't see any code which initializes these elements to Player objects and so they're all null.
If you change:
Game.PlayerList[0].Name = "something";
to:
Game.PlayerList[0] = new Player("something");
then this will set the first element to a Player object and give it a Name field at the same time.
Also, all the fields of your Player class are private and so need to be either changed to public or exposed to outside code via public properties.