+3

C# - checking if input is higher than 0. I need an idea :\

Hi! I made a code that using the user's input to calculate a sqrt without Math.sqrt. The thing is, that the code has to validate if the user's input is not a text and not a negative number. I can manage to check only one or another. How to check both conditions at the same time? Thank you in advance for any help! Here is the code: https://pastebin.com/BXXm5AnX

10/20/2019 1:06:42 AM

DIY Mods

4 Answers

New Answer

+7

Basically, check through the input to see if all are numbers. Then convert it to a numeric type and see if the value is less than 0

+1

DIY Mods Sounds like regex does exactly what you want in one line. https://code.sololearn.com/ce93AeTth5ae/?ref=app

+1

@Prometheus [EXAMS]🇸🇬 Sounds great but doesn't work for some reason

0

Why are you calling double.TryParse and Convert.ToDouble? You already have the value as a double in number. I would just do if (double.TryParse(textBox1.Text, out number) && number >= 0)