-1

I am 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";
            }
        }
aloisdg
  • 22,270
  • 6
  • 85
  • 105

3 Answers3

1

.Substring() will throw a exception if you try to go out of its range. So if the word was "josh" and you went "josh".Substring(0,8) it would throw a exception as josh is only 4 letters and i am trying to substring to 8.

To fix it check the length first

        if (!(txtId.Text.Length==13))
        {
            LblDisp.Text = "Invalid Length";
        }
        else
        {
            string id = txtId.Text; // you do not need to even do SubString now as you know it is a length of 13
            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";
            } 
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22
1

What you are trying to do is a pattern called a guard cause.

// check for the length first
if (txtId.Text.Length < 13)
{
    LblDisp.Text = "Invalid Length";
    return;
}

// use substring later. You dont want to substring on a short string.
// If you try you can get ArgumentOutOfRangeException.
string id = txtId.Text;
string year = id.Substring(0, 2); // substring returns a string
string month = id.Substring(2, 2);
string day = id.Substring(4, 2);
string gender = id.Substring(6, 1);

// by the way, you should check if all this chars are numbers. You can use int.TryParse for this.
int yy = int.TryParse(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";
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105
0

.Substring() will fail with that error if the arguments you give it are invalid.

I.e. if the string is shorter than 13 characters and you attempt to take a substring of 13 characters, you get that error.

AKX
  • 152,115
  • 15
  • 115
  • 172