Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Good morning,
I’m sorry to disturb you but I have a slight problem with my code. Here, when I try to run my code, I get the following message. However, I have no idea what I need to correct; I tried several things, in doubt, I checked well if I converted everything from "string" to "int", but nothing to do, I still have the same error.
I would very much like to have some help so that we can fix this. Thank you in advance.
19 Replies
Debugger is your friend
Seems like
numberGoldPiece
is a number that's not a valid index in either XArrayCoordoninateCoins
or YArrayCoordoninateCoins
arrayWhere can I see this ? (Sorry, I'm very beginner with Visual Studio and C# in general).
I'm not sure to understand ; where you see this ? (wich part of the code/number of line ?).
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
As to where I see it, well, the error says that the exception occured on line 81
thanks.
It also says that the error that happened was about accessing a nonexistant index from an array
A line above that, line 80, has two array accesses
Ergo, the error must've happened there
And it must've been what I said
Reading the error message can be useful 😛
Line 81'''if (XArrayCoordoninateCoins[numberGoldPiece] == x YArrayCoordoninateCoins[numberGoldPiece] == y)
{
Console.WriteLine("That exists.");
}
else
{
}'''
Yes ; but right there with'''XArrayCoordoninateCoins[numberGoldPiece] == x YArrayCoordoninateCoins[numberGoldPiece] == y)'''
I try to check each value contained in both array. Isn’t that how we do it?
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/And, yeah, that would be how you do it
But it doesn't mean the error doesn't exist
The recipe for cookies calls for a cup of sugar, and you added a cup of sugar, all's well
But there was an error earlier, by mistake you poured salt into the sugar container
So you got salty cookies in the end
AH ! The error happened in line 81, but the core of the error is earlier ?
Could be, yeah
That's why the debugger is how you solve the issue
It lets you see how the code flows exactly, how the values change over time
So you can easily see "wait, this is not what it should be!"
And trace that error to where it happened
I had never heard of the debugger. Thank you very much, I will see it and hope that it helps me! Thank you very much! ❤️
:Ok:
It worked ; that help me to understand where was the problem. Thank you very much.
Nice
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered