How to check if a string value is in an Enum list?

In my query string, I have an age variable ?age=New_Born .

Is there any way to check if this is the string value of New_Born in my Enum list

 [Flags] public enum Age { New_Born = 1, Toddler = 2, Preschool = 4, Kindergarten = 8 } 

I could use an if statement right now, but if the Enum list gets bigger. I want to find a better way to do this. I am thinking about using Linq, just not sure how to do this.

+55
May 29 '12 at 17:45
source share
6 answers

You can use:

  Enum.IsDefined(typeof(Age), youragevariable) 
+101
May 29 '12 at 17:46
source share

You can use the Enum.TryParse method:

 Age age; if (Enum.TryParse<Age>("New_Born", out age)) { // You now have the value in age } 
+23
May 29 '12 at 17:48
source share

You can use the TryParse method, which returns true if it is successful:

 Age age; if(Enum.TryParse<Age>("myString", out age)) { //Here you can use age } 
+7
May 29 '12 at 17:48
source share

To analyze age:

 Age age; if (Enum.TryParse(typeof(Age), "New_Born", out age)) MessageBox.Show("Defined"); // Defined for "New_Born, 1, 4 , 8, 12" 

To find out if this is defined:

 if (Enum.IsDefined(typeof(Age), "New_Born")) MessageBox.Show("Defined"); 

Depending on how you plan to use the Age enumeration, flags may be wrong. As you probably know, [Flags] indicates that you want to allow multiple values ​​(as in a bitmask). IsDefined will return false for Age.Toddler | Age.Preschool Age.Toddler | Age.Preschool , as it has several meanings.

0
May 29 '12 at 17:47
source share

You must use Enum.TryParse to achieve your goal.

That's an example:

 [Flags] private enum TestEnum { Value1 = 1, Value2 = 2 } static void Main(string[] args) { var enumName = "Value1"; TestEnum enumValue; if (!TestEnum.TryParse(enumName, out enumValue)) { throw new Exception("Wrong enum value"); } // enumValue contains parsed value } 
0
May 29 '12 at 17:52
source share

I know this is an old thread, but here is a slightly different approach using attributes in Enumerates, and then a helper class to find the numbering that matches.

This way you can have multiple mappings for a single listing.

 public enum Age { [Metadata("Value", "New_Born")] [Metadata("Value", "NewBorn")] New_Born = 1, [Metadata("Value", "Toddler")] Toddler = 2, [Metadata("Value", "Preschool")] Preschool = 4, [Metadata("Value", "Kindergarten")] Kindergarten = 8 } 

With my helper class like this

 public static class MetadataHelper { public static string GetFirstValueFromMetaDataAttribute<T>(this T value, string metaDataDescription) { return GetValueFromMetaDataAttribute(value, metaDataDescription).FirstOrDefault(); } private static IEnumerable<string> GetValueFromMetaDataAttribute<T>(T value, string metaDataDescription) { var attribs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof (MetadataAttribute), true); return attribs.Any() ? (from p in (MetadataAttribute[]) attribs where p.Description.ToLower() == metaDataDescription.ToLower() select p.MetaData).ToList() : new List<string>(); } public static List<T> GetEnumeratesByMetaData<T>(string metadataDescription, string value) { return typeof (T).GetEnumValues().Cast<T>().Where( enumerate => GetValueFromMetaDataAttribute(enumerate, metadataDescription).Any( p => p.ToLower() == value.ToLower())).ToList(); } public static List<T> GetNotEnumeratesByMetaData<T>(string metadataDescription, string value) { return typeof (T).GetEnumValues().Cast<T>().Where( enumerate => GetValueFromMetaDataAttribute(enumerate, metadataDescription).All( p => p.ToLower() != value.ToLower())).ToList(); } } 

you can do something like

 var enumerates = MetadataHelper.GetEnumeratesByMetaData<Age>("Value", "New_Born"); 

And for completeness, there is an attribute here:

  [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)] public class MetadataAttribute : Attribute { public MetadataAttribute(string description, string metaData = "") { Description = description; MetaData = metaData; } public string Description { get; set; } public string MetaData { get; set; } } 
0
Apr 13 '17 at 22:45
source share



All Articles