37

Say that I have variable whose value (for example, "listMovie") is the name of an enum member:

public enum Movies
{
    regMovie = 1,
    listMovie = 2 // member whose value I want
}

In this example, I would like to get the value 2. Is this possible? Here is what I tried:

public static void getMoviedata(string KeyVal)
{
    if (Enum.IsDefined(typeof(TestAppAreana.MovieList.Movies), KeyVal))
    {
        //Can print the name but not value
        ((TestAppAreana.MovieList.Movies)2).ToString(); //list
        Enum.GetName(typeof(TestAppAreana.MovieList.Movies), 2);
    }
}
DavidRR
  • 18,291
  • 25
  • 109
  • 191
Navajyoth CS
  • 968
  • 2
  • 7
  • 15

4 Answers4

78

Assuming that KeyVal is a string representing the name of a certain enum you could do this in the following way:

int value = (int)Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal);
Paweł Bejger
  • 6,176
  • 21
  • 26
  • but the value is inside a variable i can't do like this TestAppAreana.MovieList.Movies.KeyVal – Navajyoth CS Feb 04 '14 at 19:34
  • it still prints listMovie not the value – Navajyoth CS Feb 04 '14 at 19:41
  • 2
    @NavajyothCS it doesn't sound like you're following the instructions here. It should work as he wrote it. – Tim S. Feb 04 '14 at 19:43
  • @bejger yes i tried it but I am getting the same sting as value. I wish I could show you the screen. – Navajyoth CS Feb 04 '14 at 19:45
  • @NavajyothCS: What are you doing with the `int` value once you have it? Are you running it through the same code that you posted, that is: `((TestAppAreana.MovieList.Movies)value).ToString(); Enum.GetName(typeof(TestAppAreana.MovieList.Movies), value);`? – Chris Sinclair Feb 04 '14 at 19:47
  • @bejger Int value is not supporting so I tried this string str= Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal).ToString(); – Navajyoth CS Feb 04 '14 at 19:47
  • 1
    @NavajyothCS: You _must_ include the cast to `int`: `((int)Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal)).ToString();` Maybe try storing it to a local `int` variable first for easy reading/management. – Chris Sinclair Feb 04 '14 at 19:48
  • 1
    Note there's a helpful overload on `Enum.Parse()` that takes a third parameter, `bool ignoreCase = false`. – Aaron Feb 24 '19 at 07:45
14

You want to get the Enum value from the string name. So you can use the Enum.Parse method.

int number = (int)Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal)

You can also try Enum.TryParse to check whether parsing is successful or not.

Movies movie;
if (Enum.TryParse(KeyVal, true, out movie))
{

}
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • it still prints listMovie not the value – Navajyoth CS Feb 04 '14 at 19:41
  • @NavajyothCS are you sure that you are printing `number` variable as in my code, and it is `int` type and can't hold `listMovie` as value. – Sachin Feb 04 '14 at 19:43
  • Int value is not supporting so I tried this string str= Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal).ToString(); – Navajyoth CS Feb 04 '14 at 19:46
  • @NavajyothCS **No it will not work**, You have to cast it in `int` to get the number and then you can convert the `int` into the `string`. – Sachin Feb 04 '14 at 19:48
2

Use:

var val= (int)Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal)
-1

Not sure about past, becasue I'm new to C#, but as of today, this works and gives value of 2:

(int)Movies.listMovie

jarekj9
  • 37
  • 4
  • 3
    This does not really answer the question. The question is asking how to convert a **string value** to the **integer value of an enum value** by enum name, e.g. `"listMovie"` => `Movies.listMovie` => `2`. You are only showing how to convert an **enum value** to an **integer value** which answers only part of the question. – dbc Jun 30 '21 at 17:25