C
C#2y ago
cgxlm

Validation help

Need help with some simple validation
20 Replies
cgxlm
cgxlm2y ago
I realize whats wrong Im just not certain on how I fix it if i enter a letter this line obviously cant convert it double isales = Convert.ToDouble(sales); How do i fix
Angius
Angius2y ago
$tryparse
MODiX
MODiX2y 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.
Angius
Angius2y ago
This method was made for this explicit reason It checks if your string can be converted into a given numeric type, and if so, converts it
cgxlm
cgxlm2y ago
im finding it a bit confusing to understand, where would i put this in my code and what would i need to change
Angius
Angius2y ago
You'd use it instead of your convert
cgxlm
cgxlm2y ago
Ahhh I see ive got it in now and its working thank you
cgxlm
cgxlm2y ago
cgxlm
cgxlm2y ago
How would I return overtime so I can use it down there @Angius
Angius
Angius2y ago
result is your parsed number
cgxlm
cgxlm2y ago
yeah but how do i still get it down to use there because changing overtime to result it still cant reach it
Angius
Angius2y ago
You're trying to use the variable in the outermost scope So you need to declare it in the outermost scope And instead of out var result in your tryparse, you can then use out thatOuterVariable
cgxlm
cgxlm2y ago
so do i put int overtimetotal = (overtime * 15); inside of the parse one
Angius
Angius2y ago
Try and see
cgxlm
cgxlm2y ago
Angius
Angius2y ago
Well, that's not how you do it, then $scopes
MODiX
MODiX2y ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
cgxlm
cgxlm2y ago
im confused so where abouts do i put it
Angius
Angius2y ago
You're trying to use the overtimetotal variable in the outermost scope So you need to declare it in the outermost scope
int foo = 0;
{
{
{
{
foo += 10;
}
}
}
}
Console.WriteLine(foo);
int foo = 0;
{
{
{
{
foo += 10;
}
}
}
}
Console.WriteLine(foo);
That's the tl;dr
cgxlm
cgxlm2y ago
ohhhhhhhh isee now thank you so much taking me a while to wrap my head around it lol
Want results from more Discord servers?
Add your server
More Posts