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.
Access using
PossibleItems.Item1.Name;
When I need an associated description with the enum values, I do this by putting a
DescriptionAttribute
on each enum value and then use an extension method to gain access to that data. Here is a complete LINQPad example of this.This outputs