question

Aaronsoggi-6095 avatar image
0 Votes"
Aaronsoggi-6095 asked Viorel-1 commented

How would i check if the value entered is an int in if statement?

I would've used Try Parse but i don't want to use the value, i just want to check if its an integer or not.


 if (txtCardNumber.TextLength < 16 || txtCardNumber.TextLength > 16)
             {
                 MessageBox.Show("Please ensure that your card number is 16 digits long and in the correct format!");
             }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered Viorel-1 commented

You don't need to declare a var with TryParse, do it like this

Add this class to your project (makes it reusable), it's known as a language extension.

 public static class StringExtensions    
 {
     public static bool IsNumeric(this string text) => double.TryParse(text, out _);
    
 }


Use it

 if (txtCardNumber.Text.IsNumeric())
 {
        
 }

Using double indicates it can represent an int, double essentially a number

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Perfect, thankyou!

0 Votes 0 ·

Do you really accept non-digits inside the Card Number?

1 Vote 1 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered

You do not say what txtCardNumber is but if it is a text box then I assume the relevant string is txtCardNumber.Text. If so then if (txtCardNumber.Text.All(Char.IsDigit)) will be true if all characters are a digit and false otherwise.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.