How to set space on Enum - c#

I want to set the space on my enum. Here is my code sample:
public enum category
{
goodBoy=1,
BadBoy
}
I want to set
public enum category
{
Good Boy=1,
Bad Boy
}
When I retrieve I want to see Good Boy result from the enum

You can decorate your Enum values with DataAnnotations, so the following is true:
using System.ComponentModel.DataAnnotations;
public enum Boys
{
[Display(Name="Good Boy")]
GoodBoy,
[Display(Name="Bad Boy")]
BadBoy
}
I'm not sure what UI Framework you're using for your controls, but ASP.NET MVC can read DataAnnotations when you type HTML.LabelFor in your Razor views.
Here' a Extension method
If you are not using Razor views or if you want to get the names in code:
public class EnumExtention
{
public Dictionary<int, string> ToDictionary(Enum myEnum)
{
var myEnumType = myEnum.GetType();
var names = myEnumType.GetFields()
.Where(m => m.GetCustomAttribute<DisplayAttribute>() != null)
.Select(e => e.GetCustomAttribute<DisplayAttribute>().Name);
var values = Enum.GetValues(myEnumType).Cast<int>();
return names.Zip(values, (n, v) => new KeyValuePair<int, string>(v, n))
.ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
Then use it:
Boys.GoodBoy.ToDictionary()

You are misunderstanding what an enum is used for. An enum is for programming purposes, essentially giving a name to a number. This is for the programmer's benefit while reading the source code.
status = StatusLevel.CRITICAL; // this is a lot easier to read...
status = 5; // ...than this
Enums are not meant for display purposes and should not be shown to the end user. Like any other variable, enums cannot use spaces in the names.
To associate internal values with "pretty" labels you can display to a user, can use a dictionary or hash.
myDict["Bad Boy"] = "joe blow";

using System.ComponentModel;
then...
public enum category
{
[Description("Good Boy")]
goodboy,
[Description("Bad Boy")]
badboy
}
Solved!!

Think this might already be covered, some suggestions:
How do I have an enum bound combobox with custom string formatting for enum values?
C# Getting Enum values
Just can't beat stackoverflow ;) just sooo much on here nowdays.

Based on Smac's suggestion, I've added an extension method for ease, since I'm seeing a lot of people still having issues with this.
I've used the annotations and a helper extension method.
Enum definition:
internal enum TravelClass
{
[Description("Economy With Restrictions")]
EconomyWithRestrictions,
[Description("Economy Without Restrictions")]
EconomyWithoutRestrictions
}
Extension class definition:
internal static class Extensions
{
public static string ToDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
}
Example using the enum:
var enumValue = TravelClass.EconomyWithRestrictions;
string stringValue = enumValue.ToDescription();
This will return Economy With Restrictions.
Hope this helps people out as a complete example. Once again, credit goes to Smac for this idea, I just completed it with the extension method.

That's not possible, an enumerator cannot contain white space in its name.

Developing on user14570's (nice) workaround referred above, here's a complete example:
public enum MyEnum
{
My_Word,
Another_One_With_More_Words,
One_More,
We_Are_Done_At_Last
}
internal class Program
{
private static void Main(string[] args)
{
IEnumerable<MyEnum> values = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
List<string> valuesWithSpaces = new List<string>(values.Select(v => v.ToString().Replace("_", " ")));
foreach (MyEnum enumElement in values)
Console.WriteLine($"Name: {enumElement}, Value: {(int)enumElement}");
Console.WriteLine();
foreach (string stringRepresentation in valuesWithSpaces)
Console.WriteLine(stringRepresentation);
}
}
Output:

Why don't you use ToString() ?
I mean that when use ToString(),it gives the enum value. Just you have to add some identifier to catch space.For example:
public enum category
{
good_Boy=1,
Bad_Boy
}
When you get an enum in codes like category a = ..., you can use ToString() method. It gives you value as a string. After that, you can simply change _ to empty string.

I used Regex to split the values by capital letter and then immediately join into a string with a space between each string in the returned array.
string.Join(" ", Regex.Split(v.ToString(), #"(?<!^)(?=[A-Z])"));
First get the values of the enum:
var values = Enum.GetValues(typeof(Category));
Then loop through the values and use the code above to get the values:
var ret = new Dictionary<int, string>();
foreach (Category v in values)
{
ret.Add((int)v, string.Join(" ", Regex.Split(v.ToString(), #"(?<!^)(?=[A-Z])")));
}
In my case I needed a dictionary with the value and display name so that it why I have the variable "ret"

C# now has a built in function to get the description from an enum. Here's how it works
My Enum:
using System.ComponentModel.DataAnnotations;
public enum Boys
{
[Description("Good Boy")]
GoodBoy = 1,
[Description("Bad Boy")]
BadBoy = 2
}
This is how to retrieve the description in code
var enumValue = Boys.GoodBoy;
string stringValue = enumValue.ToDescription();
Result is : Good Boy.

public enum MyEnum { With_Space, With_Two_Spaces } //I store spaces as underscore. Actual values are 'With Space' and 'With Two Spaces'
public MyEnum[] arrayEnum = (MyEnum[])Enum.GetValues(typeof(MyEnum));
string firstEnumValue = String.Concat(arrayEnum[0].ToString().Replace('_', ' ')) //I get 'With Space' as first value
string SecondEnumValue = String.Concat(arrayEnum[1].ToString().Replace('_', ' ')) //I get 'With Two Spaces' as second value

If you do not want to write manual annotations, you can use an extension method that will add spaces to the enum's names:
using System.Text.RegularExpressions;
public static partial class Extensions
{
public static string AddCamelSpace(this string str) => Regex.Replace(Regex.Replace(str,
#"([^_\p{Ll}])([^_\p{Ll}]\p{Ll})", "$1 $2"),
#"(\p{Ll})([^_\p{Ll}])" , "$1 $2");
public static string ToCamelString(this Enum e) =>
e.ToString().AddCamelSpace().Replace('_', ' ');
}
You can use like this:
enum StudentType
{
BCStudent,
OntarioStudent,
badStudent,
GoodStudent,
Medal_of_HonorStudent
}
StudentType.BCStudent.ToCamelString(); // BC Student
StudentType.OntarioStudent.ToCamelString(); // Ontario Student
StudentType.badStudent.ToCamelString(); // bad Student
StudentType.GoodStudent.ToCamelString(); // Good Student
StudentType.Medal_of_HonorStudent.ToCamelString(); // Medal of Honor Student
See on .NET fiddle

Retrieving enum value is pretty complicated to me, while enum value is different than its name. For this purpose, I would like to use a class with const fields and a list that contains all of these fields. Using this list, I could check later to validate.
public class Status
{
public const string NOT_STARTED = "not started";
public const string IN_PROGRESS = "in progress";
public const string ON_HOLD = "on hold";
public const string COMPLETED = "completed";
public const string REFUSED = "refused";
public static string[] List = new string[] {
NOT_STARTED,
IN_PROGRESS,
ON_HOLD,
COMPLETED,
REFUSED
};
}
class TestClass {
static void Main(string[] args) {
var newStatus = "new status"
if (!Status.List.Contains(newStatus))
{
// new status is not valid
}
if (newStatus == Status.IN_PROGRESS)
{
// new status in progress
}
}
}

I define Enum with underscore and replace when using later in code:
// Enum definition
public enum GroupName
{
Major_Features, Special_Features, Graphical_Features
};
// Use in code:
internal Group GetGroup(GroupName groupName)
{
//...
string name = groupName.ToString().Replace('_',' '));
//...
}

What is your purpose of this question, if you want to have a set of strings, then you want to have a integer value for each of keys, the best way is using a Dictionary from your keys and values, like this:
Dictionary<string, int> MyDictionary = new Dictionary<string, int>()
{
{"good Boy", 1 },
{"Bad Boy", 2 },
};
Then you can give the integer value from the key like this:
int value = MyDictionary["good Boy"];

You can write the spaced word in brackets. Then define a constructor to take up the values inside the bracket. As given below
public enum category
{
goodBoy("Good Boy"),
BadBoy("Bad Boy")
}
private String categoryType;
category(String categoryType) {
this.categoryType = categoryType;
}

You cannot have enum with spaces in .Net. This was possible with earlier versions of VB and C++ of course, but not any longer. I remember that in VB6 I used to enclose them in square brackets, but not in C#.

Since the original question was asking for adding a space within the enum value/name, I would say that the underscore character should be replaced with a space, not an empty string. However, the best solution is the one using annotations.

An enumerator cannot contain white space in its name.
As we know that enum is keyword used to declare enumeration.
you can check throw this link
https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

Related

Enum item mapped to another value

I have enum:
enum MyEnum{
aaaVal1,
aaaVal2,
aaaVal3,
}
I need to have abbreviated version of 'MyEnum' which maps every item from 'MyEnum' to different values. My current approach is method which simply translates every item:
string translate(MyEnum myEnum)
{
string result = "";
switch ((int)myEnum)
{
0: result = "abc";
1: result = "dft";
default: result = "fsdfds"
}
return result;
}
the problem with this approach is that every time programmer changes MyEnum he should also change translate method.
This is not a good way of programming.
So..
Is there any more elegant solution for this problem?
Thank you :-)
Four options:
Decorate your enum values with attributes, e.g.
enum MyEnum
{
[Description("abc")]
AaaVal1,
[Description("dft")]
AaaVal2,
AaaVal3,
}
Then you can create a mapping (like the dictionary solution below) via reflection.
Keep the switch statement but switch on the enum value instead of a number for better readability:
switch (myEnum)
{
case MyEnum.AaaVal1: return "abc";
case MyEnum.AaaVal2: return "dft";
default: return "fsdfds";
}
Create a Dictionary<MyEnum, string>:
private static Dictionary<MyEnum, string> EnumDescriptions =
new Dictionary<MyEnum, string>
{
{ MyEnum.AaaVal1, "abc" },
{ MyEnum.AaaVal2, "dft" },
};
You'd need to handle the defaulting in the method, of course.
Use a resource file, with an entry for each string representation. This would be better if you're really trying to translate in a way that might need different translations for different cultures.
Considering that the use of descriptors on enums is quite common, here it's a good-enough class to do it:
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
class EnumDescriptor : Attribute
{
public readonly string Description;
public EnumDescriptor(string description)
{
this.Description = description;
}
public static string GetFromValue<T>(T value) where T : struct
{
var type = typeof(T);
var memInfo = type.GetField(value.ToString());
var attributes = memInfo.GetCustomAttributes(typeof(EnumDescriptor), false);
if (attributes.Length == 0)
{
return null;
}
return ((EnumDescriptor)attributes[0]).Description;
}
}
enum MyEnum
{
[EnumDescriptor("Hello")]
aaaVal1,
aaaVal2,
aaaVal3,
}
string translate(MyEnum myEnum)
{
// The ?? operator returns the left value unless the lv is null,
// if it's null it returns the right value.
string result = EnumDescriptor.GetFromValue(myEnum) ?? "fsdfds";
return result;
}
I'm finding what you're trying to do a bit weird.
If you're making translations, then you should create a RESX file and create ACTUAL translations.
But to answer your question, I guess you could create another enum with the same amount of fields and same numbering (if you're using anything other than the default) and have that act as the abbreviated names. Connecting one to the other should be straightforward:
string GetAbbreviation(Enum1 enum1)
{
return ((Enum2)((int)enum1)).ToString();
}
Attributes will be nice solution for this case. You can specify translations for enumeration members via declarative way:
public class TranslateAttribute
{
public string Translation { get; private set; }
public TranslateAttribute(string translation)
{
Translation = translation;
}
}
enum MyEnum
{
[Translate("abc")]
aaaVal1,
[Translate("dft")]
aaaVal2,
[Translate("fsdfds")]
aaaVal3
}
After this you should write common method for obtaining translations. It should check attribute with translation (via reflection) and return translation if it was specified and default value in other cases.

How to restrict a variable to a fixed set of strings?

If I want to restrict the values of the spicelevel column in the database to 1, 2 and 3, I could do something like
private enum SpiceLevel
{
Low=1,
Medium=2,
Hot=3
}
Then in the code I could do (int)SpiceLevel.Low to pick 1 as the spice level.
Now what if I have a need where I can only accept "Red Rose","White Rose" and "Black Rose" for the values of a column in the database? What is a graceful way to handle this?
I am thinking of storing them in a config file or constants, but neither is as graceful as enums. Any ideas?
Update:
The answer here worked for me
You can use a property for this
public string[] AllowedRoses = new string[] { "Red Rose", "White Rose" ,"Black Rose" };
string _Rose = "Red Rose";
public string Rose
{
get
{
return _Rose;
}
set
{
if (!AllowedRoses.Any(x => x == value))
throw new ArgumentException("Not valid rose");
_Rose = value;
}
}
I can see the following options:
verify the value in the setter (see for example l4V's answer)
conceptually, you're thinking about an enum. So you could do the following:
enum RoseType { RedRose, WhiteRose, BlackRose };
and then provide appropriate conversion from this enum to string. Two convenient options how to do it are described here: Enum ToString with user friendly strings. One is to use a custom Description attribute, and the second (I'd prefer this one) to provide an extension method:
public static class RoseTypeExtensions
{
public static string GetString(this RoseType #this)
{
switch (#this)
{
case RoseType.RedRose:
return "Red Rose";
case RoseType.WhiteRose:
return "White Rose";
case RoseType.BlackRose:
return "Black Rose";
default:
throw new InvalidOperationException();
}
}
}
create a set of constants:
public class RoseType
{
public readonly RoseType RedRose = new RoseType("Red Rose");
public readonly RoseType WhiteRose = new RoseType("White Rose");
public readonly RoseType BlackRose = new RoseType("Black Rose");
public string Content { get; private set; }
private RoseType(string content)
{
this.Content = content;
}
public override string ToString()
{
return this.Content;
}
}
As Oskar Berggren correctly pointed out in the comment, RoseType should also provide other standard overrides beside ToString: Equals, GetHashCode, operator== and operator!=.
There is no really good solution. All of them require the database to be "synchronized" with the enum in C#.
Simplest solution:
What you said store the enum values as integers in the database.
Almost as simple but less efficient:
Store the values as strings in the database and convert between string and enum with anEnumVar.ToString() and Enum.Parse (or any of the other parse methods in Enum).
Complex but flexible:
Have a sort of enum in the database: a table with string values and ids and then use foreign keys to that table where you want to save the enums. This allows you to either select/update/insert using the numeric value or the string value (via a join).
It also maintains integrity as it is not possible to store an integer which has no corresponding enum value.
The downside is the complexity.
Create a mapping of string to enum with Dictionary<string, SpiceLevel> to associate the string to the Enum. Wrap them in a class.
You could also use a Decorator attribute [Name("Red Rose"] Low=1, and get that from the enum itself, but that involves reflection, which has some performance issues, especially when iterating through enum values to find the one with the matching attribute.
public static class Spice
{
public enum Level
{
Low = 1,
Medium = 2,
Hot = 3
}
private static readonly Dictionary<string, Level> spices = new Dictionary<string, Level>{
{ "Red Rose", Level.Low },
{ "White Rose", Level.Medium },
{ "Black Rose", Level.Hot },
};
public static bool TryGet(string spiceName, out Level spiceLevel) => spices.TryGetValue(spiceName, out spiceLevel);
public static string SpiceName(Level target) => Enum.GetName(typeof(Spice.Level), target);
}
/// <summary>
/// Some tests to validate it works. This could be a unit test or just in a console app
/// </summary>
public class SpiceTest
{
public void VerifyBlackRoseIsHot()
{
string subject = "Black Rose";
Spice.Level expectedSpice;
// Here's the ease of use. Pass a string, get an enum and whether it's a valid string
var result = Spice.TryGet(subject, out expectedSpice);
//Some Assertion from a unit test library
Assert.True(result, $"Unable to find spice '{subject}', when it should exist");
Assert.True(Spice.Level.Hot.Equals(expectedSpice), $"The returned spice '{ Spice.SpiceName(expectedSpice) }' was not the value 'Hot' as expected");
}
}

Can I make an Enum return something other than an integer?

If I have the following:
public enum TYPE
{
One = 1,
Two = 2,
Three = 3
}
When I do:
var a = TYPE.One;
I would like it to populate the variable a with a string in the format "01". In other words two digits with a leading zero.
Is it possible to do this by assigning some method to the SomeEnum? I realized I could use TYPE.One.ToString("00")but I would like to have it self-contained in the enum and something very simple to use.
You can add a string to each enum element using the description attribute.
e.g.
public Enum MyEnum
{
[Description("Value A Description")]
ValueA,
[Description[("Value B Description")]
ValueB
}
To retrieve the description value, use an extender class
public static class MyEnumExtender
{
public static string Description(this Enum Value)
{
FieldInfo FI = Value.GetType().GetField(Value.ToString());
IEnumerable<DescriptionAttribute> Attributes = FI.GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>();
return (Attributes.Any()) ? Attributes.First().Description : Value.ToString();
}
}
....
MyEnum EnumVar = MyEnum.ValueA;
string Description = EnumVar.Description();
can do something like this :
public static class Ext {
public static string ToMyString(this Enumer en ) {
return ((int)en).ToString("00");
}
}
and after use this like:
public enum TYPE { One = 1, Two = 2, Three = 3 }
Type t = TYPE.One;
string s = t.ToMyString();
Yes, conceptually it's the same as like declaring a string , but it's hidden inside extension method.
Other solution is: to simply avoid, at this point, using enums in that way.
Don't use enums for that, but something like a Dictionary or Hash. Enums are there when there is a limited set of possibilities and you do not want or need a value. How it is stored is irrelevant.

C# Enum return string with breaks

I need my enum to return a specific string, but I can't work out how to make it return a string with breaks in it without having a method to do the conversion. Is it possible to make LicenseTypes.DISCOUNT_EARLY_ADOPTER return DISCOUNT EARLY-ADOPTER without the helper method?
// All license types
public enum LicenseTypes
{
DISCOUNT,
DISCOUNT_EARLY_ADOPTER,
COMMERCIAL,
COMMERCIAL_EARLY_ADOPTER
}
// Convert enum to correct string
public static string LicenseTypeToString(LicenseTypes Enum)
{
if (Enum == LicenseTypes.COMMERCIAL)
return "COMMERCIAL";
else if (Enum == LicenseTypes.COMMERCIAL_EARLY_ADOPTER)
return "COMMERCIAL EARLY-ADOPTER";
else if (Enum == LicenseTypes.DISCOUNT)
return "DISCOUNT";
else if (Enum == LicenseTypes.DISCOUNT_EARLY_ADOPTER)
return "DISCOUNT EARLY-ADOPTER";
else
return "ERROR";
}
Firstly, a separate option from a helper method is simply to have a Dictionary<LicenseTypes, string> which you populate once. That would probably be the simplest approach, to be honest:
private static readonly Dictionary<LicenseTypes, string> LicenseDesciptions =
new Dictionary<LicenseTypes, string>
{
{ LicenseTypes.COMMERCIAL, "COMMERCIAL" },
{ LicenseTypes.COMMERCIAL_EARLY_ADOPTER, "COMMERCIAL EARLY-ADOPTER" },
{ LicenseTypes.DOMESTIC, "DOMESTIC" },
{ LicenseTypes.DOMESTIC_EARLY_ADOPTER, "DOMESTIC EARLY-ADOPTER" },
};
(As noted in comments, another alternative is a switch/case... but I personally prefer this way, as effectively you've got a data mapping, so it makes sense to use a data structure rather than an execution flow structure. It also means you can swap out dictionaries for different languages etc if you want.)
Secondly, one option would be to decorate each enum value with a [Description] attribute (or your own attribute if you want), and find that out with reflection - Unconstrained Melody has an extension method which can do that very easily:
// Throws ArgumentOutOfRangeException if the licenseType value isn't defined
// or doesn't have a description.
string description = licenseType.GetDescription();
Also, following .NET naming conventions it should be:
public enum LicenseType // Singular as it's not a Flags enum
{
Discount,
DiscountEarlyAdopter,
Commercial,
CommercialEarlyAdopter
}
A little reflection, and Attribute magic, and this should just answer it :)
Getting attributes of Enum's value
Might also make it an extension method.
here's how it should look (also added generics):
public static class MyExtensionsClass
{
public static string ToDescriptionString<T>(this T val)
where T : struct, IConvertible
{
if (typeof(T).IsEnum)
{
var type = val.GetType();
var memInfo = type.GetMember(val.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
return ((DescriptionAttribute)attributes[0]).Description;
}
return ""; //all paths must return a value
}
}
public enum LicenseTypes
{
[Description("DISCOUNT")]
DISCOUNT,
[Description("DISCOUNT EARLY-ADOPTER")]
DISCOUNT_EARLY_ADOPTER,
[Description("COMMERCIAL")]
COMMERCIAL,
[Description("COMMERCIAL EARLY-ADOPTER")]
COMMERCIAL_EARLY_ADOPTER
}
Thank you, guys that wrote these:
Enhance enums using extension methods
Create Generic method constraining T to an Enum
Good luck!
While it doesn't eliminate the helper method, note that in your case you could just special-case the values that you can't get using ToString:
switch (Enum) {
case LicenseTypes.COMMERCIAL_EARLY_ADOPTER:
return "COMMERCIAL EARLY-ADOPTER";
case LicenseTypes.DISCOUNT_EARLY_ADOPTER:
return "DISCOUNT EARLY-ADOPTER";
default
return Enum.ToString();
}
I use this from Google's dotnet client - StringValueAttribute.cs
and Utilities.cs
public enum LicenseType
{
[StringValue("DISCOUNT")] Discount,
[StringValue("DISCOUNT EARLY-ADOPTER")] DiscountEarlyAdopter,
[StringValue("COMMERCIAL")] Commercial,
[StringValue("COMMERCIAL EARLY-ADOPTER")] CommercialEarlyAdopter
}
Then you can simply do this:
licenseType.GetStringValue();
Convert to string using "G" format, then replace "_" (underscore) with spaces:
LicensceTypes license = LicenseTypes.COMMERCIAL_EARLY_ADOPTERS;
string licenseDescription = license.ToString("G").Replace('_', ' ');
// licenseDescription = "COMMERCIAL EARLY ADOPTERS"
I think I would use a class to avoid this scenario. :/
public class LicenceType
{
private string name;
public LicenceType(string Name)
{
this.name = Name;
}
public override string ToString()
{
return name;
}
}
public static class LicenceTypes
{
public static LicenceType DISCOUNT = new LicenceType("DISCOUNT");
public static LicenceType DISCOUNT_EARLY_ADOPTER= new LicenceType("DISCOUNT EARLY-ADOPTER");
public static LicenceType COMMERCIAL= new LicenceType("COMMERCIAL");
public static LicenceType COMMERCIAL_EARLY_ADOPTER= new LicenceType("COMMERCIAL EARLY-ADOPTER");
}

Numeric constants in enum (c#)

I'm creating this selectbox in a SharePoint web part and need to have a drop down with the current version so I need to use an Enum.
public enum SelectVersionEnum { 2010, 2007 };
Well you can see where it breaks, is there any way to use integers in a enum?
Most of all I'd like to use
public enum SelectVersionEnum { 2010=14, 2007=12 };
No, you can not name enums with integer names.
An enum value name is a normal identifier and must follow the same rules as everything else.
You can, however, use:
public enum SelectVersionEnum
{
Version2007 = 12,
Version2010 = 14
}
Additionally, Enum.Parse can parse strings with integers into their corresponding enum value, even if value described in the string doesn't exist.
Try the following in LINQPad:
void Main()
{
Enum.Parse(typeof(SelectVersionEnum), "12").Dump();
Enum.Parse(typeof(SelectVersionEnum), "14").Dump();
Enum.Parse(typeof(SelectVersionEnum), "2007").Dump();
}
public enum SelectVersionEnum
{
Version2007 = 12,
Version2010 = 14
}
The output:
Version2007
Version2010
2007
What do you think would've happened if you defined the following:
public enum SelectVersionEnum
{
12 = 14,
14 = 16
}
Does the string "14" now mean "12" or "14"?
Enum members must be valid C# identifiers.
They cannot start with numbers.
Instead, you can use something like Office2007, Office2010 or V2007, V2010.
No, enum identifiers can't start with a numeric character.
You can add an extension method to your enum like any other type.
So you could create an extension for your SelectVersionEnum to help you get the enum values in the right format...
public static class SelectVersionEnumExtension
{
public static int Version(this SelectVersionEnum enumValue)
{
return 0; // Obviously you should return something meaningful here..
}
}
This gives you a lot of flexibilty.
One way you could have numeric values associated with enums is by using the description attribute. For example you might have the enum:
[Serializable]
public enum SelectVersionEnum
{
[Description("2007")]
v2007,
[Description("2010")]
v2010
}
You could then write an extension method to get the numeric value you are looking for.
public static string Description(this Enum value)
{
var type = value.GetType();
var name = Enum.GetName(type, value);
if (name != null)
{
if (type.GetField(name) != null)
{
var attr = Attribute.GetCustomAttribute(type.GetField(name), typeof(DescriptionAttribute)) as DescriptionAttribute;
return attr != null ? attr.Description : name;
}
}
return null;
} // end
You would use it like this:
var version = SelectVersionEnum.v2007.Description();

Categories

Resources