✅ 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
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. (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.
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__ 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.
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
@Retax this is something that i did before using tryparse that seemed to work
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
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
(this is a completely seperate project)*
i wanted to replicate something like this
@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
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?
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
everything else fails
aha
okay
done
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
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.