简体   繁体   中英

C# can't reach Enum class variables

I have created the following class:

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"
            };
    }
}

Now i wish to use this in my following class:

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;
        }

    }
}

However Visual studio says its an error.

Im fairly new to C# So i hope you guys would be able to help me out ? :)

You defeated the point of an enum. It should be declared and used like this:

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

Note that it's declared as "enum" not "class".

Also note, that if you declare the enum like:

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

The value of the first name begins at 0 and autoincrements by 1 for each following name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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