I suggest to use a dictionary to map between the input and the command. The simplest solution is to map directly between keys and the command text.
Dictionary<ConsoleKey, String> map = new Dictionary<ConsoleKey, String>()
{
{ ConsoleKey.A, "process image" },
{ ConsoleKey.B, "apply blur effect" },
{ ConsoleKey.C, "save as png" }
};
ConsoleKey key = Console.ReadKey().Key;
String command;
if (map.TryGetValue(key, out command))
{
SendCommand(command);
}
else
{
HandleInvalidInput();
}
Depending on your actual needs it might be a cleaner solution to perform a two step mapping - from input key to an enum value, and from the enum value to the command text. You should also think about creating a command class and providing static instances for your commands.
public class Command
{
public Command(String commandText)
{
this.CommandText = commandText;
}
public String CommandText { get; private set; }
public void Send()
{
// Dummy implementation.
Console.WriteLine(this.CommandText);
}
// Static command instances.
public static readonly Command ProcessImage = new Command("process image");
public static readonly Command BlurImage = new Command("apply blur effect");
public static readonly Command SaveImagePng = new Command("save as png");
}
With this class the code to send the commands would be something like the following.
Dictionary<ConsoleKey, Command> map = new Dictionary<ConsoleKey, Command>()
{
{ ConsoleKey.A, Command.ProcessImage },
{ ConsoleKey.B, Command.BlurImage},
{ ConsoleKey.C, Command.SaveImagePng }
};
ConsoleKey key = Console.ReadKey().Key;
Command command;
if (map.TryGetValue(key, out command))
{
command.Send();
}
else
{
HandleInvalidInput();
}