I am using an enum as my options for a switch statement and it works. The problem is if the user enter a non valid option the program crashes. What should I add so that the default is used?
my enum class
public enum Options : byte
{
Display = 1,
Add,
Toggle,
Max,
Mean,
Medium,
Exit
}
in main my switch statement
string volString = Console.ReadLine();
Options options = (Options)Enum.Parse(typeof(Options), volString);
// this is the line that is giving me the runtime error. Since other options are not found
in the enum the program crashes.
switch (options)
{
case Options.Display: //dispaly regular time
case Options.Toggle://toggle
default:
Console.WriteLine("entry blah blah");
break;
How about: