Question Beginner enum use, is there a better way?

robwong81

New member
Joined
Sep 19, 2019
Messages
2
Programming Experience
Beginner
I took C# in college back in 2010 and wanted to get back into programming so I have been self teaching again. I wrote the following code working through a tutorial on enum but I wanted to figure out how to convert input string to integer so I can pass the argument to a switch command. I wrote the code until I got it to work then searched the internet for solutions and it seems my solution is not really common so just looking for some feedback on if this is a bad way to complete the task I was going for or not.

Thanks to anyone willing to give input. Remember I am relearning and do not program for a career, although that would be a dream.

Code for console app:
using System;
namespace Program
{
    public class TreeSales
    {
        public enum Tree
        {
            Apple = 1,
            Oak,
            Orange
        }
        static void Main()
        {
            Tree treeType = Tree.Apple;
            Console.WriteLine("Please pick an option from below on what type of tree you would like information about:");
            Console.WriteLine("Options:\n" +
                "1.Apple\n" +
                "2.Oak\n" +
                "3.Orange");

            string conInput = Console.ReadLine();
            Convert.ToInt32(conInput);

            if (Convert.ToInt32(conInput) == 1)
            {
                treeType = Tree.Apple;
            }
            else if (Convert.ToInt32(conInput) == 2)
            {
                treeType = Tree.Oak;
            }
            else if (Convert.ToInt32(conInput) == 3)
            {
                treeType = Tree.Orange;
            }

            switch (treeType)
            {
                case Tree.Apple:
                    Console.WriteLine("Apple");
                    break;
                case Tree.Oak:
                    Console.WriteLine("Oak");
                    break;
                case Tree.Orange:
                    Console.WriteLine("Orange");
                    break;
            }
        }
    }
}
 
Here's one way to do things:
C#:
using System;
namespace Program
{
    public class TreeSales
    {
        public enum Tree
        {
            Apple = 1,
            Oak,
            Orange
        }

        static void Main()
        {
            Console.WriteLine("Please pick an option from below on what type of tree you would like information about:");
            foreach (var value in Enum.GetValues(typeof(Tree)))
                Console.WriteLine(value);

            string input = Console.ReadLine();
            Tree choice;
            if (Enum.TryParse<Tree>(input, true, out choice))
                Console.WriteLine(choice);
            else
                Console.Error.WriteLine("Invalid choice.");
        }
    }
}

In the code above the user doesn't even have to pick a value from 1 through 3. The names of the various enum choices are printed out on lines 16-17. Line 21 parses the string entered by the user. Line 22 prints out the user's choice.
 
And in the case when the enum's name isn't representative enough of the display string associate with it, (ex. "Joshua Tree"), then you'll really need to have the user enter a value, and you'll need to remap that value back into an enum. Fortunately, you can cast an enum to an integer (or other base integral type) and back again. Here's an example:
C#:
using System;
using System.Collections.Generic;

namespace Program
{
    public class TreeSales
    {
        public enum Tree
        {
            Apple = 1,
            Oak,
            Orange,
            JoshuaTree
        }

        static Dictionary<Tree, string> TreeNames = new Dictionary<Tree, string>()
        {
            [Tree.Apple] = "Apple",
            [Tree.Oak] = "Oak",
            [Tree.Orange] = "Orange",
            [Tree.JoshuaTree] = "Joshua Tree",
        };

        static void Main()
        {
            Console.WriteLine("Please pick an option from below on what type of tree you would like information about:");
            foreach (Tree value in Enum.GetValues(typeof(Tree)))
            {
                var intValue = (int)value;
                Console.WriteLine("{0}. {1}", intValue, TreeNames[value]);
            }

            string input = Console.ReadLine();
            int choice;
            if (int.TryParse(input, out choice))
            {
                if ((int)Tree.Apple <= choice && choice <= (int)Tree.JoshuaTree)
                {
                    Tree treeChoice = (Tree)choice;
                    Console.WriteLine(TreeNames[treeChoice]);
                }
                else
                {
                    Console.Error.WriteLine("Invalid choice.");
                }
            }
            else
            {
                Console.Error.WriteLine("Not an integer.");
            }
        }
    }
}

Notice the casting from enum to int that is happening on lines 29 and 37, and the casting from int to enum on line 39.

Also in play in the code above is the use of a Dictionary. The Tree enum is used as a key into the dictionary to get the dictionary value -- the display name. (See lines 30 and 40.
 
Console.WriteLine("{0}. {1}", intValue, TreeNames[value]);
...
Also in play in the code above is the use of a Dictionary. The Tree enum is used as a key into the dictionary to get the dictionary value -- the display name. (See lines 30 and 40.
You can simply do value.ToString() to get the string value.
 
Not for "Joshua Tree" which has a space.
 
Oh, I missed that altogether. One could here use for example DescriptionAttribute to decorate custom strings.
 
Back
Top Bottom