0

When I run this it says that index was outside of bounds for the array but it is not. If I change public static ConectorRec[] ch = new ConectorRec[74]; to ConectorRec[] ch = new ConectorRec[75]; then they are not all referenced to an object and throws other errors on other parts of my code.

using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Dots
{
    public static class Conectors
    {
        public static ConectorRec[] ch = new ConectorRec[74];
        public static void intitialize()
        {
            ch[0] = new ConectorRec(new Rectangle(10, 20, 10, 40));
            ch[1] = new ConectorRec(new Rectangle(20, 10, 40, 10));
            ch[2] = new ConectorRec(new Rectangle(20, 60, 40, 10));
            ch[3] = new ConectorRec(new Rectangle(60, 20, 10, 40));
            int t = 0;
            int tt = 1;
            for (int i = 4; i<73; i++)
            {
                t++;
                if (t == 1)
                {
                    ch[i] = new ConectorRec(new Rectangle(50 * tt + 20, 10, 40, 10));
                }
                if (t == 2)
                {
                    ch[i] = new ConectorRec(new Rectangle(50 * tt + 20, 60, 40, 10));
                }
                if (t == 3)
                {
                    tt++;
                    ch[i] = new ConectorRec(new Rectangle(50 * tt + 10, 20, 10, 40));
                    t = 0;
                }
            }
            ch[74] = new ConectorRec(new Rectangle(10, 70, 10, 40));
        }
    }
}

please tell me what I am doing wrong and how to fix this error.

sefodopo
  • 3
  • 2

3 Answers3

8

When I run this it says that index was outside of bounds for the array but it is not

Yes it is. Even if I couldn't pinpoint the problem, I'd always bet on the runtime being more accurate than the developer's opinion on this...

This is the problem, after the loop:

ch[74] = new ConnectorRec(...);

You've declared the array to have 74 elements here:

public static ConectorRec[] ch = new ConectorRec[74];

So the valid indexes are 0..73 inclusive. If you want to make 74 a valid index, you need to declare it to have 75 elements:

public static ConectorRec[] ch = new ConectorRec[75];
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

Array indices are zero-based.Your array size is 74 so your last element is ch[73] not 74. You can verify that by using GetUpperBound method:

var maxIndex = ch.GetUpperBound(0); // this will return 73

From C# Specification Section 12. Arrays:

The length of a dimension determines the valid range of indices for that dimension: For a dimension of length N, indices can range from 0 to N – 1 inclusive.

In this case your array Length (N) is 74 and therefore max. index is 73 (N -1).

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

The error is due to

ch[74] = new ConectorRec(new Rectangle(10, 70, 10, 40));

Your array size is 74 that means you will have the maximum index of 73, since array index starts from 0

Habib
  • 219,104
  • 29
  • 407
  • 436