简体   繁体   English

如何访问此 class 中的枚举值?

[英]How do I access an enum value in this class?

I am a fairly new programmer trying to make a pretty simple game by using MonoGame in C#.我是一个相当新的程序员,试图通过在 C# 中使用 MonoGame 来制作一个非常简单的游戏。 My problem is that I want to access an enum value (not sure if that's the right term) in this other class, but I don't know how.我的问题是我想在另一个 class 中访问一个枚举值(不确定这是否正确),但我不知道如何。 My guesses are that you could do something like: return Game1.State.EnterHighScore;我的猜测是您可以执行以下操作: return Game1.State.EnterHighScore; or by making an object reference, but it has not worked for me, probably because I'm doing it incorrectly.或通过制作 object 参考,但它对我不起作用,可能是因为我做错了。 I would appreciate help!我会很感激帮助!

I'm sorry I don't know how to format the code properly, but I tried to make it as clear as possible:对不起,我不知道如何正确格式化代码,但我尽量让它尽可能清晰:

//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.
        //...
    }
//...

Keep your enum outside of any class.将您的枚举放在任何 class 之外。 Then you can directly return from any method in any class in the same namespace.然后,您可以直接从同一命名空间中的任何 class 中的任何方法返回。 eg:例如:

public enum GameState
{
    EnterHighScore,
    EnterSomeOtherScore,
    EnterLooserScore
};

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

If you keep the enum inside any class, then the scope of enum will be only to that class.如果您将枚举保留在任何 class 中,则枚举的 scope 将仅用于该 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM