我有一个带有 List 的类,我正在另一个类方法中访问它。这很好,但是当我尝试在 do-loop 中使用 List 时,我总是得到一个“ArgumentOutOfRangeExcetpion”。我真的很困惑为什么在 do-loop 中使用 List 会导致它为空,或者至少这是我认为正在发生的事情。如果有人可以向我解释为什么会发生这种情况,那将是非常有启发性的,也许我可以弄清楚我将如何让它发挥作用。
这是我的方法:
private void ListToAttributes()
{
int count = -1;
//Finds Attribute starting Line.
do
{
count = count + 1;
} while (Player.PlayerList[count] != "Attributes;");
//Adds all Attributes to a list
List<string> TypeAndAmount = new List<string>();
do
{
TypeAndAmount.Add(Player.PlayerList[count]);
count = count + 1;
} while (Player.PlayerList[count] != ".");
//Sorts by type and adds amount to Player Class
foreach (string Line in TypeAndAmount)
{
string[] Type = Line.Split(' ');
if (Type[0] == "Mind") { Player.Mind = int.Parse(Type[1]); }
if (Type[0] == "Body") { Player.Body = int.Parse(Type[1]); }
if (Type[0] == "Soul") { Player.Soul = int.Parse(Type[1]); }
}
}