【发布时间】:2015-02-26 21:15:35
【问题描述】:
我在枚举方面遇到了一些问题,然后用 switch 语句检查它。我不确定我是否做错了什么,但我没有收到任何错误,所以嘿。
# Level.cs
enum LevelID
{
Level_One = 0,
Level_Two = 1,
}
private LevelID lvlID;
private string xml;
StreamReader xmlStream;
public Level()
{
switch (lvlID)
{
case LevelID.Level_One:
xmlStream = new StreamReader("../../assets/xml/test1.tmx");
break;
case LevelID.Level_Two:
xmlStream = new StreamReader("../../assets/xml/test2.tmx");
break;
}
xml = xmlStream.ReadToEnd();
xmlStream.Close();
}
public LevelID LvlID
{
get { return this.lvlID; }
set { this.lvlID = value; }
}
public string Xml
{
get { return this.xml; }
}
## The xml gives me lots of numbers from the different files
# game.cs
Level lvl = new Level();
lvl.LvlID = LevelID.Level_Two;
## When I then get the lvlID from Level.cs again its says it holds Level_Two,
## but the switch still gives me the numbers from test1.xml
问题是我正在检查 switch 语句中的枚举,以确定我应该将哪个 xml 文件存储在我解析为 XElement 的字符串中。
当我尝试这样做时,它总是给我来自第一个 switch 案例的 xml,即使当我检查时,我的 enum 变量为第二种情况保存了正确的 enum。即使在我的开关中没有默认值,它仍然会加载第一个案例的 xml 文件。发生了什么事,我做错了什么?
【问题讨论】: