Damien
Damien

Reputation: 59

C# Enums...and user inputs

So my project is to create an enum with the planets, Mecury through Neptune(1-8) for their enum variables. My Book is about 2 pages on Enums and isn't being much for use.

I have to create a program that with an Enum labeled Planets...such as follows

enum Planet
{
    Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}

and prompting the user for an input of a number, 1-8 will display which planet is in that position in the Enum.

static void Main(string[] args)
{

    System.Console.WriteLine("What Planet Are You Looking For? 1-8? ");
    string planet1 = System.Console.ReadLine();
}

I've tried about 3 variations, and all are giving me nothing of what I need, I can display the name, but not at the user input. So I've gone back to scratch.

Upvotes: 1

Views: 9428

Answers (5)

Damien
Damien

Reputation: 59

BenGalluzzo

You nailed it on the head

and for everyone else...I did add Pluto.

Thanks everyone!

Here's the final code!

class Program
{
    enum Planet
    {
        Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto
    }

    static void Main(string[] args)
    {

        Console.WriteLine("What Planet Are You Looking For? Pluto is a planet! 1-9? ");
        string planet1 = System.Console.ReadLine();

        Planet planet = (Planet)Convert.ToInt32(planet1);

        System.Console.WriteLine(planet);


        System.Console.WriteLine();


        System.Console.ReadLine();

Upvotes: 1

steliosbl
steliosbl

Reputation: 8921

You can very simply cast the int to the enum like so:

if (int.TryParse(Console.ReadLine(), out int planetNum))
{
    if (Enum.IsDefined(typeof(Planet), planetNum)
    {
        Planet myPlanet = (Planet)planetNum;
    }
}

You'll notice two checks are performed: We use TryParse to check if the user has entered a valid integer, and Enum.IsDefined to ensure that the integer they have entered corresponds to a planet in the enum.

Alternatively, if you would like to have the user enter the names of the planets instead of numbers, you can use Enum.TryParse:

Enum.TryParse(Console.ReadLine(), out Planet planet1);

Upvotes: 4

BenGalluzzo
BenGalluzzo

Reputation: 52

While this doesn't handle parameter switches properly nor handle errors appropriately, here's one way of basically doing this. Let us know how this works out for you. :)

System.Console.WriteLine("What Planet Are You Looking For? 1-8? ");
string planet1 = System.Console.ReadLine();

Planet planet = (Planet)Convert.ToInt32(planet1);

System.Console.WriteLine(planet);

Upvotes: 1

maccettura
maccettura

Reputation: 10818

You could create a menu for the user, so its easier to see all the options:

Console.WriteLine("What Planet Are You Looking For? 1-8? ");
foreach (Planet planet in Enum.GetValues(typeof(Planet)))
{
    Console.WriteLine("{0}). {1}", (int)planet, planet.ToString());
}

Then try to parse the input as an int

int planetInput = 0;
if(int.TryParse(Console.ReadLine(), out planetInput))
{
    //Successfully parsed
    //Now see if its part of your enum
    if(Enum.IsDefined(typeof(Planet), planetInput))
    {
        Planet selectedPlanet = (Planet)value; 
    }    
}

Upvotes: 0

MistyK
MistyK

Reputation: 6222

Convert to string and then cast to enum.

(Planet)int.Parse(planet1)

Upvotes: 0

Related Questions