static void Main(string[] args) { try { Console.WriteLine("Please enter the current day of the week: "); DaysOfTheWeek day = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), Console.ReadLine(), true); // Use true boolean to ignore case. Console.WriteLine("You put in " + day); //string userString = Console.ReadLine().ToLower(); //Console.WriteLine(); //Enum.Parse(typeof(DaysOfTheWeek), userString); //Check to see if user input is found in Enum. //DaysOfTheWeek userInputEnum; //Declare enum. //for (int i = 0; i < 7; i++) //use For Loop to find which enum value the user input matches. //{ // if (userString == GetDayName(i)) // { // userInputEnum = (DaysOfTheWeek)i; // Console.WriteLine("The day you entered, as an Enum data type, is: "); // Console.WriteLine(userInputEnum); // } //} } catch (Exception) { Console.WriteLine("Please enter an actual day of the week."); } Console.Read(); }
static void Main(string[] args) { try { Console.WriteLine("Please enter the current day of the week."); string currentDay = Console.ReadLine(); DaysOfTheWeek day = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), currentDay.ToLower()); } catch (System.ArgumentException) { Console.WriteLine("Please enter an actual day of the week."); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Please enter the current day of the week:"); var entry = Console.ReadLine(); Start: try { DaysOfTheWeek day = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), entry); } catch (System.ArgumentException) { Console.WriteLine("Please enter an actual day of the week."); entry = Console.ReadLine(); goto Start; } DaysOfTheWeek value = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), entry); Console.WriteLine(value); Console.ReadLine(); }
static void Main(string[] args) { DaysOfTheWeek today = new DaysOfTheWeek(); Console.WriteLine("Please enter the current day of the week"); string dayEntered = Console.ReadLine(); DayOfTheWeek output = new DayOfTheWeek(); try { output = (DayOfTheWeek)Enum.Parse(typeof(DayOfTheWeek), dayEntered); } catch (ArgumentException ex) { Console.WriteLine("Please enter an actual day of the week."); } Console.ReadLine(); }
public static string GetDayName(int DayIndex) // Convert enum values to strings. { DaysOfTheWeek DayName = (DaysOfTheWeek)DayIndex; return(DayName.ToString()); }