Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Cast string to a given Enum

Tags:

c#

.net

enums

how to cast string enum ?

i have the code below , it gives me error when i try to assign string to levelEnum, where levelEnum is an Enumeration..

foreach (CustomProperty prop in requirementTemplate.AttributesCustomList)
{
    if (prop.Name == property)
    {
        return (CRF_DB.CRF_Requirement.LevelEnum) (prop.Value.ToString());
    }
}

Is there a way to put select Enum item by assigning value to it ?

hope it is clear enough

like image 318
Yasser Avatar asked Feb 23 '23 06:02

Yasser


1 Answers

Try the following

return (CRF_DB.CRF_Requirement.LevelEnum)Enum.Parse(
  typeof(CRF_DB.CRF_Requirement.LevelEnum), 
  prop.Value.ToString());
like image 111
JaredPar Avatar answered Mar 03 '23 06:03

JaredPar