到目前为止,我的代码执行以下操作。
我想从列表或类中调用这些名称(不确定类是如何工作的)并将其分配给新字符串。这是我到目前为止所得到的:
public virtual DbSet<OptionListItem> OptionListItems { get; set; }
我知道foreach循环可用于显示NameVariable类中的所有变量。只想知道如何将每个变量分配给不同的字符串。
在使用该类之前,我只使用了使用
工作的列表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.");
}
}
}
}
提前致谢!
答案 0 :(得分:2)
它只是
string player1 = PlayerList[0].Name;
string player2 = PlayerList[1].Name;
...
基本上,您的列表包含NameVariable对象。 PlayerList [index]为您提供对象,.Name为您提供对象的属性值。
如果您想要一个特定身份证号码的特定玩家名称,您可以使用LINQ(只是为了给您一个提示)
string player = PlayerList.Where(p => p.ID == WhateverIDNumber).First().Name;
答案 1 :(得分: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();
}
答案 2 :(得分: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,您将能够以通常的方式添加,删除或修改项目。