Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
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 22-Feb-12 1:14am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try This:
 
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();
 
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Parampreet.CCIT at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Dont use enums, use Dictionary<color,> instead
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You might try A Generic enum Parser in C#[^]
 
Hope it helps Smile | :)
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 22 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100