Manoj Nayak
Manoj Nayak

Reputation: 2509

Take a value from the list and convert it to a enum

I have a list with the following items.
Mistakes, Role and Name . In the database the Role is Integer . That is roles can be 1,2,3,4,5,6 etc. There is a enum class for Role 1= Reviewer1 2=Reviewer2 ..6-Quality Reviewer . I am aasigning the List value to the object. How can I take Role from the List and Convert to the Enum and assign to the object again . Here is the code sample

var result = new ChecklistLiability();
 result.CheckpointInstanceInfo = liabilityMapper.GetCheckInstanceInformationByIxLiability(result.IxLiability);

Result is the object.
liabilityMapper.GetCheckInstanceInformationByIxLiability(result.IxLiability); function will return a list with Role as Integer .

Upvotes: 1

Views: 79

Answers (2)

Console.WriteLine
Console.WriteLine

Reputation: 443

You can also try Enum.Parse and Enum.TryParse.

Casting does what you need but the .Parse methods are a little bit cleaner in my opinion.

Upvotes: 1

Jakub Konecki
Jakub Konecki

Reputation: 46018

Just cast it, as in this example:

int roleAsInt = 1;
Role role = (Role) roleAsInt;

Upvotes: 2

Related Questions