C# Visual Studio 2015中实现枚举时出现的随机非相关错误
正如标题所示,当我注释出与枚举相关的内容时,会出现随机错误。我评论了错误出现的地方以及vs告诉我的内容C# Visual Studio 2015中实现枚举时出现的随机非相关错误,c#,enums,visual-studio-2015,C#,Enums,Visual Studio 2015,正如标题所示,当我注释出与枚举相关的内容时,会出现随机错误。我评论了错误出现的地方以及vs告诉我的内容 namespace Overbox { class UpdateHandler { public enum GameState { MainMenu, Options, Playing, GameOver, Exiting }; public UpdateHandler() //Below bracket tells me "}
namespace Overbox
{
class UpdateHandler
{
public enum GameState { MainMenu, Options, Playing, GameOver, Exiting };
public UpdateHandler()
//Below bracket tells me "} expected" (ive counted and unless i cant do basic math there is a correct amount of brackets)
{
public GameState currentGameState = GameState.MainMenu;
}
//Update tells me "A namespace cannot directly contain members such as fields or methods" even though it actually is (even if the formatting is messing up a bit)
public void Update(GameTime gameTime)
{
}
}
}
最后一个括号告诉我“需要类型或命名空间定义,或文件结尾”您试图从构造函数中声明字段:
public UpdateHandler()
//Below bracket tells me "} expected" (ive counted and unless i cant do basic math there is a correct amount of brackets)
{
public GameState currentGameState = GameState.MainMenu;
}
public GameState currentGameState = GameState.MainMenu;
public UpdateHandler()
{
}
C#不允许这样做,您必须将声明带到外面:
public GameState currentGameState;
public UpdateHandler()
{
currentGameState = GameState.MainMenu;
}
另一个选项是在构造函数之外设置字段的值:
public UpdateHandler()
//Below bracket tells me "} expected" (ive counted and unless i cant do basic math there is a correct amount of brackets)
{
public GameState currentGameState = GameState.MainMenu;
}
public GameState currentGameState = GameState.MainMenu;
public UpdateHandler()
{
}
在我看来,这似乎是您试图实现的目标,但定位有点错误。您的代码应该是这样的
namespace Overbox
{
class UpdateHandler
{
public enum GameState
{
MainMenu,
Options,
Playing,
GameOver,
Exiting
};
public GameState currentGameState;
public UpdateHandler()
{
this.currentGameState = GameState.MainMenu;
}
public void Update(GameTime gameTime)
{
}
}
}