jeudi 12 juillet 2018

What could possibly be wrong with my Code? c#

Helo, im trying to solve this other problem. The program has to display "Invalid Length" if the characters stored in string(id) are not equal to 13, these are inputed in a textbox txtId. But however, whenever this condition is met, I get an error "System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Parameter name: length'"

        string id = txtId.Text.Substring(0, 13);
        if (!(id.Length==13))
        {
            LblDisp.Text = "Invalid Length";
        }
        else
        {
            string year = id.Substring(0, 2).ToString();
            string month = id.Substring(2, 2).ToString();
            string day = id.Substring(4, 2).ToString();
            string gender = id.Substring(6, 1).ToString();

            int yy = int.Parse(year);
            int mm = int.Parse(month);
            int dd = int.Parse(day);
            int xx = int.Parse(gender);

            if (!(yy >= 40 && yy <= 99) || (yy >=0 && yy <= 18))
            {
                LblDisp.Text = "Invalid Year";
            }

Aucun commentaire:

Enregistrer un commentaire