I really don't know a better way to phrase the title...
I have several enums in my project and what I'm trying to do is output a string representing the selected enum value.
Consider the following enum:
public enum MedicineType
{
Drops,
Liquid,
Cream,
Powder
}
If the user selected the Liquid
value, I want to output the string "02":
public string GetStringFromEnum(MedicineType medicineType)
{
// Output values should be something like:
// Drops = "01",
// Liquid = "02",
// Cream = "03",
// Powder = "04"
return ("0" + (int)medicineType + 1)
}
If the selected index is < 10, it should pad with a 0 but if its 10 or more, it should just output the value:
var selectedValue = (int)medicineType + 1;
return (selectedValue > 10) ? "0" + selectedValue : selectedValue.ToString();
Where I'm falling down is that the code to get the string representation for the selected enum value is going to be pretty much identical for every enum (I have 26) so I don't want to be repeating this code in a new method for all 26 enums. I'd prefer to have a call to a sincle GetStringFromEnum
method that will accept any enum as a parameter.
Is this possible? If not, what would be a decent workaround that would limit code repetition?