none
Exception "Index was outside the bounds of the array"

    Question

  • How can I prevent my code from evaluating this array and causing this error?
    Index was outside the bounds of the array."
    private void GetVersionInfo(string[] arr, byte[] buffer)
    {
         //The following line is throwing this error... 
         if (arr[12] != null)
         {
    • Changed type hazz Wednesday, January 27, 2010 5:46 PM
    Wednesday, January 27, 2010 5:17 PM

Answers

  • you could make sure the upperbound of the array is not being violated by using this code

    int upper = arr.GetUpperBound(0);
    This will give you the upper bound of the array and you could check your number you are looking for to make sure it is not larger than the upperbound array of the array
    Dewayne Dodd - Landshark Software "Please make sure to 'Mark As Answer' if this answer solves your question"
    • Marked as answer by hazz Wednesday, January 27, 2010 5:47 PM
    Wednesday, January 27, 2010 5:24 PM
  • Remember indices are 0 based, so the 12th element in the array is arr[11].
    • Marked as answer by hazz Wednesday, January 27, 2010 5:47 PM
    Wednesday, January 27, 2010 5:30 PM
  • The string array you're passing in as "arr" doesn't have 13 parameters.

    In your case, arr.Length < 13, so when you access element 12, you get that error.

    Avoiding this is a matter of either adding a check, or making sure the array is long enough.  To add the check, just do:



    private void GetVersionInfo(string[] arr, byte[] buffer)
    {
         if (arr.Length > 12)
         {
             // This will get skipped now...
             if (arr[12] != null)
             {
     //.. 
    

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by hazz Wednesday, January 27, 2010 5:47 PM
    Wednesday, January 27, 2010 5:25 PM

All replies

  • you could make sure the upperbound of the array is not being violated by using this code

    int upper = arr.GetUpperBound(0);
    This will give you the upper bound of the array and you could check your number you are looking for to make sure it is not larger than the upperbound array of the array
    Dewayne Dodd - Landshark Software "Please make sure to 'Mark As Answer' if this answer solves your question"
    • Marked as answer by hazz Wednesday, January 27, 2010 5:47 PM
    Wednesday, January 27, 2010 5:24 PM
  • The string array you're passing in as "arr" doesn't have 13 parameters.

    In your case, arr.Length < 13, so when you access element 12, you get that error.

    Avoiding this is a matter of either adding a check, or making sure the array is long enough.  To add the check, just do:



    private void GetVersionInfo(string[] arr, byte[] buffer)
    {
         if (arr.Length > 12)
         {
             // This will get skipped now...
             if (arr[12] != null)
             {
     //.. 
    

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by hazz Wednesday, January 27, 2010 5:47 PM
    Wednesday, January 27, 2010 5:25 PM
  • Remember indices are 0 based, so the 12th element in the array is arr[11].
    • Marked as answer by hazz Wednesday, January 27, 2010 5:47 PM
    Wednesday, January 27, 2010 5:30 PM