0

Is there a way to get the value from the Enum Description? I have been searching but most cases are returning Description from a value. Is there a way to do this backwards, return the value from the description?

I.E Given "Bio Chem Physics" I want it to return FirstYearScience. So I can use it in another dictionary and reference it by bookType.FirstYearScience.

I am only given the description nothing else, or is there a better way to approach this problem? Thanks!

using System.ComponentModel;    
public enum bookType 
    {
     [EnumMember]
     [Description("Non Fiction")]
     NonFiction,

     [EnumMember]
     [Description("Fiction")]
     Fiction,

     [EnumMember]
     [Description("Bio Chem Physics")]
     FirstYearScience,

    } 
Suzy
  • 231
  • 4
  • 14
  • 2
    Break the problem down. There is a way to get the list of entries for an enum. There is a way to get an attribute from the enum entry. There is a way to store strings and enums in a Dictionary. Plug it all together. – mjwills Sep 17 '20 at 23:56
  • Have you tried using Enum.GetName(typeof(bookType), 3)? https://learn.microsoft.com/en-us/dotnet/api/system.enum.getname?view=netcore-3.1 – Dev Sep 18 '20 at 00:01
  • Are you saying you don't have the enum type? ONLY a description?? – Rufus L Sep 18 '20 at 00:04
  • `Given "Bio Chem Physics" I want it to return FirstYearScience.` Seems that way @RufusL. – mjwills Sep 18 '20 at 00:34
  • Thanks, yea only have "Bio Chem Physics" and bookType, not the members. GetName only works with int and not with the string. Found ways for it to work if it's all int, but this case has strings, so that's the confusing part. – Suzy Sep 18 '20 at 20:26

0 Answers0