Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
for (int i = 0; i < 4; i++)
                 {
                     if (!IsOccupied(saveRloction + i, saveEloction))
                     {
                         board[saveRloction + i, saveEloction] = 1;
                         if ((ex > 40 + 48 * saveEloction) && (ex < 40 + 48 * (saveEloction + 1)) && (ey <= 37 + 48 * saveRloction) && (ex < 180))
                         {
                             Graphics g = this.CreateGraphics();
                             Pen mypen = new Pen(Color.Red, 3);

                             g.DrawLine(mypen, 37 + 48 * saveEloction, 37 + 48 * saveRloction, 37 + 48 * (saveEloction + 1), 37 + 48 * saveRloction);
                               }
                             break;
                         }
                 }

          }


What I have tried:

saveRloction: column number
saveEloction: row number
ex,ey:mouse location

i have points in rows and columns, i want to make a line between each two points.
i show Error message "Programming error:Index was outside the bounds of the array ". So, wheres the problem?!
Posted
Updated 10-Jun-19 22:48pm
v2
Comments
Herman<T>.Instance 11-Jun-19 3:20am    
If you debug your code you would know where the index was outside the bounds. In your shown code some array with no or less then 5 items will lead to this message.

TIPS:
1: debug
2: read exception stack trace to see where the exception occurred in your code.
F-ES Sitecore 11-Jun-19 3:56am    
google the error message, you'll find lots of things explaining what causes this error.

From that, we can't tell - you only use one array in that code fragment:
board[saveRloction + i, saveEloction] = 1;
And we have no idea of the values in the two indexes which is essential to working out why they are incorrect.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
Error message "Programming error:Index was outside the bounds of the array "

All is said in error message, you try to access a part of array that does not exist.
The only way to see what is going on is to use the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900