0

I dont know why this so hard no..

I have to check is the user input in Enum.

Now i have two different methods, which tell (False or True) is input in the Enum. I do not want show those things to user, i just want program to print like:

"Your input "user input here" found in Car makers list with name "Audi"

using static System.Console;

namespace EnumAutomerkit
{
    enum Automerkit
    {
        Audi = 100,
        BMW = 101,
        Citroen = 102,
        Mercedes_Benz = 103,
        Skoda = 104,
        Toyota = 105,
        Volkswagen = 106,
        Volvo = 107
    }


    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("AUTOMERKIT");

            foreach (int luku in Enum.GetValues(typeof(Automerkit)))
                WriteLine($"{luku} = {Enum.GetName(typeof(Automerkit), luku)}");

            while (true)
            {
                Console.Write("Valitse automerkki joko numerolla tai nimellä (tyhjä lopettaa): ");  //"Choose car maker with number or name (empty breaks): "
                string userinput = Console.ReadLine();

                int.TryParse(userinput, out int userinput2);//muuttaa annetun stringin int olioksi

                Console.WriteLine("{0}: {1}", userinput2, Enum.IsDefined(typeof(Automerkit), userinput2)); //this can check is the number user gave in enum
                Console.WriteLine("{0}: {1}", userinput, Enum.IsDefined(typeof(Automerkit), userinput)); //this can check is the text or chars user gave in enum


                if (userinput == "")
                {
                    break;
                }
            }
        }
    }
}
1
  • You can use Enum.GetName(typeof(Automerkit), luku) to get the name for an enum value. Commented Dec 8, 2019 at 12:05

3 Answers 3

3

An enum value is automatically converted to an enum name when converted to string. Therefore you can write

foreach (Automerkit enumValue in Enum.GetValues(typeof(Automerkit))) {
    WriteLine($"{(int)enumValue} = {enumValue}");
}

Note that the foreach statement automatically converts the element type (object returned by GetValues here) to the type of the iteration variable (Automerkit).

For more information, see The foreach statement section of the C# language specification. The parameter of the WriteLine uses String interpolation in C# (Microsoft Docs).

Sign up to request clarification or add additional context in comments.

4 Comments

GetValues returns System.Array of the type passed into instead of object thereby not converting.
@snr, No. Try to use foreach (var enumValue in Enum.GetValues(typeof(Automerkit))) { ... with a var and you will see that enumValue will be inferred as object. (Note that the code still works with var, so converting the object to the enum type is not required but better reflects the intent of the programmer). Note, even if the array is an Automerkit[], it is returned as System.Array. Therefore you have to cast either the whole array or its elements if you want an object explicitly typed as Automerkit.
Sir, at first, thank your effort and nice explanation but Enum.GetValues(typeof(Automerkit)) returns type of Automerkit[]. enumValue is of type object. Do you mean the type is cast to Automerkit type implicitly? (object -> Automerkit). Would you mind glancing at i.sstatic.net/BWJ39.png?
See the definition of Enum.GetValues(Type) Method. It returns an Array Class having an indexer Array.IList.Item[Int32] Property returning an object. The runtime type of the items is Automerkit, but the static type at compile time is object. To see the real object you must cast it.
2

You can use the Enum.GetNames function and then parse the name to get the int value.

foreach (var merkkiluettelo in Enum.GetNames(typeof(Automerkit)))
{
    Enum.TryParse(merkkiluettelo, out Automerkit a);
    Console.WriteLine($"{(int)a} = {merkkiluettelo}");
}

Comments

1

You can use the GetName method to get the name of the enum value associated with its underlying value (there are other methods too, see this post). You only need one foreach loop, and you can use string interpolation to print in the format value = name:

foreach (int rawValue in Enum.GetValues(typeof(Automerkit))) {
    WriteLine($"{rawValue} = {Enum.GetName(typeof(Automerkit), rawValue)}");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.