C
Join ServerC#
help
✅ trying to validate an input
Rrb12/30/2022
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]


MMODiX12/30/2022
The TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
- since C# 7 you can declare a variable that will be used as an
- it forces you to check if the
Avoid: Convert.ToInt32 — it's a bad choice for parsing an
Avoid: int.Parse — you have to use a
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__
- 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
(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
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");
}
Rrb12/30/2022
i've tried doing that
Rrb12/30/2022
i did tryparse but i dont think im doing it right
Rrb12/30/2022
hold on
Rrb12/30/2022
ill show u another example thats probably simpler that also isnt working
Rrb12/30/2022
i think you've helped me on this before with a previous example
Rrb12/30/2022
@Retax this is something that i did before using tryparse that seemed to work

Rrb12/30/2022
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
Rrb12/30/2022
im just not 100% on how to use tryparse in the context of a dowhile loop
Rrb12/30/2022
i have SquareIsValid as a bool var
Rrb12/30/2022
so i need another one to check the input?
Rrb12/30/2022
right
Rrb12/30/2022
what's going in and out
Rrb12/30/2022
i already have an int choice
Rrb12/30/2022

Rrb12/30/2022
i was thinking about another choice variable
Rrb12/30/2022
as a string
Rrb12/30/2022
that i can just put as
Rrb12/30/2022
string choice = "0"
Rrb12/30/2022
i did it on another project
Rrb12/30/2022
wait
Rrb12/30/2022
yea
Rrb12/30/2022
and then i had
Rrb12/30/2022
int intChoice
Rrb12/30/2022
(this is a completely seperate project)*

Rrb12/30/2022
i wanted to replicate something like this
Rrb12/30/2022

Rrb12/30/2022
@Retax little experimenting
Rrb12/30/2022
me too
Rrb12/30/2022
im trying to validate an input
Rrb12/30/2022
my problem is
Rrb12/30/2022
if a user enters a string
Rrb12/30/2022
thats not between 1-3 or 9
Rrb12/30/2022
it just crashes
Rrb12/30/2022

Rrb12/30/2022

Rrb12/30/2022
which part
Rrb12/30/2022
the whole thing?
Rrb12/30/2022
i read the documentation i still dont fully understand how tryparse actually works
Rrb12/30/2022
it literally just converts a string to an integer and gets stored in a boolean?
Rrb12/30/2022

Rrb12/30/2022

Rrb12/30/2022
this is so utterly confusing bro
Rrb12/30/2022
i really appreciate your patience lol
Rrb12/30/2022
okay done that was an easy switch
Rrb12/30/2022
only problem is
Rrb12/30/2022
what do i do with Choice
Rrb12/30/2022
yeah thats the problem i got given this skeleton program and got told to alter it
Rrb12/30/2022
so if i change Choice
Rrb12/30/2022

Rrb12/30/2022
everything else fails
Rrb12/30/2022
aha
Rrb12/30/2022
okay
Rrb12/30/2022
done
Rrb12/30/2022

Rrb12/30/2022
it works
Rrb12/30/2022
thank you my friend i appreciate it
Rrb12/30/2022
its a really weird project that ive been given
Rrb12/30/2022
idk what any of the words mean
AAccord12/31/2022
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.