-7

I get a string from input ,I want to give it a index like the following:

For example:

string Name = "Jack"

There are 5 possibilities :

Jack   = 1, 
Alice  = 2,
Stiven = 3,
Alex   = 4,
Katrin = 5

when I get Name from input I want to go through this enum class and get the index of it

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
papagallo
  • 145
  • 7

4 Answers4

6

Use a dictionary better, they are exactly meant to store a value corressponding to a given object.

Dictionary<string, int> dict = new Dictionary<string, int>() { 
  {"Jack", 1 }, {"Alice", 2 }, {"Steven", 3 }, {"Alex", 4 }, {"Katrin", 5 }};

string name = "Jack";

int value = dict[name]; // returns 1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
M Bakardzhiev
  • 632
  • 5
  • 13
1

you can try Enum.TryParse, following way :-

string Name="Jack";
enum CustomEnum {
  Jack=1,
  Alice=2,
  Stiven=3,
  Alex=4,
  Katrin=5
}

CustomEnum TheName;
if (Enum.TryParse(Name, out TheName))
{
    switch (TheName) { 
      case Jack: /* code here*/ break;
      case Alice: /* code here*/ break;
      /* and so on */
    }
}
manish
  • 1,450
  • 8
  • 13
1

Try this.

using System;

public class Test {
 enum Names {
  Jack = 1,
   Alice,
   Stiven,
   Alex,
   Katrin
 };
 public static void Main() {

  string Name = "Alex";
  Names eName;
  Enum.TryParse < Names > (Name, out eName);
  Console.WriteLine((int) eName);
 }
}

Demo here : https://ideone.com/ArhV67

Sankar
  • 6,908
  • 2
  • 30
  • 53
1

First declare an enum:

enum Names
{
    Jack = 1,
    Alice = 2,
    Stiven = 3,
    Alex = 4,
    Katrin = 5
}

and then use Enum.TryParse to get the enum that matches "Jack", at the end cast myNames to int to get the desired index :

string Name = "Jack";
Names myNames;
Enum.TryParse(Name, out myNames);
int index = (int)myNames;
Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33
  • You don't need to declare `myNames` earlier, that's the advantage of `out`. You can simply do: `Enum.TryParse(Name, out Names myNames);` – L. Guthardt Apr 06 '18 at 12:34
  • @L.Guthardt I tried it your way but it doesn't work. I'am using Visual Studio 2015 – Slaven Tojić Apr 06 '18 at 12:37
  • `TryParse` does return a `bool` that represents if the parsing did work or not. – Cleptus Apr 06 '18 at 12:42
  • @bradbury9 I know that it returns a bool but i didn't assing it to a variable because i don't have to use it. I use TryParse to avoid exceptions – Slaven Tojić Apr 06 '18 at 12:45
  • @SlavenHvar Have a look at the [MSDN about out](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier). It is possible to instantiate the parameter in the *method parameter section*. – L. Guthardt Apr 06 '18 at 13:50
  • @L.Guthardt your way works only with C# 7. I am using Visual studio 2015 where new features of C#7 don't work. I tried your examples and it won't compile – Slaven Tojić Apr 06 '18 at 13:58
  • @SlavenHvar Ok, I somehow missed that you wrote you are using VS2015. – L. Guthardt Apr 06 '18 at 14:02
  • @L.Guthardt no problem. Thanks for the info anyway. – Slaven Tojić Apr 06 '18 at 14:04