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
Angius
Angius3w ago
Debugger is your friend Seems like numberGoldPiece is a number that's not a valid index in either XArrayCoordoninateCoins or YArrayCoordoninateCoins array
Jiraknight3
Jiraknight3OP3w ago
Where 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 ?).
Angius
Angius3w ago
$debug
MODiX
MODiX3w ago
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.
Angius
Angius3w ago
As to where I see it, well, the error says that the exception occured on line 81
Jiraknight3
Jiraknight3OP3w ago
thanks.
Angius
Angius3w ago
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 😛
Jiraknight3
Jiraknight3OP3w ago
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?
Angius
Angius3w ago
$code
MODiX
MODiX3w ago
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/
Angius
Angius3w ago
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
Jiraknight3
Jiraknight3OP3w ago
AH ! The error happened in line 81, but the core of the error is earlier ?
Angius
Angius3w ago
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
Jiraknight3
Jiraknight3OP3w ago
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! ❤️
Angius
Angius3w ago
:Ok:
Jiraknight3
Jiraknight3OP3w ago
It worked ; that help me to understand where was the problem. Thank you very much.
Angius
Angius3w ago
Nice
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?