How to handle a System.OverflowException

I have one Windows application that has a text box in it. When I enter the amount in a text box, it converts it into words in another text box with a name txtrupees. Sum The maximum length of the text field is set to 11 places in the last three places .00.

My problem now, when I enter the amount with .00, it works fine. But if I enter 11 places, it will give the following error:

System.OverflowException error in mscorlib.dll The value was too large or too small for the following Int32.tried code.

How can I prevent such an error?

private void txtamount_TextChanged(object sender, EventArgs e)
{
    if (txtamount.Text != string.Empty)
    {
        string[] amount = txtamount.Text.Split('.');
        if (amount.Length == 2)
        {
            int rs, ps;
            int.TryParse(amount[0], out rs);
            int.TryParse(amount[1], out ps);

            string rupees = words(rs);
            string paises = words(ps);
            txtrupees.Text = rupees + " rupees and " + paises + " paisa only ";
        }
        else if (amount.Length == 1)
        {
            string rupees = words(Convert.ToInt32(amount[0]));
            txtrupees.Text = rupees + " rupees only";
        }
    }
}
+4
source share
2 answers

Convert.ToInt32(amount[0]), amount[0] , Int.MaxValue Int.MinValue, .

int.TryParse(amount[0], out foo); foo:

else if (amount.Length == 1)
{
    int ps;
    if(int.TryParse(amount[0], out ps))
    {
        string rupees = words(ps);
        txtrupees.Text = rupees + " rupees only";
    }
    else
        txtrupees.Text = "Invalid number";
}

, Int64, Double Decimal

+5

Source: https://habr.com/ru/post/1675694/


All Articles