2015-09-04 68 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的很好和簡單的解釋。