Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hiiiiiiiiiiiiiiii...

I want in this program that the user enter the color name and the compiler shows the color and color's code like that output:

"The color you have chosen is blue and the color code is 101010."

please give me some suggesstions that how can i do it

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            color skyblue = color.blue;
            int code = (int)Enum.Parse(typeof(color), skyblue.ToString());


            if (code == 101010)
            {
                Console.WriteLine("The color you have chosen is " + skyblue + " and the color code is {0}.",code.ToString());
                Console.ReadKey();
            }
        }
        public enum color
        {
            blue = 101010,
            black = 000000,
            white = 111111
        }
    }
}
Posted

Try This:

C#
static void Main(string[] args)
        {
            string c=Console.ReadLine();

            color skyblue = (color)Enum.Parse(typeof(color), c);

            int code = (int)Enum.Parse(typeof(color), skyblue.ToString());



            Console.WriteLine("The color you have chosen is " + skyblue + " and the color code is {0}.",code.ToString());
            Console.ReadKey();

        }
 
Share this answer
 
This[^] is a good example where you can show different colors in a console window.

The key is to use Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, name); appropriately.
 
Share this answer
 
Comments
Parampreet.CCIT 22-Feb-12 6:28am    
in that program i gave the color name and code in the coding
but i wanna the user enter the color name and the compiler shows the color name and color's code
Dont use enums, use Dictionary<color,> instead

C#
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            populateColors();
           //get user input here inside variable code


            if (code == 101010)
            {
                Console.WriteLine("The color you have chosen is " + colorMap[code] + " and the color code is {0}.",code.ToString());
                Console.ReadKey();
            }
        }
       Dictionary<int,> colorMap = new Dictionary<int,>();

       void populateColors()
      {
           colorMap[101010] = Color.Blue; //add more here
      }

    }
}

NOTE: i have not compiled it. it is just pseudo code.
 
Share this answer
 
You might try A Generic enum Parser in C#[^]

Hope it helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900