Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer enum to string

Tags:

c#

enums

considering the following enum:

public enum LeadStatus 
{ 
    Cold = 1, 
    Warm = 2, 
    Hot = 3, 
    Quote = 5, 
    Convert = 6 
} 

How can I convert the integer value back to string when I pull the value from a database. I've tried:

DomainModel.LeadStatus status = (DomainModel.LeadStatus)Model.Status;

but all I seem to get is "status = 0"

like image 879
user216205 Avatar asked Dec 18 '22 01:12

user216205


2 Answers

What you are looking for is Enum.Parse.

"Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object."

Here is the MSDN page: http://msdn.microsoft.com/en-us/library/essfb559.aspx

Example:

enum Colour
{
   Red,
   Green,
   Blue
} 

// ...
Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);

Courtesy of http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

like image 83
vfilby Avatar answered Dec 24 '22 01:12

vfilby


Between Enum.Parse and Enum.ToString, you should be able to do everything you need.

like image 26
Chris Clark Avatar answered Dec 24 '22 00:12

Chris Clark