Roland
Roland

Reputation: 18415

Enum with parameters

Is it possible in C# to collect information in an enum instead of a dedicated class?

Example how it would work in Java:

  public enum Action {

    JUMP( "JUMP", 1),
    CROUCH ("CROUCH", 2),
    ;

    private String animationId;
    private int buttonId;

    private Action( String animationId, int buttonId) {
      this.animationId = animationId;
      this.buttonId = buttonId;
    }

    public String getAnimationId() {
      return animationId;
    }

    public int getButtonId() {
      return buttonId;
    }
  }

Upvotes: 1

Views: 2029

Answers (3)

vasily.sib
vasily.sib

Reputation: 4002

No, but you can use static class fields instead:

public sealed class Action
{
    public static readonly Action JUMP = new Action("JUMP", 1);
    public static readonly Action CROUCH = new Action("CROUCH", 2);

    public string AnimationId { get; }
    public int ButtonId { get; }

    private Action(String animationId, int buttonId)
    {
        AnimationId = animationId;
        ButtonId = buttonId;
    }

    public override string ToString() => AnimationId;
}

Upvotes: 2

bluray
bluray

Reputation: 1963

You can use enum with attributes:

public enum Action{
  [MyValue("JUMP", 1)]
  JUMP,

  [MyValue("CROUCH", 2)]
  CROUCH
}

[AttributeUsage(
   AttributeTargets.Field |
   AttributeTargets.Method |
   AttributeTargets.Property,
   AllowMultiple = true)]
public class MyValueAttribute : System.Attribute{
  public string Value{get; private set}
  public string AnimationId{get; private set;}
  public MyValueAttribute(string animationId, string value){
     AnimationId = animationId;
     Value = value;
}

and you can get value as follows:

public static class EnumExtensions{
        public static string GetValue(this Enum value)
        {
            var type = value.GetType();
            var name = Enum.GetName(type, value);
            if (name == null) return string.Empty;
            var field = type.GetField(name);
            if (field == null) return string.Empty;
            var attr = Attribute.GetCustomAttribute(field, typeof(MyValueAttribute)) as MyValueAttribute;
            return attr != null ? attr.Value: string.Empty;
        }

        public static string GetAnimationId(this Enum value)
        {
            var type = value.GetType();
            var name = Enum.GetName(type, value);
            if (name == null) return string.Empty;
            var field = type.GetField(name);
            if (field == null) return string.Empty;
            var attr = Attribute.GetCustomAttribute(field, typeof(MyValueAttribute)) as MyValueAttribute;
            return attr != null ? attr.AnimationId: string.Empty;
        }
}

Usage:

Action.JUMP.GetValue();
Action.JUMP.GetAnimationId();

or you can use one method which return for example Tuple with AnimationId and Value

Upvotes: 4

Andy Riedlinger
Andy Riedlinger

Reputation: 311

You could definitely use attributes like suggested. However, you can call .ToString() on an enum value to get its name as a string value, and you can also assign int values to them. By default they are assigned 0-X based on index. However you could do this

public enum Action {
JUMP=1, 
CROUCH=2
}

And then to access these values

Action action = Action.JUMP;
int value = (int) action; //Is set to 1
string name = action.ToString(); //Is set to "JUMP"

While this certainly will not work in every case depending on how much you want your enum to store, for the situation you described this is much easier.

Upvotes: 0

Related Questions