❔ ✅ Help with a Lab
Hello!
I'm pretty new when it comes to c# and i'm working on a Lab where we're making a basic game; guessing game with a "Hot & Cold" feature added to it. My code is in my native language; Swedish. But i'm getting this error after a random amount of guesses:
"System.IndexOutOfRangeException" and it's at row 108 of my code. Would love some input. 🙂
142 Replies
BlazeBin - pssjnhoxlaaf
A tool for sharing your source code with the world!
here's the code in question
first rule of programming is program is english
so that other people can understand your code
oh its english
Yeah its just the menu texts that are in Swedish
🙂
ah yeah thats fine
Any ideas? Have you encountered that type of error before?
yeah that means you tried to access an element thats outside of the range
meaning it doesnt exist
e.g.
Cyberrex
REPL Result: Failure
Exception: IndexOutOfRangeException
Compile: 419.938ms | Execution: 34.645ms | React with ❌ to remove this embed.
the range for this array is [0,1], when we try to access 2 it throws IndexOutOfRange
do you know how to use a debugger?
I don't think we have yet 😮
VS debugger?
its simple and really useful
not necessarily vs
what editor are you using?
Visual Studio 2019
Where do you see the array range being 0,1?
VS 2022
for me
(its the free version)
Is your app a Windows Forms App and not .NET Framework ?
it's .NET Framework
K
Don't switch tho
That won't do anything
oh, I'm just trying to figure out why this is happening. spent so much time on this its kinda silly hahaha
I know absolutely zero about this cuz I'm a beginner
me too, this programme started like 2 weeks ago for me
those arent mutually exclusive
youre on vs?
Yeah i am!
when you click next to a line ou can put a red dot
try it
What type of a line?
All I know is simple shit like background images
next to the line number
Can't help with this
when you hover your cursor this white circle appears
click and it becomes red
red dot means breakpoint
oooh i see now
which means the app will pause when it reaches this line
try it
should i do that at row 108?
put a breakpoint on the first line of your app
i assume
oh
just to try the debugger
then we can actually debug your problem
yup
like this?
and now run your app
(in debug mode)
why isnt your syntax highliting working?
I dont know 😮
can you run your app?
from vs?
Yeah
huh weird
anyway
run your app
no with your breakpoint
dont remove it
I didnt remove the breakpoint, its red 😮
huh
can you screenshot your entire vs window
and the solution explorer
hmm could be a netfx thing
could it have something to do with row 108?
as in this
when you run your app should pause at the first line
it shouldnt reach that point
because of the breakpoint
oh
okay now its not running
i think
it only ran because i used the second start button
what second one
oh you mean the solid button
that one haha
no thats not the one you should be using
oh
i use that all the time
but how did that work
that buttons runs without debugging
Haha good question
wait this is the one you used now?
the screen is empty
yeah im aware
because the program is paused
oh
ok so when your writing a program always run with the debugger so it breaks at exceptions
aka dont use this one
use the other one
thats why it wasnt working
anyway
Okay, that makes sense
break means pause
anyway
so now your editor looks like this right?
the line highlighted in yellow is the line that will be executed next]
yeah!
which means it hasnt executed yet
click this
done, it moved to the next one
this button will execute the currently highlighted line
thats handy
yup
so now you can go line by line
and
when you hover your mouse over a variable, you can see its value
also
at the bottom left panel
theres a
Locals
tab
this shows you all the local variables
local means variables declared in the current scopeCurrent scope as in things it sees on that highlighted line?
locals are highlighted in this color
oh
no it means things in the current method
in this case
oh i see
so
difficultyLevel
, maxNumber
, responses
, are all localsthat makes sense 😮
What do i do after the stepover?
and you can see their value by either hovering your mouse over them or looking at the locals panel
like just keep testing each line?
so now you know how to use the debugger
oh
put your breakpoint at the line where it breaks
oh another thing
the continue button
where the run button used to be
means "execute until you reach the next breakpoint"
so for example if you have a breakpoint in a loop, you can click continue and your program will run normally until it reaches the breakpoint again in the next iteration
now your issue is at line 108
oooh that's very handy
so put a breakpoint there
I did, pressed start and the program works fine...
yeah so now try to reproduce the issue
it crashes after the first guess
crashes?
it doesnt break?
(again, break means pause execution)
the program runs normally at first, i enter a value (in this instance it's 20)
the command prompt crashes
did you set the breakpiont?
Yeah its red
at #108
and run with this button?
Yeah
can you record what happens?
snipping tool can record video
okay
I cant type again in the Command prompt
it doesnt crash
that happens because your program is frozen
because execution is paused
ohh
thats whats supposed to happen
now leave the command prompt
i see; sorry this is all new to me lol
go to vs and execute line by line to see why the index is out of range
no its fine :)
So i just step over and press continue?
and keep trying?
actually you want to put the breakpoint at the start of the loop
So on the While loop line? and just go down?
yeah
or no
when you declare
responseIndex
because thats whats causing the issue
you understand why this happens right?vaguely understand, i will know for sure when i've tested this a couple times
all of this has taken me ~5-6h to make o_o
i meant you understand what
IndexOutOfRange
means?Yeah i'd like to think that it self explanatory
but
basically
say i have 5 arrays
and im trying to access array #5
it does not exist
since 0,1,2,3,4,
exists
or something like that right?
element not array
but yes
Yeah that! still learning the words in specific XD
Okay, so i did it from 101 to 115
the code ran and i got to choose a number
from all of those up until the if (firstGuess) Line
then it just crashed
so i assume thats where it stops working
@.sibber
huh?
wait which line are talking about
Nvm, it just crashed
we already know the problem is at line 108
the problem is with whatever you do here
responseIndex
becomes out of rangehmmm
can you tell me what happens here?
in your own word?
why it throws?
int difference = Math.Abs(guess - number); - this counts the difference between the users guesses and the right number
int responseIndex = Array.FindIndex(responseRanges, range => difference >= range);
this uses an array to search for the first element(?) where the difference is greater than the value for range.
yes thats right
but i meant
explain why it would error
when you try to acces an element in the array
If i had to guess
and i could absolutely be wrong here
but my guess is that responseIndex is being set to -1 and theres no match in responseRanges???
sorry i had to go
the answer i was looking for is because youre trying to access and element thats not available kn
responses
qka theres no element at index responseIndex
so, use the debugger and figure out what responseIndex
is and the length of responses
its okay, i appreciate all the help im getting
i think i figured it out
great :)
Thank you so much for everything and the patience 😄
np :)
$close
Use the /close command to mark a forum thread as answered
@whitecoataladdin ^
if you have no further questions
Will do, thanks again 🙂
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.