Click here to Skip to main content
11,477,625 members (73,866 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hey i have Writen a Code in COnsole Application in which user have to Enter a Color name and when it press enter and write something forground color will be the enter color by user.

Console.WriteLine("Enter the Name of Color in Which do You Want to Print");
                  string str = Console.ReadLine();
 
                  Console.ForegroundColor = (ConsoleColor) str;

but it gives error Error:- Cannot convert type 'string' to 'System.ConsoleColor'
any idea how can i convert string to System.ConsoleColor
Posted 25-Oct-10 3:02am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

ShilpaKumario wrote:
Console.ForegroundColor = (ConsoleColor) str;

As you may easily find yourself reading the documentation[^], you should use:

(ConsoleColor) Enum.Parse(typeof(ConsoleColor), str);
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

While several people (4 so far) have answered your question, you'd be better off not doing it this way. There may be typos when you ask users to type in the name of color. A better option would be to give them a list of colors and ask them to choose from that list. I assume the code snippet you showed was for test purposes and that your actual code has some kind of proper UI.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Console.WriteLine("Enter the Name of Color in Which do You Want to Print");
           string str = Console.ReadLine();
           Console.ForegroundColor =(ConsoleColor) Enum.Parse(typeof(ConsoleColor), str,true);
           Console.Read()
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can solve this error by using Enum.Parse property take a look how it will works for you

Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), str);
 // Parse is used to convert value to equivalent enumerated object.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Well, there are various ways to get color for a given string. One of them:
ColorConverter conv = new ColorConverter();
conv.ConvertFromString("Red");
try!
  Permalink  
Comments
MDNadeemAkhter at 25-Oct-10 8:26am
   
ColorConverter class is of System.Drawing namespace and as i know in Console Class System.Drawing namespace is not access by default. thats why not a good answer

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 239
1 Richard Deeming 225
2 Tomas Takac 150
3 Sascha Lefèvre 145
4 _duDE_ 140
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,256
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 25 Oct 2010
Copyright © CodeProject, 1999-2015
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