C
C#14mo ago
Mekasu0124

✅ ✅ Issues With Input Validation

public static string? VerifyEmptyOrChanged(string? oldEntry, string newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}
public static string? VerifyEmptyOrChanged(string? oldEntry, string newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}
I'm asking the user for information when they select to update an entry within the database. Their instruction is to just press enter and leave the entry blank if they want to keep the information the same, however, I also need this function to check a date format and a time format (two separate checks) and I can't figure it out. To clarify, The first entry is asking them for an adjusted date, or to leave it empty. When they put in something, it needs to check if that entry is empty or not and whether it matches MM/dd/yyyy format. Then it asks them for the updated start time, or to leave it empty. That needs to check if the entry is empty and whether it matches HH:MM format and same for the third check. Thanks
68 Replies
Mekasu0124
Mekasu0124OP14mo ago
so like
Console.WriteLine("When Editing Information, Leave Blank and Press Enter To Leave The Same");
Console.WriteLine($"Current Date: {currentDate}");
Console.Write("Enter Correct Date: ");

string? newDate = Console.ReadLine();
newDate = UserValidation.VerifyEmptyOrChanged(currentDate, newDate);

Console.WriteLine($"Current Start Time: {currentStartTime}");
Console.Write("Enter Correct Start Time: ");

string? newStartTime = Console.ReadLine();
newStartTime = UserValidation.VerifyEmptyOrChanged(currentStartTime, newStartTime);

Console.WriteLine($"Current End Time: {currentEndTime}");
Console.Write("Enter Correct End Time: ");

string? newEndTime = Console.ReadLine();
newEndTime = UserValidation.VerifyEmptyOrChanged(currentEndTime, newEndTime);
Console.WriteLine("When Editing Information, Leave Blank and Press Enter To Leave The Same");
Console.WriteLine($"Current Date: {currentDate}");
Console.Write("Enter Correct Date: ");

string? newDate = Console.ReadLine();
newDate = UserValidation.VerifyEmptyOrChanged(currentDate, newDate);

Console.WriteLine($"Current Start Time: {currentStartTime}");
Console.Write("Enter Correct Start Time: ");

string? newStartTime = Console.ReadLine();
newStartTime = UserValidation.VerifyEmptyOrChanged(currentStartTime, newStartTime);

Console.WriteLine($"Current End Time: {currentEndTime}");
Console.Write("Enter Correct End Time: ");

string? newEndTime = Console.ReadLine();
newEndTime = UserValidation.VerifyEmptyOrChanged(currentEndTime, newEndTime);
When it prompts and the user enters their information, or leaves it blank and the validation function is ran, it needs to check 1. Is the string empty 2. If not empty, does it match MM/DD/YYYY 3. If not empty, does it match HH:MM and return the string when conditions are met
Pobiega
Pobiega14mo ago
Sounds like you want three different methods instead of just one Since you call them distinctly anyways
Mekasu0124
Mekasu0124OP14mo ago
Yes but no. If it’s possible to have 3 checks in 1 call then id rather go that route tbh. I’m already having to write 100+ lines per file and I’ve only gotten 2/8 menu options completed besides this minor set back. I’d rather not have to write 15 more lines if I don’t have to
Pobiega
Pobiega14mo ago
That's the wrong mindset tbh Methods should be specific rather than broad You certainly can do this with one method, but it'll be harder to reason about Like, it will accept a date when you really expected a time Or opposite
Mekasu0124
Mekasu0124OP14mo ago
Tbh you’re right. I just wanted an easier way of it but frankly 15 lines in the same function or 15 lines between 3 functions are still 15 lines
Pobiega
Pobiega14mo ago
yup And most importantly, you prevent a time from being saved in the date field (or crashing because you expected the value to be parsable when it wasnt)
Mekasu0124
Mekasu0124OP14mo ago
True. Thank you 🥰 ok so I'm having a problem with getting the order of calls right
public static CodeSession StartSessionEdit(CodeSession currentSession)
{
Console.WriteLine("\nWhen Editing Information, If you want it left the same then press ENTER to skip.");
Console.WriteLine($"\nCurrent Date Created: {currentSession.TodaysDate}");
Console.Write("Enter Corrected Date: ");

string? newDate = Console.ReadLine();
newDate = UserValidation.VerifyEmptyOrChanged(currentSession.TodaysDate, newDate);

Console.WriteLine($"\nCurrent Start Time: {currentSession.StartTime}");
Console.Write("Enter Corrected Time: ");

string? newStartTime = Console.ReadLine();
newStartTime = UserValidation.VerifyEmptyOrChanged(currentSession.StartTime, newStartTime);

Console.WriteLine($"\nCurrent End TIme: {currentSession.EndTime}");
Console.Write("Enter Corrected Time: ");

string? newEndTime = Console.ReadLine();
newEndTime = UserValidation.VerifyEmptyOrChanged(currentSession.EndTime, newEndTime);

string? newDuration = Helpers.CalculateDuration(newStartTime, newEndTime);

CodeSession updatedSession = new()
{
Id = currentSession.Id,
TodaysDate = newDate,
StartTime = newStartTime,
EndTime = newEndTime,
Duration = newDuration
};

Console.WriteLine($"New Date: {newDate}");
Console.WriteLine($"New Start Time: {newStartTime}");
Console.WriteLine($"New End Time: {newEndTime}");
Console.WriteLine($"New Duration: {newDuration}");
Console.WriteLine("\n Are You Satisfied With These Changes? Y/N");
Console.Write("Your Selection: ");

string? satisfied = Console.ReadLine().ToLower();
satisfied = UserValidation.ValidateYesNo(satisfied);

if (satisfied == "y")
{
return updatedSession;
}
else
{
Console.WriteLine("Restarting Session Edit.");
return StartSessionEdit(currentSession);
}
}
public static CodeSession StartSessionEdit(CodeSession currentSession)
{
Console.WriteLine("\nWhen Editing Information, If you want it left the same then press ENTER to skip.");
Console.WriteLine($"\nCurrent Date Created: {currentSession.TodaysDate}");
Console.Write("Enter Corrected Date: ");

string? newDate = Console.ReadLine();
newDate = UserValidation.VerifyEmptyOrChanged(currentSession.TodaysDate, newDate);

Console.WriteLine($"\nCurrent Start Time: {currentSession.StartTime}");
Console.Write("Enter Corrected Time: ");

string? newStartTime = Console.ReadLine();
newStartTime = UserValidation.VerifyEmptyOrChanged(currentSession.StartTime, newStartTime);

Console.WriteLine($"\nCurrent End TIme: {currentSession.EndTime}");
Console.Write("Enter Corrected Time: ");

string? newEndTime = Console.ReadLine();
newEndTime = UserValidation.VerifyEmptyOrChanged(currentSession.EndTime, newEndTime);

string? newDuration = Helpers.CalculateDuration(newStartTime, newEndTime);

CodeSession updatedSession = new()
{
Id = currentSession.Id,
TodaysDate = newDate,
StartTime = newStartTime,
EndTime = newEndTime,
Duration = newDuration
};

Console.WriteLine($"New Date: {newDate}");
Console.WriteLine($"New Start Time: {newStartTime}");
Console.WriteLine($"New End Time: {newEndTime}");
Console.WriteLine($"New Duration: {newDuration}");
Console.WriteLine("\n Are You Satisfied With These Changes? Y/N");
Console.Write("Your Selection: ");

string? satisfied = Console.ReadLine().ToLower();
satisfied = UserValidation.ValidateYesNo(satisfied);

if (satisfied == "y")
{
return updatedSession;
}
else
{
Console.WriteLine("Restarting Session Edit.");
return StartSessionEdit(currentSession);
}
}
this is the primary function. When it asks the user for the newDate, it checks to see if the input is empty or not.
public static string? VerifyEmptyOrChanged(string? oldEntry, string newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}

public static bool? VerifyDateInput(string input)
{
while (input != String.Format("MM/dd/yyyy"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: MM/DD/YYYY");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Date: ");

input = Console.ReadLine();
return VerifyDateInput(input);
}

return true;
}

public static bool? VerifyTimeInput(string input)
{
while (input != String.Format("HH:MM"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: HH:MM");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Time: ");

input = Console.ReadLine();
return VerifyTimeInput(input);
}

return true;
}
public static string? VerifyEmptyOrChanged(string? oldEntry, string newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}

public static bool? VerifyDateInput(string input)
{
while (input != String.Format("MM/dd/yyyy"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: MM/DD/YYYY");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Date: ");

input = Console.ReadLine();
return VerifyDateInput(input);
}

return true;
}

public static bool? VerifyTimeInput(string input)
{
while (input != String.Format("HH:MM"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: HH:MM");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Time: ");

input = Console.ReadLine();
return VerifyTimeInput(input);
}

return true;
}
these are the validation functions that I created for checking if the string is empty, or not. Check if it's the correct date format and time format. So since the first question deals with the date, how would I do the call order to check if the string is empty or not, and if not then check it's date format?
Pobiega
Pobiega14mo ago
i uh... what those methods are... what? looped AND recursive AND returns bool? when it never returns null? this is confusing
Mekasu0124
Mekasu0124OP14mo ago
ok I'll fix it
Pobiega
Pobiega14mo ago
your null annotations are all wrong in general
public static string? VerifyEmptyOrChanged(string? oldEntry, string newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}
public static string? VerifyEmptyOrChanged(string? oldEntry, string newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}
newEntry is flagged as never being null, but you check it for null so I suggest you mark it as string? newEntry
Mekasu0124
Mekasu0124OP14mo ago
yep. I already fixed that the string? newEntry part that is
Pobiega
Pobiega14mo ago
right. I'm trying to wrap my head around your original issue now. Ah okay I see it.
Mekasu0124
Mekasu0124OP14mo ago
so it checks if the newEntry is empty or if the new entry matches the current entry. If it is empty or does match, it returns the current entry. Otherwise, it returns the new entry
Pobiega
Pobiega14mo ago
you need the "allow null/empty to keep same value", while still validating dates/times my gut instinct is to use higher order functions, but that might be... hard? its the idea of passing functions as parameters to other functions
Mekasu0124
Mekasu0124OP14mo ago
I haven't learned that yet
Pobiega
Pobiega14mo ago
Would you be okay with learning it now, or prefer a simpler approach? also, just so I can see all the types involved here can you show me the CodeSession class?
Mekasu0124
Mekasu0124OP14mo ago
and I'm not entirely sure on how to go about that. There are 3 entry inputs. newDate, newStartTime, and newEndTime. 1. all 3 check for if the newEntry is empty or matches the oldEntry so that if the newEntry is empty or matches the oldEntry, it can keep the same value that's currently recorded in the database. 2. newDate needs to check against the mm/dd/yyyy format to follow the requirements You should tell the user the specific format you want the date and time to be logged and not allow any other format. 3. newStartTime and newEndTime needs to check against the HH:MM format to follow the same requirements. The 3 are checked against the String.Format()'s in the event the user wants to change the information and doesn't leave the newEntry blank
public class CodeSession
{
public int Id { get; set; }
public string TodaysDate { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Duration { get; set; }
}
public class CodeSession
{
public int Id { get; set; }
public string TodaysDate { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Duration { get; set; }
}
they're all strings except the Id because of the Tip on the projects page Sqlite doesn't support dates. We recommend you store the datetime as a string in the database and then parse it using C#. You'll need to parse it to calculate the duration of your sessions.
Pobiega
Pobiega14mo ago
okay The simplest way to do this is to have your Date/Time validations allow a blank string ie, they allow valid times/dates or blank string so "" is a "valid time" and would mean keep the old time you could then call it like updatedSession.StartTime = VerifyEmptyOrChanged(currentSession.StartTime, VerifyTimeInput(input)); so as long as the user is trying to use a non-blank value, its validated but if its blank, we just say "oh you wanted to keep the old value, thats fine" that solves your "order of operations" problem input != String.Format("MM/dd/yyyy") isnt valid code btw, as I'm sure you know or actually it might be, but it doesnt do what you think it does
Mekasu0124
Mekasu0124OP14mo ago
so then what's the better way to write it
Pobiega
Pobiega14mo ago
the format check?
Mekasu0124
Mekasu0124OP14mo ago
yea if it doesn't do what I think it's doing then how am I supposed to write it?
Mekasu0124
Mekasu0124OP14mo ago
public static string? VerifyDateInput(string input)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "MM/dd/yyyy";
DateTimeStyles style = DateTimeStyles.None;
DateTime result;

while (!DateTime.TryParseExact(input, format, provider, style, out result))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: MM/DD/YYYY");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Date: ");

input = Console.ReadLine();
return VerifyDateInput(input);
}

return result;
}
public static string? VerifyDateInput(string input)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "MM/dd/yyyy";
DateTimeStyles style = DateTimeStyles.None;
DateTime result;

while (!DateTime.TryParseExact(input, format, provider, style, out result))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: MM/DD/YYYY");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Date: ");

input = Console.ReadLine();
return VerifyDateInput(input);
}

return result;
}
so like this
Pobiega
Pobiega14mo ago
Something like that try it out you still have the weird loop+recursion thing thou just remove the recursion?
Mekasu0124
Mekasu0124OP14mo ago
yes because while the input isn't what it's supposed to be, it's to prompt the user for another attempt
Pobiega
Pobiega14mo ago
your loop body does that you gain nothing by calling the method again brb 15-20 min
Mekasu0124
Mekasu0124OP14mo ago
if their first input doesn't match, then it triggers the while statement and then they enter another input which triggers the while loop again as the while loop is the validation check
Pobiega
Pobiega14mo ago
The loop is all you need
Mekasu0124
Mekasu0124OP14mo ago
ok I removed the return statement
Pobiega
Pobiega14mo ago
Just remove the recursive call inside the loop I'd also urge you to use the debugger more
Mekasu0124
Mekasu0124OP14mo ago
I do use the debugger I just couldn't figure out the order of calls
public static string? VerifyDateInput(string input)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "MM/dd/yyyy";
DateTimeStyles style = DateTimeStyles.None;
DateTime result;

while (!DateTime.TryParseExact(input, format, provider, style, out result))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: MM/DD/YYYY");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Date: ");

input = Console.ReadLine();
}

return result.ToString();
}
public static string? VerifyDateInput(string input)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "MM/dd/yyyy";
DateTimeStyles style = DateTimeStyles.None;
DateTime result;

while (!DateTime.TryParseExact(input, format, provider, style, out result))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be In Format: MM/DD/YYYY");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter Corrected Date: ");

input = Console.ReadLine();
}

return result.ToString();
}
here's where I'm at so with testing it, it works so now I have
// in file A
public static CodeSession StartSessionEdit(CodeSession currentSession)
{
Console.WriteLine();
Console.WriteLine();
Console.Write("Enter Corrected Date: ");
string? new Date = Console.ReadLine();
newDate = UserValidation.VerifyEmptyOrChanged(currentSession.TodaysDate, USerValidation.VerifyDateInput(newDate));
}

// in file B
public static string? VerifyDateInput(string? input)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "MM/dd/yyyy";
DateTimeStyles style = DateTimeStyles.None;
DateTime result;

while (!DateTime.TryParseExact(input, format, provider, style, out result))
{
Console.WriteLine();
Console.Write("Enter Corrected Date: ");
input = Console.ReadLine();
}
return result.ToString();
}
// in file A
public static CodeSession StartSessionEdit(CodeSession currentSession)
{
Console.WriteLine();
Console.WriteLine();
Console.Write("Enter Corrected Date: ");
string? new Date = Console.ReadLine();
newDate = UserValidation.VerifyEmptyOrChanged(currentSession.TodaysDate, USerValidation.VerifyDateInput(newDate));
}

// in file B
public static string? VerifyDateInput(string? input)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "MM/dd/yyyy";
DateTimeStyles style = DateTimeStyles.None;
DateTime result;

while (!DateTime.TryParseExact(input, format, provider, style, out result))
{
Console.WriteLine();
Console.Write("Enter Corrected Date: ");
input = Console.ReadLine();
}
return result.ToString();
}
the problem I have now, and yes I tested it before writing this, is that if I just press enter to leave the prompt empty, it doesn't allow for the string to be empty. So now the user has to input a date even if it's the same date to keep the same value so how can I edit the validation function to allow the string to be empty as well?
MODiX
MODiX14mo ago
Pobiega
The simplest way to do this is to have your Date/Time validations allow a blank string
Quoted by
React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
Im sure you can figure that out
Mekasu0124
Mekasu0124OP14mo ago
so wrap it in an if statement to check if null or empty and if not then run validation loop
Pobiega
Pobiega14mo ago
no
Mekasu0124
Mekasu0124OP14mo ago
remove the ? from the parameter?
Pobiega
Pobiega14mo ago
how does that help?
Mekasu0124
Mekasu0124OP14mo ago
I guess it doesn't. was just a thought
Pobiega
Pobiega14mo ago
what part of your validation method is doing the actual validation? like, what is the condition that is being checked and decides if an input is valid or not?
Mekasu0124
Mekasu0124OP14mo ago
the while loop
Pobiega
Pobiega14mo ago
yep
Mekasu0124
Mekasu0124OP14mo ago
so change it to say while (!DateTime.TryParseExact(input, format, provider, style, out result) && !String.IsNullOrEmpty())
Pobiega
Pobiega14mo ago
almost
Mekasu0124
Mekasu0124OP14mo ago
well input belongs in the second check
Pobiega
Pobiega14mo ago
yes 🙂
Mekasu0124
Mekasu0124OP14mo ago
ok so then question nevermind
public static string? VerifyEmptyOrChanged(string? oldEntry, string? newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}
public static string? VerifyEmptyOrChanged(string? oldEntry, string? newEntry)
{
if (String.IsNullOrEmpty(newEntry) || newEntry == oldEntry)
{
return oldEntry;
}
else
{
return newEntry;
}
}
so this function is ok as it is then, yea?
Pobiega
Pobiega14mo ago
pretty much
Mekasu0124
Mekasu0124OP14mo ago
ok thank you ❤️ last question for a while I promise
Mekasu0124
Mekasu0124OP14mo ago
public static void DeleteSession()
{
Console.Clear();

List<CodeSession> allSessions = LogQueries.GetAllCodingSessions();
var tableData = new List<List<object>>();

foreach (CodeSession session in allSessions)
{
var item = new List<object>()
{
session.Id, session.TodaysDate, session.StartTime, session.EndTime, session.Duration
};

tableData.Add(item);
}

ConsoleTableBuilder
.From(tableData)
.WithColumn("Id", "Date Created", "Start Time", "End Time", "Duration")
.ExportAndWrite();

Console.WriteLine("\nTo Select An Entry To Delete, Enter It's ID Number");
Console.Write("Your Selection: ");

string? input = Console.ReadLine();
int? selectedId = UserValidation.ValidateNumericInput(input);
selectedId = UserValidation.ValidateIdSelection(selectedId, allSessions);

CodeSession currentSession = LogQueries.GetCodeSession(selectedId);

Console.WriteLine("Are You Sure You Want To Delete This Entry? Y/N");
Console.Write("Your Selection: ");

string? selectedAction = UserValidation.ValidateYesNo(input);

if (selectedAction == "y")
{
LogQueries.DeleteData(currentSession.Id);
Helpers.FinishedCodingSession(currentSession, "Deleted");
}
else
{
Console.WriteLine("You Entered No. Press ENTER To Go Back To Main Menu.");
Console.ReadLine();
Console.Clear();
MainMenu.ShowMenu();
}
}
public static string? ValidateYesNo(string? input)
{
List<string> options = new() { "y", "n" };

while (!options.Contains(input))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be Y or N");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Your Selection: ");

input = Console.ReadLine();
return ValidateYesNo(input);
}

return input;
}
public static void DeleteSession()
{
Console.Clear();

List<CodeSession> allSessions = LogQueries.GetAllCodingSessions();
var tableData = new List<List<object>>();

foreach (CodeSession session in allSessions)
{
var item = new List<object>()
{
session.Id, session.TodaysDate, session.StartTime, session.EndTime, session.Duration
};

tableData.Add(item);
}

ConsoleTableBuilder
.From(tableData)
.WithColumn("Id", "Date Created", "Start Time", "End Time", "Duration")
.ExportAndWrite();

Console.WriteLine("\nTo Select An Entry To Delete, Enter It's ID Number");
Console.Write("Your Selection: ");

string? input = Console.ReadLine();
int? selectedId = UserValidation.ValidateNumericInput(input);
selectedId = UserValidation.ValidateIdSelection(selectedId, allSessions);

CodeSession currentSession = LogQueries.GetCodeSession(selectedId);

Console.WriteLine("Are You Sure You Want To Delete This Entry? Y/N");
Console.Write("Your Selection: ");

string? selectedAction = UserValidation.ValidateYesNo(input);

if (selectedAction == "y")
{
LogQueries.DeleteData(currentSession.Id);
Helpers.FinishedCodingSession(currentSession, "Deleted");
}
else
{
Console.WriteLine("You Entered No. Press ENTER To Go Back To Main Menu.");
Console.ReadLine();
Console.Clear();
MainMenu.ShowMenu();
}
}
public static string? ValidateYesNo(string? input)
{
List<string> options = new() { "y", "n" };

while (!options.Contains(input))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be Y or N");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Your Selection: ");

input = Console.ReadLine();
return ValidateYesNo(input);
}

return input;
}
this is the function that gives the user a list of items in the database and asks them for the id of which one they want to delete and then a simple yes or no for confirmation before deleting. When I test this function, everything does fine except for where it asks the user Yes or No. When it gets to this part, it immediately goes straight to the error. What do I have wrong?
No description
Pobiega
Pobiega14mo ago
Y is not the same as y also.. uh shouldn't this method return bool? and you have the recursion thing again here is my helper method for "yes/no" prompts
public static bool YesOrNo(string prompt)
{
while (true)
{
Console.Write(prompt);
var response = Console.ReadLine();

switch (response?.ToLowerInvariant())
{
case "y" or "yes" or "true":
return true;
case "n" or "no" or "false":
return false;
default:
Console.WriteLine("Invalid format. Try again.");
break;
}
}
}
public static bool YesOrNo(string prompt)
{
while (true)
{
Console.Write(prompt);
var response = Console.ReadLine();

switch (response?.ToLowerInvariant())
{
case "y" or "yes" or "true":
return true;
case "n" or "no" or "false":
return false;
default:
Console.WriteLine("Invalid format. Try again.");
break;
}
}
}
for inspiration
Mekasu0124
Mekasu0124OP14mo ago
you're right here. I just went through the entire validation file and removed all recurrsions within the while loops that were occuring true. I've changed it to string? selectedAction = Console.ReadLine().ToLower();
Pobiega
Pobiega14mo ago
👍
Mekasu0124
Mekasu0124OP14mo ago
I supposed it could. Not a bad idea
Pobiega
Pobiega14mo ago
I'd probably do that inside the method btw makes it easier to use the method
Mekasu0124
Mekasu0124OP14mo ago
I just realized my mistake too I have the validation set equal to where the selected action is, but never prompt the user for the initial input
Pobiega
Pobiega14mo ago
😄
Mekasu0124
Mekasu0124OP14mo ago
this is pretty neat. I didn't know about ToLowerInvariant to allow a check for capitolized or not
Pobiega
Pobiega14mo ago
ToLowerInvariant is just like ToLower, but ignores your current culture its just a habit I have I like switches like this thou and the pattern matching you can do is nice
Mekasu0124
Mekasu0124OP14mo ago
public static bool ValidateYesNo()
{
while (true)
{
Console.WriteLine("Are You Sure You Want To Delete This Entry? Y/N");
Console.Write("Your Selection: ");

string? input = Console.ReadLine();

switch (input?.ToLowerInvariant())
{
case "y" or "yes" or "true":
return true;
case "n" or "no" or "false":
return false;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be 'Y', 'Yes', 'N', or 'No'");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Your Selection: ");

input = Console.ReadLine();
break;
}
}
}
public static bool ValidateYesNo()
{
while (true)
{
Console.WriteLine("Are You Sure You Want To Delete This Entry? Y/N");
Console.Write("Your Selection: ");

string? input = Console.ReadLine();

switch (input?.ToLowerInvariant())
{
case "y" or "yes" or "true":
return true;
case "n" or "no" or "false":
return false;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Input Must Be 'Y', 'Yes', 'N', or 'No'");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Your Selection: ");

input = Console.ReadLine();
break;
}
}
}
ok so not going to lie. It looked a lot smarter than what I had so I used most of it. Hope that's ok. My question is, if neither case happens, like the user enters Dog or cat instead of y, yes, n, or no. What makes it iterate again and ask for another input like the while loops?
Pobiega
Pobiega14mo ago
yep! while(true) loops forever so the only way out is to type one of the valid cases, so the return statements trigger
Mekasu0124
Mekasu0124OP14mo ago
and the break makes it iterate again that makes sense
Pobiega
Pobiega14mo ago
the break is related to the switch not the while
Mekasu0124
Mekasu0124OP14mo ago
oh ok maybe I didn't quite grasp it then 😂 while loop lasts forever correct condition between the two case statements returns out of the switch and breaks the loop as well? so if the switch case doesn't return out, then the loop never breaks
Pobiega
Pobiega14mo ago
return is always "out of the method" only methods can return so return stops the entire method
Mekasu0124
Mekasu0124OP14mo ago
right and break allows the rest of the code to continue as it only breaks out of the switch statement itself and not the function as a whole
Pobiega
Pobiega14mo ago
yes so it hits the while loop again
Mekasu0124
Mekasu0124OP14mo ago
that's pretty dope coolGuns
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server