C # Class Array

I am trying to create a console game in which you enter as many names as you want before 24. To do this, I created an array of a class called PlayerData named PlayerDataAr[] , which contains 24 elements. It prompts the user to enter some names, and these names are assigned to each element of the array whose value is string Name and bool isAlive , but for some reason I canโ€™t access these values โ€‹โ€‹while I assign them to the player.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace hG { public class PlayerData { private static bool _isAlive; public static bool isAlive { get { return _isAlive;} set { _isAlive = value;} } private static string _Name; public static string Name { get { return _Name; } set { _Name = value; } } public PlayerData() { _isAlive = false; _Name = ""; } public static void SetisAlive(bool a) { _isAlive = a; } } class Program { static void Main(string[] args) { string EnterName = ""; //Array of Player Data PlayerData[] PlayerDataAr = new PlayerData[24]; for (int x = 0; x < 24; x++ ) { PlayerDataAr[x] = new PlayerData(); } //Set up the Console Console.Title = "Hunger Games"; Console.SetBufferSize(100, 42); Console.SetWindowSize(100, 42); Console.BackgroundColor = ConsoleColor.Yellow; Console.Clear(); Console.ForegroundColor = ConsoleColor.Magenta; Console.BackgroundColor = ConsoleColor.DarkRed; Console.WriteLine("Welcome"); Console.BackgroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Enter the names for tributes and press enter when done:"); //Loop through collecting names for(int x = 0; x < 25; x++) { Console.Write("--> "); EnterName = Console.ReadLine(); if (EnterName == "") { break; } else { //Assign Player Data PlayerDataAr[x].Name = EnterName; //Error appears here PlayerDataAr[x].isAlive = true; } } Console.Clear(); for (int x = 0; x < 24; x++) { } //Start Game while(true) { Console.ReadLine(); } } } } 

It returns:

User "hG.PlayerData.isAlive.get" cannot be accessed with reference to the instance; qualify it instead of the type name.

I do not know what he is saying about this, both for Name and isAlive . Any help would be greatly appreciated.

+6
source share
2 answers

Remove static from PlayerData :

 public class PlayerData { private bool _isAlive; public bool isAlive { get { return _isAlive;} set { _isAlive = value;} } private string _Name; public string Name { get { return _Name; } set { _Name = value; } } public PlayerData() { _isAlive = false; _Name = ""; } public void SetisAlive(bool a) { _isAlive = a; } } 

An even better design is to use automatically implemented properties:

 public class PlayerData { public bool isAlive { get; set; } public string Name { get; set; } public PlayerData() { isAlive = false; // redundant isAlive == false by default Name = ""; } // redundant: you can easily put isAlive = value; public void SetisAlive(bool value) { isAlive = value; } } 

static means that based on the class: PlayerData as a whole has the exceptional value of the isAlive property. You want a different behavior: each PlayerData instance has its own property value, so isAlive should just be an instance property (no static ).

+8
source

You get an error because isAlive is static , which means that it is not part of any instance. If you want to assign a value to the isAlive property, you need to do this by type name:

 PlayerData.isAlive = true; 

But , looking at your code, you want to really remove static and access it through the instance link:

 private bool _isAlive; public bool isAlive { get { return _isAlive;} set { _isAlive = value;} } 

Then PlayerDataAr[x].isAlive = true; will work fine.

There is a good and simple explanation of the static keyword here .

+4
source

All Articles