Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store Multiple Values in an Enum

I have an enum of different States of an user in a Db,

public enum UserState {
    ACTIVE = 0,
    INACTIVE = 1,
    MEMORIAL = 2,
    APPLICATION = 3,
}

I need to assign multiple value to each of these states.

I need to assign a URL for redirection, and also a string with the name of the state.

How can I store those 2 values in my enum and get them without having to declare multiple classes or using configuration files?

I tried to use [Description] Attribute to store one

public enum UserState {
    [Description("Active user")]
    ACTIVE = 0,
    [Description("Inactive user")]
    INACTIVE = 1,
    [Description("Dead user")]
    MEMORIAL = 2,
    [Description("Application")]
    APPLICATION = 3,
}

And with this I was able to retrieve the name of the state using

public class UserStateExtension{

    public static string GetStateName(UserState actualState)
    {
        DescriptionAttribute[] descriptionAttributes = (DescriptionAttribute[])actualState
            .GetType()
            .GetField(actualState.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false);
            return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : string.Empty;
    }
}

Is there a method to use a similar trick for the UrlRedirection?

like image 891
Kaiwinta Avatar asked Jul 12 '26 18:07

Kaiwinta


2 Answers

Maybe an enum is not the best data type for you. If you have to store several information, a class might be better. In order to get the enum-like behaviour of just having a few possible values, you can make your constructor private and have some public static instances:

public class UserState
{
   public string Id {get; private set; }

   public int Value {get; private set; }

   public string Description {get; private set; }

   public string Url {get; private set; }

   private UserState() { }

   public static UserState Active { get; }
         = new UserState { Id = "ACTIVE", Value = 0, Description = "Active User", Url = "http://www.active.com/" };

   public static UserState Inactive{ get; }
      = new UserState { Id = "INACTIVE", Value = 1, Description = "Inactive User", Url = "http://www.inactive.com/" };

     // and so on for the other states

     //the following can be very nice when looping over all possible states
     // or if you want to find a state with a certain Value
     public static IEnumerable<UserState> PossibleStates
     {
        get
        {
          yield return Active;
          yield return Inactive;
          // and so on for the other states
        }
      }
}

Usage is

UserState myUserState = UserState.Active;
Console.WriteLine(myUserState.Description + " has the url " + myUserState.Url);
like image 148
SomeBody Avatar answered Jul 15 '26 08:07

SomeBody


Would using extension methods be suitable? For example:

public static class UserStateExt
{
    public static string Description(this UserState userState)
    {
        return userState switch
        {
            UserState.ACTIVE      => "Active User",
            UserState.INACTIVE    => "Inactive User",
            UserState.MEMORIAL    => "Dead User",
            UserState.APPLICATION => "Application",

            _ => throw new ArgumentOutOfRangeException(nameof(userState), userState, null)
        };
    }

    public static string UriRedirection(this UserState userState)
    {
        return userState switch
        {
            UserState.ACTIVE      => "active redir",
            UserState.INACTIVE    => "inactive redir",
            UserState.MEMORIAL    => "memorial redir",
            UserState.APPLICATION => "app redir",

            _ => throw new ArgumentOutOfRangeException(nameof(userState), userState, null)
        };
    }
}

Then you can write code like

UserState state = UserState.INACTIVE; 
Console.WriteLine(state.Description());
like image 42
Matthew Watson Avatar answered Jul 15 '26 06:07

Matthew Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!