2015-09-04 80 views
6

我试图制作一个控制台游戏,您可以输入多达24个名称。为此,我创建了一个名为PlayerData的类的数组,名称为PlayerDataAr[],其中包含24个元素。它会提示用户输入一些名称,并将这些名称分配给阵列中的每个元素,其值为string Namebool isAlive,但由于某种原因,我似乎无法在访问这些值时将其分配给播放器。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(); 
      }  
     } 
    } 

} 

它返回:

会员 'hG.PlayerData.isAlive.get' 不能与实例引用来访问;相反,使用类型名称来限定它。

我不知道它是什么谈论它,这样对NameisAlive。 任何帮助将不胜感激。

+0

你试过'get {return PlayerData._isAlive;}'? – LInsoDeTeh

+1

您可以通过简单地查找静态关键字的含义来了解您在代码中使用的问题。 – nv3

回答

8

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

一个更好的设计是使用自动实现的属性

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意味着类为主PlayerData一个整个有一个单数isAlive物业价值。你想不同行为:每个PlayerData实例都有自己的属性值,这就是为什么isAlive应该只是一个实例属性(无static)。

+0

谢谢,我想这很简单,我会让这个答案,当我被允许 –

+1

我会upvote这个答案,如果它不仅提供了鱼,但也教OP如何钓鱼(**编辑** - 对不起,这可能太抽象了,如果它不仅告诉OP他们需要删除'static',还要*为什么*以便将来的读者了解静态是什么,以及为什么它在这种情况下是不正确的) –

4

你会得到错误,因为isAlivestatic,这意味着它不是任何实例的一部分。如果你想要一个值分配给您isAlive属性需要通过类型名称要做到这一点:

PlayerData.isAlive = true; 

看你的代码,你真的做什么是删除static和访问它通过实例参考:

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

然后PlayerDataAr[x].isAlive = true;将正常工作。

有关于static关键字here的很好和简单的解释。