Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get enum int value by string

Tags:

c#

enums

I have implemented the enum below:

public enum CaseOriginCode
{
    Web = 0,
    Email = 1,
    Telefoon = 2
}

I would like to get the enum int value by a string. Something like this:

public void setCaseOriginCode(string caseOriginCodeParam)
{
    int caseOriginCode = CaseOriginCode.GetEnumByString(caseOriginCodeParam);
    // do something with this integer
}

So my input is a string and my output needs to be an int.

Or is it better to implement an dictionary with an key value. The key will be the string and the will be an int.

like image 640
Ola Avatar asked Jun 03 '13 07:06

Ola


1 Answers

Please try with the below code snippet.

public void setCaseOriginCode(string CaseOriginCode)
{
    int caseOriginCode = (int)(CaseOriginCode)Enum.Parse(typeof(CaseOriginCode), CaseOriginCode);
}

Let me know if any concern.

like image 113
Jayesh Goyani Avatar answered Oct 21 '22 19:10

Jayesh Goyani