2017-09-19 21 views
2

到目前爲止,我的代碼執行以下操作。C#從公共類和列表中分配字符串

  1. 要求用戶提供一個數字量「玩家」
  2. 然後詢問名稱爲每個被添加到列表和類玩家

我想打電話給那些名字從列表或類(不確定類是如何工作的)並將其分配給新的字符串。這是我走到這一步:

public class NameVariable 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     bool IsUserWrong = false; 

     Console.WriteLine("Write amount of players"); 

     while (!IsUserWrong) 
     { 
      int TotalPlayers; 
      while (!Int32.TryParse(Console.ReadLine(), out TotalPlayers)) 
      { 
       Console.WriteLine("Value must be numeric."); 
      } 

      if (TotalPlayers >= 12 && TotalPlayers <= 16) 
      { 
       List<NameVariable> PlayerList = new List<NameVariable>(); 

       for (int index = 0; index < TotalPlayers; index++) 
       { 
        Console.WriteLine("Enter player {0}'s name:", index + 1); 
        PlayerList.Add(new NameVariable 
        { 
         Name = Console.ReadLine(), 
         ID = index 
        }); 
       } 

       // string player1 = ??? 
       // string player2 = ??? 
       // and so on for 12-16 players 
      } 
      else 
      { 
       Console.WriteLine("Please enter a value between 12 and 16."); 
      } 
     } 
    } 
} 

我知道foreach循環可用於顯示所有的變量在NameVariable類。只想知道如何將每個變量分配給不同的字符串。

使用類之前,我只是用它曾通過提前使用

string player1 = PlayerList[0]; 
string player2 = PlayerList[1]; 
// and so on for the remaining players 

感謝名單!

+1

你不需要。只需使用該集合。 – SLaks

+0

集合應該像*那樣替換*編號的變量;現在不需要他們。 – BradleyDotNET

回答

1

它只是

string player1 = PlayerList[0].Name; 
string player2 = PlayerList[1].Name; 
... 

本質上的列表中包含NameVariable對象。 PlayerList [index]爲您提供對象,而.Name爲您提供對象的屬性值。

如果通過特定的ID號需要特定的球員的名字,你可以使用LINQ(只是給你一個提示)

string player = PlayerList.Where(p => p.ID == WhateverIDNumber).First().Name; 
+3

儘管這是完全正確的,但應該指出的是,基於某些索引對其進行硬編碼打破了首先使用集合的目的。 – mason

1

雖然回答你的眼前問題,即如何訪問性能正如其他人所表明的,我覺得這個代碼有更大的問題。那就是你試圖在一個函數中做得太多,即Main()。所以我建議實際上試着重構你的代碼,以便一個函數完成一件事。例如:

public static int GetNumberOfPlayers() 
{ 
    Console.Write("Enter number of players: "); 
    int totalPlayers; 
    while (!Int32.TryParse(Console.ReadLine(), out totalPlayers)) 
    { 
     Console.WriteLine("Value must be numeric."); 
    } 
    return totalPlayers; 
} 

public static List<NameVariable> GetPlayerList(int num) 
{ 
    var list = new List<NameVariable>(); 
    for (int i = 0; i < num; i++) 
    { 
     Console.WriteLine("Enter player {0}'s name:", i + 1); 
     list.Add(new NameVariable 
     { 
      Name = Console.ReadLine(), 
      ID = i 
     }); 
    } 
    return list; 
} 

public static void DisplayPlayers(List<NameVariable> list) 
{ 
    foreach(var player in list) 
    { 
     Console.WriteLine("Player {0}, Name: {1}", player.ID, player.Name); 
    } 
} 

public static void CantThinkOfAGoodName() 
{ 
    while (true) 
    { 
     int totalPlayers = GetNumberOfPlayers(); 
     if (totalPlayers > 16 || totalPlayers < 12) 
     { 
      Console.WriteLine("Please enter a value between 12 and 16."); 
     } 
     else 
     { 
      var playerList = GetPlayerList(totalPlayers); 
      DisplayPlayers(playerList); 
      break; 
     } 
    } 
} 

public static void Main() 
{ 
    CantThinkOfAGoodName(); 
    Console.ReadLine(); 
} 
+0

我已經採納了您的建議,並將主要部分拆分爲單獨的部分!幫助反覆使用相同的代碼。 – Sulhan

0

不確定是否有幫助,但您可以使用索引器按名稱獲取玩家。

public NameVariable this[string name] 

比方說,你創建一個類的保藏

public class NameColection : List<NameVariable> 
{ 
    public NameVariable this[string name] 
    { 
     get 
     { 
      return this.FirstOrDefault(n => n.Name == name); 
     } 
    } 
} 

然後你的名字

var players = new NameColection() 
{ 
    new NameVariable() { ID = 1 , Name = "John" }, 
    new NameVariable() { ID = 2 , Name = "Paul" }, 
    new NameVariable() { ID = 3 , Name = "George" }, 
    new NameVariable() { ID = 4 , Name = "Ringo" } 
}; 
var player1 = players["John"]; 

List訪問球員NameColection inhertits,你就可以添加,刪除或按通常的方式修改項目。