C
C#โ€ข16mo ago
F

โ” โœ… 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
F
FOPโ€ข16mo ago
BlazeBin - pssjnhoxlaaf
A tool for sharing your source code with the world!
F
FOPโ€ข16mo ago
here's the code in question
sibber
sibberโ€ข16mo ago
first rule of programming is program is english so that other people can understand your code oh its english
F
FOPโ€ข16mo ago
Yeah its just the menu texts that are in Swedish ๐Ÿ™‚
sibber
sibberโ€ข16mo ago
ah yeah thats fine
F
FOPโ€ข16mo ago
Any ideas? Have you encountered that type of error before?
sibber
sibberโ€ข16mo ago
yeah that means you tried to access an element thats outside of the range meaning it doesnt exist e.g.
MODiX
MODiXโ€ข16mo ago
Cyberrex
REPL Result: Failure
int[] arr = { 1, 2 };
arr[2]
int[] arr = { 1, 2 };
arr[2]
Exception: IndexOutOfRangeException
- Index was outside the bounds of the array.
- Index was outside the bounds of the array.
Compile: 419.938ms | Execution: 34.645ms | React with โŒ to remove this embed.
sibber
sibberโ€ข16mo ago
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?
F
FOPโ€ข16mo ago
I don't think we have yet ๐Ÿ˜ฎ
br0kE
br0kEโ€ข16mo ago
VS debugger?
sibber
sibberโ€ข16mo ago
its simple and really useful not necessarily vs what editor are you using?
br0kE
br0kEโ€ข16mo ago
Visual Studio 2019
F
FOPโ€ข16mo ago
Where do you see the array range being 0,1? VS 2022 for me (its the free version)
br0kE
br0kEโ€ข16mo ago
Is your app a Windows Forms App and not .NET Framework ?
F
FOPโ€ข16mo ago
it's .NET Framework
br0kE
br0kEโ€ข16mo ago
K Don't switch tho That won't do anything
F
FOPโ€ข16mo ago
oh, I'm just trying to figure out why this is happening. spent so much time on this its kinda silly hahaha
br0kE
br0kEโ€ข16mo ago
I know absolutely zero about this cuz I'm a beginner
F
FOPโ€ข16mo ago
me too, this programme started like 2 weeks ago for me
sibber
sibberโ€ข16mo ago
those arent mutually exclusive youre on vs?
F
FOPโ€ข16mo ago
Yeah i am!
sibber
sibberโ€ข16mo ago
when you click next to a line ou can put a red dot try it
F
FOPโ€ข16mo ago
What type of a line?
br0kE
br0kEโ€ข16mo ago
All I know is simple shit like background images
sibber
sibberโ€ข16mo ago
next to the line number
br0kE
br0kEโ€ข16mo ago
Can't help with this
sibber
sibberโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
when you hover your cursor this white circle appears click and it becomes red red dot means breakpoint
F
FOPโ€ข16mo ago
oooh i see now
sibber
sibberโ€ข16mo ago
which means the app will pause when it reaches this line try it
F
FOPโ€ข16mo ago
should i do that at row 108?
sibber
sibberโ€ข16mo ago
put a breakpoint on the first line of your app
F
FOPโ€ข16mo ago
i assume oh
sibber
sibberโ€ข16mo ago
just to try the debugger then we can actually debug your problem
F
FOPโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
yup
F
FOPโ€ข16mo ago
like this?
sibber
sibberโ€ข16mo ago
and now run your app (in debug mode) why isnt your syntax highliting working?
F
FOPโ€ข16mo ago
I dont know ๐Ÿ˜ฎ
sibber
sibberโ€ข16mo ago
can you run your app? from vs?
F
FOPโ€ข16mo ago
Yeah
sibber
sibberโ€ข16mo ago
huh weird anyway run your app
F
FOPโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
no with your breakpoint dont remove it
F
FOPโ€ข16mo ago
I didnt remove the breakpoint, its red ๐Ÿ˜ฎ
sibber
sibberโ€ข16mo ago
huh can you screenshot your entire vs window
F
FOPโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
and the solution explorer
F
FOPโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
hmm could be a netfx thing
F
FOPโ€ข16mo ago
could it have something to do with row 108? as in this
sibber
sibberโ€ข16mo ago
when you run your app should pause at the first line
F
FOPโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
it shouldnt reach that point because of the breakpoint
F
FOPโ€ข16mo ago
oh okay now its not running i think it only ran because i used the second start button
sibber
sibberโ€ข16mo ago
what second one
F
FOPโ€ข16mo ago
No description
F
FOPโ€ข16mo ago
No description
sibber
sibberโ€ข16mo ago
oh you mean the solid button
F
FOPโ€ข16mo ago
that one haha
sibber
sibberโ€ข16mo ago
no thats not the one you should be using
F
FOPโ€ข16mo ago
oh i use that all the time
sibber
sibberโ€ข16mo ago
but how did that work that buttons runs without debugging
F
FOPโ€ข16mo ago
Haha good question
sibber
sibberโ€ข16mo ago
wait this is the one you used now?
F
FOPโ€ข16mo ago
No description
F
FOPโ€ข16mo ago
the screen is empty
sibber
sibberโ€ข16mo ago
yeah im aware because the program is paused
F
FOPโ€ข16mo ago
oh
sibber
sibberโ€ข16mo ago
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
F
FOPโ€ข16mo ago
Okay, that makes sense
sibber
sibberโ€ข16mo ago
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]
F
FOPโ€ข16mo ago
yeah!
sibber
sibberโ€ข16mo ago
which means it hasnt executed yet
sibber
sibberโ€ข16mo ago
click this
No description
F
FOPโ€ข16mo ago
done, it moved to the next one
sibber
sibberโ€ข16mo ago
this button will execute the currently highlighted line
F
FOPโ€ข16mo ago
thats handy
sibber
sibberโ€ข16mo ago
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 scope
F
FOPโ€ข16mo ago
Current scope as in things it sees on that highlighted line?
sibber
sibberโ€ข16mo ago
locals are highlighted in this color
No description
F
FOPโ€ข16mo ago
oh
sibber
sibberโ€ข16mo ago
no it means things in the current method in this case
F
FOPโ€ข16mo ago
oh i see
sibber
sibberโ€ข16mo ago
so difficultyLevel, maxNumber, responses, are all locals
F
FOPโ€ข16mo ago
that makes sense ๐Ÿ˜ฎ What do i do after the stepover?
sibber
sibberโ€ข16mo ago
and you can see their value by either hovering your mouse over them or looking at the locals panel
F
FOPโ€ข16mo ago
like just keep testing each line?
sibber
sibberโ€ข16mo ago
so now you know how to use the debugger
F
FOPโ€ข16mo ago
oh
sibber
sibberโ€ข16mo ago
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
F
FOPโ€ข16mo ago
oooh that's very handy
sibber
sibberโ€ข16mo ago
so put a breakpoint there
F
FOPโ€ข16mo ago
I did, pressed start and the program works fine...
sibber
sibberโ€ข16mo ago
yeah so now try to reproduce the issue
F
FOPโ€ข16mo ago
it crashes after the first guess
sibber
sibberโ€ข16mo ago
crashes? it doesnt break? (again, break means pause execution)
F
FOPโ€ข16mo ago
the program runs normally at first, i enter a value (in this instance it's 20) the command prompt crashes
sibber
sibberโ€ข16mo ago
did you set the breakpiont?
F
FOPโ€ข16mo ago
Yeah its red at #108
sibber
sibberโ€ข16mo ago
and run with this button?
No description
F
FOPโ€ข16mo ago
Yeah
sibber
sibberโ€ข16mo ago
can you record what happens? snipping tool can record video
F
FOPโ€ข16mo ago
okay
F
FOPโ€ข16mo ago
F
FOPโ€ข16mo ago
I cant type again in the Command prompt
sibber
sibberโ€ข16mo ago
it doesnt crash that happens because your program is frozen because execution is paused
F
FOPโ€ข16mo ago
ohh
sibber
sibberโ€ข16mo ago
thats whats supposed to happen now leave the command prompt
F
FOPโ€ข16mo ago
i see; sorry this is all new to me lol
sibber
sibberโ€ข16mo ago
go to vs and execute line by line to see why the index is out of range no its fine :)
F
FOPโ€ข16mo ago
So i just step over and press continue? and keep trying?
sibber
sibberโ€ข16mo ago
actually you want to put the breakpoint at the start of the loop
F
FOPโ€ข16mo ago
So on the While loop line? and just go down?
sibber
sibberโ€ข16mo ago
yeah or no when you declare responseIndex because thats whats causing the issue you understand why this happens right?
F
FOPโ€ข16mo ago
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
sibber
sibberโ€ข16mo ago
i meant you understand what IndexOutOfRange means?
F
FOPโ€ข16mo ago
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?
sibber
sibberโ€ข16mo ago
element not array but yes
F
FOPโ€ข16mo ago
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
br0kE
br0kEโ€ข16mo ago
@.sibber
sibber
sibberโ€ข16mo ago
huh? wait which line are talking about
F
FOPโ€ข16mo ago
Nvm, it just crashed
sibber
sibberโ€ข16mo ago
we already know the problem is at line 108 the problem is with whatever you do here
int difference = Math.Abs(guess - number);
int responseIndex = Array.FindIndex(responseRanges, range => difference >= range);

if (responseIndex == -1)
{
responseIndex = responses.Length - 1; // Default till "It's lit!" om avstรฅndet รคr mindre รคn 0
}

Console.WriteLine(responses[responseIndex]);
int difference = Math.Abs(guess - number);
int responseIndex = Array.FindIndex(responseRanges, range => difference >= range);

if (responseIndex == -1)
{
responseIndex = responses.Length - 1; // Default till "It's lit!" om avstรฅndet รคr mindre รคn 0
}

Console.WriteLine(responses[responseIndex]);
responseIndex becomes out of range
F
FOPโ€ข16mo ago
hmmm
sibber
sibberโ€ข16mo ago
can you tell me what happens here? in your own word? why it throws?
F
FOPโ€ข16mo ago
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.
sibber
sibberโ€ข16mo ago
yes thats right but i meant explain why it would error when you try to acces an element in the array
F
FOPโ€ข16mo ago
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???
sibber
sibberโ€ข16mo ago
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
F
FOPโ€ข16mo ago
its okay, i appreciate all the help im getting i think i figured it out
sibber
sibberโ€ข16mo ago
great :)
F
FOPโ€ข16mo ago
Thank you so much for everything and the patience ๐Ÿ˜„
sibber
sibberโ€ข16mo ago
np :) $close
MODiX
MODiXโ€ข16mo ago
Use the /close command to mark a forum thread as answered
sibber
sibberโ€ข16mo ago
@whitecoataladdin ^ if you have no further questions
F
FOPโ€ข16mo ago
Will do, thanks again ๐Ÿ™‚
Accord
Accordโ€ข16mo ago
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.

Did you find this page helpful?