我如何在这个类中访问一个枚举值?

问题描述 投票:0回答:1

我是一个相当新的程序员,试图通过使用C#中的MonoGame制作一个非常简单的游戏。我的问题是,我想访问另一个类中的一个枚举值(不确定是否是正确的术语),但我不知道如何访问。我的猜测是,你可以做一些类似: return Game1.State.EnterHighScore; 或者做一个对象引用,但对我来说没有用,可能是因为我做得不对。我希望得到帮助!

很抱歉,我不知道如何正确地格式化代码,但我试图让它尽可能清楚。

//File name: GameElements.cs
//...
    //...
        //...
            //...   
                    if (e.CheckCollision(player)) 
                    {
                        player.IsAlive = false;
                        return /*EnterHighScore*/; // I want to return the enum value EnterHighscore, 
                                                   //..which is in the class Game1
                    }
            //...
        //...
    //...
//...  
//File name: Game1.cs
//...
    public class Game1 : Game
    {
        enum State { PrintHighScore, EnterHighScore }; // I want to access EnterHighScore.
        //...
    }
//...
c# visual-studio enums state monogame
1个回答
1
投票

把你的枚举放在任何类之外。然后你可以直接从同一命名空间的任何类中的任何方法返回。

public enum GameState
{
    EnterHighScore,
    EnterSomeOtherScore,
    EnterLooserScore
};

public class SomeClass
{
    public GameState CheckGame()
    {
        return GameState.EnterHighScore;
    }
}

如果你把枚举保存在任何一个类里面,那么枚举的范围将只限于该类。

© www.soinside.com 2019 - 2024. All rights reserved.