C#无法达到Enum类变量

时间:2017-05-08 20:58:35

标签: c# namespaces

我创建了以下类:

namespace com.censureret.motions
{
    public class EnumPlayerStances {
        public const int OneHandSword = 50;

        /// <summary>
        /// Friendly name of the type
        /// </summary>
        public static string[] Names = new string[] {
            "One handed Sword"
            };
    }
}

现在我希望在以下课程中使用它:

namespace com.censureret.motions{
    public class OneHandSword_Idle : MotionControllerMotion
    {
        public override bool TestActivate()
        {
            if (!mIsStartable) { return false; }
            if (!mMotionController.IsGrounded) { return false; }

            if (mActorController.State.Stance != EnumPlayerStances.OneHandSword)

                return false;
        }

    }
}

然而Visual Studio说这是一个错误。

我是C#的新手所以我希望你们能够帮助我吗? :)

1 个答案:

答案 0 :(得分:1)

你击败了枚举点。它应该像这样声明和使用:

using System;

namespace StackOverflow_Events
{
    class Program
    {
        static void Main(string[] args)
        {
            string enumName = Enum.GetName(typeof(EnumPlayerStances), EnumPlayerStances.One_Handed_Sword).Replace("_", " ");
            int value = (int)EnumPlayerStances.One_Handed_Sword;
            var example = EnumPlayerStances.One_Handed_Sword;
            switch (example)
            {
                case EnumPlayerStances.One_Handed_Sword:
                    // do stuff
                    break;
            }
            Console.WriteLine($"Name: {enumName}, Value: {value}");
            Console.ReadKey();
        }
    }

    public enum EnumPlayerStances
    {
        One_Handed_Sword = 50
    }
}

请注意,它被声明为“enum”而不是“class”。

另请注意,如果您将枚举声明为:

public enum EnumPlayerStances
{
    No_Sword, // 0
    One_Handed_Sword, // 1
    Two_Handed_Sword // 2
}

名字的值从0开始,每个后续名称的自动增量为1。

相关问题