Sto cercando di creare un gioco per console in cui inserisci tutti i nomi che desideri fino a 24. Per fare questo ho creato un array di una classe chiamata PlayerData
denominata PlayerDataAr[]
che ha 24 elementi. Richiede all'utente di immettere alcuni nomi e tali nomi vengono assegnati a ciascun elemento dell'array che ha un valore di string Name
e bool isAlive
ma per qualche motivo non riesco a poter accedere a questi valori mentre li assegno al lettore .C# Array of Classes
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();
}
}
}
}
Restituisce:
Stati 'hG.PlayerData.isAlive.get' non è possibile accedere con un riferimento istanza; qualificarlo invece con un nome tipo.
Non so cosa si sta parlando sembra sia Name
e isAlive
. Qualsiasi aiuto sarebbe molto apprezzato.
Hai provato 'get {return PlayerData._isAlive;}'? – LInsoDeTeh
Avresti potuto scoprire il problema semplicemente osservando cosa significa la parola chiave static, che stai usando nel tuo codice. – nv3