C
C#3y ago
maroonlol

System.FormatException 'Input string was not in a correct format.' - Correct format is entered

https://github.com/AbdulRaheemNazir/MiniApps/blob/main/Main I have uploaded my code here to display a menu page which it does succesfully. I click option 2 and option 3 and option 4 and have no Issue However when I click option 1 the program ends displaying the question and doesn't allow me to input and gives me the error in the screenshot. However I'm unsure what is causing this error to occur Any assistance would be great Thank you
GitHub
MiniApps/Main at main · AbdulRaheemNazir/MiniApps
Contribute to AbdulRaheemNazir/MiniApps development by creating an account on GitHub.
7 Replies
TheBoxyBear
TheBoxyBear3y ago
Can't find the issue right now, but in the meantime you should rewrite the ReadLine to avoid crashing when the input is incorrect $tryparse
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.
maroonlol
maroonlolOP3y ago
Unfortunatly I have tried that an no luck
maroonlol
maroonlolOP3y ago
maroonlol
maroonlolOP3y ago
I used a try catch but now it displays this
maroonlol
maroonlolOP3y ago
GitHub
MiniApps/Problem Here at main · AbdulRaheemNazir/MiniApps
Contribute to AbdulRaheemNazir/MiniApps development by creating an account on GitHub.
maroonlol
maroonlolOP3y ago
Here is where the problem lies the update file
Want results from more Discord servers?
Add your server