I have an enum:
public enum Status
{
Incomplete = 1, Complete = 2, Cancelled = 3, Deleted = 4
}
Now on a certain page I wish to list this enum in a checkboxlist. This would be fine except that I want the text of each checkbox to display different text than the enum.
i.e the check boxes should say:
“Not Processed” instead of “Incomplete”
“Processed” instead of “Complete”
“Void” instead of “Cancelled”
Is it possible to put this enum in a foreach and then switch on the status and update the text. Like so:
var statuses = Enum.GetNames(typeof(Status));
foreach (var status in statuses)))
{
switch (status)
{
case Status.Complete.ToString():
status = "Processed";
break; ...etc
}
}
Any ideas would be greatly appreciated.
C# has a language feature that directly addresses your question. Here’s an article that gives you the complete details.
The short version: apply extension attribute values to each value:
Then apply said values to your radio-buttons, listbox or whatever: