0

Im trying to make a config that can change the color of the console message. Here is the config

"ErrorColor": "Red",

here is before there was no config to change the color.

Console.ForegroundColor = color == ConsoleColor.Black ? ConsoleColor.Red : color;

here is what I tried to add it.

Console.ForegroundColor = color == ConsoleColor.Black ? ConsoleColor.(session.LogicSettings.ErrorColor) : color;

how would my code look like?

Mark
  • 89
  • 8

1 Answers1

2

Parse/TryParse is normally how you would get an enum value from a string, but isn't your json deserialization going to handle that? Anyway it would probably look like this

ConsoleColor color;
if (!Enum.TryParse(session.LogicSettings.ErrorColor, out color))
{
    // this is the fallback color in case an invalid value was entered.
    color = ConsoleColor.Red;
}

Console.ForegroundColor = color;
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • would i keep Console.ForegroundColor = color == ConsoleColor.Black ? ConsoleColor.(session.LogicSettings.ErrorColor) : color; or replace it? – Mark Aug 11 '16 at 01:10
  • @Mark No you wouldn't keep that. That's not even valid C#. This was more of a demonstration on how to use TryParse, not per se something for you to copy and paste. – Mike Zboray Aug 11 '16 at 02:22