Click here to Skip to main content
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 2: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 766
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 25 Oct 2010
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