C# index outside bounds of array


Jan 5, 2013
Im making a game and after the first run run when the user clicks new game I get index outside bounds of array.
This is the the new game button code

            int x = int.Parse(heightentered.Text);
            int y = int.Parse(widthentered.Text);
            int density = int.Parse(blockedpercentageentered.Text); 
            uniwidth = y - 1;
            uniheight = x - 1;
            // 2d string array. Feel free to edit
            positions = new Label[y, x];
            empty = new int[y, x];
            for (int i = 0; i < y; i++)
                for (int j = 0; j < x; j++)
                    Label gridmaze = new Label();
                    gridmaze.Size = new Size(8, 8);
                    gridmaze.Location = new Point(20 + (12 * i), 120 + (12 * j));
                    gridmaze.Tag = new Random(Guid.NewGuid().GetHashCode()).Next(0, 100);
                    if (int.Parse(gridmaze.Tag.ToString()) < density)
                        gridmaze.BackColor = Color.Red;
                        empty[i, j] = 1;
                        gridmaze.BackColor = Color.Yellow;
                        empty[i, j] = 0;
                    positions[i, j] = gridmaze;
            //set the borders to red
            for (int i = 0; i < y; i++)
                positions[i, 0].BackColor = Color.Red;
                positions[i, x - 1].BackColor = Color.Red;
                empty[i, 0] = 1;
                empty[i, x - 1] = 1;

            for (int i = 0; i < x; i++)
                positions[y - 1, i].BackColor = Color.Red;
                positions[0, i].BackColor = Color.Red;
                empty[0, i] = 1;
                empty[y - 1, i] = 1;

            //creates a start and end point
             positions[1,5].BackColor = positions[2, 5].BackColor
            = positions[3, 5].BackColor = Color.Yellow;
             empty[0,5] = empty[uniwidth,4] = empty[1, 5] = empty[2, 5] = empty[3,5] = 0;
             positions[uniwidth - 2, 4].BackColor = positions[uniwidth - 1, 4].BackColor = positions[uniwidth, 4].BackColor = Color.Yellow;
            positions[0, 5].BackColor = positions[uniwidth, 5].BackColor = Color.Violet;

​I try to call a clear function that sets both my empty, and positions arrays to null but I still get the same error. Im not sure why this happens because even if it wasn't empty would it now just override whats already in there?
Any help would be appreciated.

its most common one.. ur trying to access a position in array which actually doesnt exists in memory...
Like innosoft said. Index out of bounds literally means your accessing an index in the array that doesn't exist. Its like having an array that has 10 slots and your trying to access the 11th slot, it doesn't exist.
It seems to work here:


Does that button call some other methods on click?
Just post the whole method where the error occurs plus the exact line.
Thanks turns out it has to do with some other functions that the problem is coming from. I have 2 questions.
1. That functions is not called until the user clicks another button, so does the compiler not care about when its called as long as it's called?
2. Is there a way to reset my entire function that I posted above so it doesn't run into this problem because it seems the problem is in multiple functions
I know this thread's pretty old, but if you can post the other code then that will help us figure it out. It might be as simple as adding a minus 1 somewhere. So far the code above runs fine without error, so you will have to post the rest of the code. And to answer your question, the compiler can tell if you are going to try to access a nonexistent element before even running the program.
X and Y are logically reversed in your code, Y should be height and X should be width. You seem to be consistent about your usage, but if you are passing these off somewhere that is expecting traditional (x,y) layout you may have a bounds error. You (or I) also seem somewhat confused about your boundary conditions. Typically you want to avoid "val - 1" unless you are dealing with strings, as it is almost never needed.
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features and essential functions on BlackHatWorld and other forums. These functions are unrelated to ads, such as internal links and images. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock