✅ Console Readline accepting empty value.
Hi all,
I'm trying to set console readline to not accept empty value (""). However, I've tried a bunch of different options and I still get "System.FormatException: 'The input string '' was not in a correct format.'
"
Here is my code
Ideally the goal was to make a loop that displays a message stating that a number needs to be entered.
8 Replies
Im not sure if understood correctly
i think u mean
if (manticoreDistance == string.Empty) {
// handle it
}
I will give this a try to see if it works. I wasn't sure how to explain it
a much simpler option would be to simply use $tryparse btw
When you don't know if a string is actually a number when handling user input, use
int.TryParse
(or variants, e.g. double.TryParse
)
TryParse
returns a bool
, where true
indicates successful parsing.
Remarks:
- Avoid int.Parse
if you do not know if the value parsed is definitely a number.
- Avoid Convert.ToInt32
entirely, this is an older method and Parse
should be preferred where you know the string can be parsed.
Read more herewould be something like
Thank you so much! This works exactly how I wanted it to work! I didn't know you can add Console.ReadLine to the loop condition 😮
glad i could help o7
if ur question is answered dont forget to $close the channel
Use the /close command to mark a forum thread as answered