I start external process and after its completion print a message to the user. The message content depends on exit code that this process returns. I have defined this codes in enum:
public enum ExitCode {
Success = 0,
Warning = 1,
Error = 2
}
I call some method only if the process exited with success.
I have 2 approaches. First is to use switch statement
switch (process.ExitCode)
{
case ExitCode.Success:
Console.WriteLine("Success message.");
CallSomeMethod();
break;
case ExitCode.Warning:
Console.WriteLine("Warning message.");
break;
case ExitCode.Error:
Console.WriteLine("Error message.");
break;
default:
Console.WriteLine("Unknown exit code.");
break;
}
Second way is to create dictionary with (StatusCode, messageContent) pairs
if (dictionary.TryGetValue(process.ExitCode, out msg))
{
Console.WriteLine(msg);
if (process.ExitCode == ExitCode.Success)
{
CallSomeMethod();
}
}
else
{
Console.WriteLine("Unknown exit code.");
}
Which version is more elegant? Feel free to reject both and propose better solution.