Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have an enum store extra info on each of it's entry

Tags:

c#

oop

enums

I have a set of items that have information on them. These items are defined by me, the programmer, and the user do not ever need to change them, they will never need to change based on configuration, and the only time they might change is in a future version of my application. I know before hand how many of these items there should be, and what their exact data is.

An enum, is a great programming construct that let's me predefine a group of keys available in my application and group them under a logical Type.

What I need now, is a construct that let's me predefine a group of keys that have extra information attached to them.

Example:

This is a standard enum:

public enum PossibleKeys
{
    Key1,
    Key2,
    Key3
}

This is the enum I would need:

public enum PossibleItems
{
    Item1
    {
        Name = "My first item",
        Description = "The first of my possible items."
    },
    Item2
    {
        Name = "My second item",
        Description = "The second of my possible items."
    },
    Item3
    {
        Name = "My third item",
        Description = "The third of my possible items."
    }
}

I know this kind of enum does not exist. What I'm asking is: How can I, in C#, hard code a set of predefined items whose data is set in code? What would be the best way to do this?

EDIT: I don't necessarily want a solution that uses enums, just a solution that allows me to have this behaviour, which is to know all possible items in my application, and the info that each of them has.

EDIT 2: It's important for me to be able to get all existing items at runtime. So being able to query all items and to iterate over them is required. Just like I could with an enum.

like image 701
Didier A. Avatar asked Oct 15 '25 07:10

Didier A.


2 Answers

If it's purely for description you can use the built-in DescriptionAttribute as stated in some of the other answers. If you need functionality that an attribute can't supply, however, you can create a lookup with some sort of metadata object.

Something like this:

public enum PossibleKeys
{
    Key1,
    Key2,
    Key3
}

public class KeyMetadata
{
    public PossibleKeys Id { get; set; }
    public string Description { get; set; }
    public SomeOtherClass SomethingAttributesCantHandle { get; set; }
}

private static readonly IReadOnlyDictionary<PossibleKeys, KeyMetadata> KeyMetadataLookup;

static EnumContainerClass()
{
    Dictionary<PossibleKeys, KeyMetadata> metadata = new Dictionary<PossibleKeys, KeyMetadata>();
    metadata.Add(PossibleKeys.Key1, new KeyMetadata { Id = PossibleKeys.Key1, Description = "First Item" });
    metadata.Add(PossibleKeys.Key2, new KeyMetadata { Id = PossibleKeys.Key2, Description = "Second Item" });
    metadata.Add(PossibleKeys.Key3, new KeyMetadata { Id = PossibleKeys.Key3, Description = "Third Item" });
    KeyMetadataLookup = new ReadOnlyDictionary<PossibleKeys, KeyMetadata>(metadata);
}

Then to retrieve:

KeyMetadataLookup[PossibleKeys.Key1].Description

Note that I'd only use this if there was something attributes couldn't handle. If it's all primitive types you can also simply make your own custom attribute. You're not limited to simply the built-in ones.

Your own custom attribute would end up like:

[System.AttributeUsage(System.AttributeTargets.Field)]
public class CustomDataAttribute : System.Attribute
{
    public string Name { get; set; }

    public string Description { get; set; }
}

Then in use:

public enum PossibleItems
{
    [CustomData(Name = "My first item", Description = "The first of my possible items.")]
    Item1,

    [CustomData(Name = "My second item", Description = "The second of my possible items.")]
    Item2,

    [CustomData(Name = "My third item", Description = "The third  of my possible items.")]
    Item3
}
like image 102
McAden Avatar answered Oct 17 '25 06:10

McAden


try this

public enum PossibleKeys{
    [Description("key 1 desc")]
    Key1,
    [Description("key 2 desc")]
    Key2,
    [Description("key 3 desc")]
    Key3
}
like image 44
Milan Melman Avatar answered Oct 17 '25 04:10

Milan Melman