0

I'm new to C#, with basic knowledge from other languages. And I came up with this problem:

public void startupMessage(string appTitle, string (((color))))
{                                               V--------V
    Console.ForegroundColor = ConsoleColor.(((color)));
}

What I think is some of the problem is that the second parameter has to be something else than string. But I'm not sure.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
m0nsterr
  • 131
  • 1
  • 6
  • 1
    Possible duplicate of [How should I convert a string to an enum in C#?](https://stackoverflow.com/questions/16100/how-should-i-convert-a-string-to-an-enum-in-c) – Camilo Terevinto Apr 16 '18 at 11:47
  • 3
    Why is `color` a `string` and not a `ConsoleColor`? – Patrick Hofman Apr 16 '18 at 11:49
  • It's best to first read through the [documentation](https://msdn.microsoft.com/en-us/library/system.console.foregroundcolor(v=vs.110).aspx) before asking – Gilad Green Apr 16 '18 at 11:55
  • @GiladGreen Would not know what to look for. – m0nsterr Apr 16 '18 at 12:05
  • @PatrickHofman Because I don't want to call startupMessage with ConsoleColor.Blue as a parameter. I just want "Blue". Dmigo answered my question perfectly in his second solution using Enum.Parse. – m0nsterr Apr 16 '18 at 12:08

1 Answers1

5

I see two possible solutions for the problem:
First is to use ConsoleColor as an argument type:

public void startupMessage(string appTitle, ConsoleColor color)
{
   Console.ForegroundColor = color;
}

Second is to parse the argument:

public void startupMessage(string appTitle, string color)
{
    Console.ForegroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), color);
}
dmigo
  • 2,849
  • 4
  • 41
  • 62
  • 4
    Since our answers are basically the same, I've deleted mine and upvoted yours. Seems like you could use the rep more then me. – Zohar Peled Apr 16 '18 at 11:55
  • I tried to upvote yours @zohar it was first. – Jeremy Thompson Apr 16 '18 at 11:57
  • @ZoharPeled Please mark as duplicate instead. No reason to have yet another Enum.Parse duplicate on this site – Camilo Terevinto Apr 16 '18 at 11:57
  • Awesome! That worked! Solution 2 is exactly what I wanted to learn! Probably not as much needed for this project, could have easly used solution 1 aswell. But solution 2 answered my question perfectly. Thanks. – m0nsterr Apr 16 '18 at 11:58
  • 1
    It's not an exact duplicate @Camilo there's a choice of argument in parameter or cast string to enum. – Jeremy Thompson Apr 16 '18 at 11:59
  • @CamiloTerevinto actually, this question is not an exact duplicate. It's unclear from the question alone if the OP wants to parse the enum value from the string or was just wondering what to pass as the color. However, in his comment to my answer, the OP clarified that, so yes, that's the correct duplicate. – Zohar Peled Apr 16 '18 at 12:00
  • @JeremyThompson The OP mentioned in another answer what they wanted, so it is a duplicate – Camilo Terevinto Apr 16 '18 at 12:01
  • @ZoharPeled Then either close as unclear or as duplicate, still no reason to answer. – Camilo Terevinto Apr 16 '18 at 12:01
  • @CamiloTerevinto Please let me decide for myself if I want to answer a question or vote to close. IMHO, There is nothing wrong with answering duplicate questions, and I know that some of the highest ranking members agree with this opinion. Jeff Atwood even [blogged about that.](https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/) – Zohar Peled Apr 16 '18 at 12:06