C
C#3y ago
hills

✅ trying to validate an input

i have this skeleton program for a game that doesnt seem to validate inputs correctly. i dont really know how to correct it [images below]
21 Replies
MODiX
MODiX3y ago
The TryParse pattern is considered best practice of parsing data from a string: - a TryParse method returns true or false to inform you if it succeeded or not, so you can use it directly in a condition, - since C# 7 you can declare a variable that will be used as an out argument inline in an argument list, - it forces you to check if the out argument contains valid data afterwards, Avoid: Convert.ToInt32 — it's a bad choice for parsing an int. It exists only for backwards compatibility reasons and should be considered last resort.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(Note: Convert does contain useful conversion methods: To/FromBase64String, To/FromHexString, ToString(X value, int toBase), ToX(string? value, int fromBase)) Avoid: int.Parse — you have to use a try/catch statement to handle invalid input, which is a less clean solution.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
hills
hillsOP3y ago
i've tried doing that i did tryparse but i dont think im doing it right hold on ill show u another example thats probably simpler that also isnt working i think you've helped me on this before with a previous example
hills
hillsOP3y ago
@Retax this is something that i did before using tryparse that seemed to work
hills
hillsOP3y ago
it was a similar situation i had to validate an input and make sure it was an integer even if the user entered something weird im just not 100% on how to use tryparse in the context of a dowhile loop i have SquareIsValid as a bool var so i need another one to check the input? right what's going in and out i already have an int choice
hills
hillsOP3y ago
hills
hillsOP3y ago
i was thinking about another choice variable as a string that i can just put as string choice = "0" i did it on another project wait yea and then i had int intChoice
hills
hillsOP3y ago
(this is a completely seperate project)*
hills
hillsOP3y ago
i wanted to replicate something like this
hills
hillsOP3y ago
hills
hillsOP3y ago
@Retax little experimenting me too im trying to validate an input my problem is if a user enters a string thats not between 1-3 or 9 it just crashes
hills
hillsOP3y ago
hills
hillsOP3y ago
hills
hillsOP3y ago
which part the whole thing? i read the documentation i still dont fully understand how tryparse actually works it literally just converts a string to an integer and gets stored in a boolean?
hills
hillsOP3y ago
hills
hillsOP3y ago
hills
hillsOP3y ago
this is so utterly confusing bro i really appreciate your patience lol okay done that was an easy switch only problem is what do i do with Choice yeah thats the problem i got given this skeleton program and got told to alter it so if i change Choice
hills
hillsOP3y ago
hills
hillsOP3y ago
everything else fails aha okay done
hills
hillsOP3y ago
hills
hillsOP3y ago
it works thank you my friend i appreciate it its a really weird project that ive been given idk what any of the words mean
Accord
Accord3y 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.

Did you find this page helpful?