I need to write a validation for a textbox which value is < = 2147483647
my code is something like:
Textbox1.Text = "78987162789"
if(Int32.Parse(Textbox1.text) > 2147483647)
{
Messagebox("should not > ")
}
I am getting error message something like: value is too small or too big for Int. How can I fix this?
There's a TryParse method which is better for that purpose.
int Val;
bool ok = Int32.TryParse (Textbox1.Text, out Val);
if (!ok) { ... problem occurred ... }
Integers are stored using 32 bits, so you only have 32 bits with which to represent your data; 31 once you take into account negative numbers. So numbers that are larger than 2^31 - 1 cannot be represented as integers. That number is 2147483647. So since 78987162789 > 2147483648, it cannot convert it to an integer.
Try using a long instead.
Edit:
Of course, long only works up to 9,223,372,036,854,775,807 (2 ^ 63 - 1), so you may end up in the same problem. So, as other people have suggested, use Int32.TryParse - if that fails, you can assume it's not a number, or it's bigger than your limit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With