3

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?

1
  • Consider using constants, such as int32.MaxValue instead of hardcoding them. Commented Nov 18, 2009 at 4:41

5 Answers 5

5

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 ... }
Sign up to request clarification or add additional context in comments.

1 Comment

@paxdiablo - i want to up vote also but i don't have points. Thanks for your answer.
3

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.

5 Comments

And when the user enters 3742643647634537643465346534634535453563453563635353 into the textbox?
I hope that randomly entered number is greater than (2^63)-1 or I'm going to look pretty foolish :-)
Hah. Point made though -- there's no getting away from not handling inputs that are invalid or not a number. That said, this was the first answer to say use long instead of int.
I think you meant Int32.TryParse, otherwise you'll have to have another check to ensure the number is less that the limit specified.
Its heaps longer : 4587566320000000000000000000
0

The error occurs because 78987162789 is greater than 2^31, so it's too big for an Int32. As suggested, use the TryParse method, and only proceed if it returns true.

Comments

0
Int64 result;
if (!Int64.TryParse(Textbox1.Text, out result))
{
    // The value is not a number or cannot be stored in a 64-bit integer.
}
else if (result > (Int64)Int32.MaxValue)
{
    // The value cannot be stored in a 32-bit integer.
}

4 Comments

What's the value of the else if condition?
I don't understand your question.
@Chaos: I think he means, "what's the added value of performing that additional check?" Also, note that a successful Int64.TryParse does not guarantee that the value is a number, as your comment suggests.
nm, I'm a moron that can't read... I missed the ! in front of int64.TryParse.
0

You can use the Validating event.

private void textbox1_Validating(object sender, CancelEventArgs e)
{
    try
    {
        Int64 numberEntered = Int64.Parse(textBox1.Text);
        if (numberEntered > 2147483647)
        {
            e.Cancel = true;
            MessageBox.Show("You have to enter number up to 2147483647");
        }
    }
    catch (FormatException)
    {
        e.Cancel = true;
        MessageBox.Show("You need to enter a valid integer");
    }
}



    private void InitializeComponent()
    {

        // 
        // more code
        // 
        this.Textbox1.Validating += new System.ComponentModel.CancelEventHandler(this.textbox1_Validating);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.