Learning C#, trying to get a user to input array index and return name stored in that index
Now the exception handler says the index is currently out of bounds but why is this happening since the user has not made an input yet
14 Replies
This right there
Check the length of your array and then check the indices again :)
bruhh, my bad on missing that out
Happens
alright so i got a new problem with it
actually wait nvm
so when i type in a string, it does give me the error message within my if statement, but entering anything outside of my index range just gives me another error which again is the out of bounds array
Can you tell, what is exactly thrown and where?
I figured it out, my conditions for my if statement were incomplete and the condition itself was for checking that is within range instead of checking that it is out of range
the exception is thrown at my last console writeline, so if i wrote down -1 then the program would accept it because it is a valid integer, just out of range and then it would say out of range
Nice, that you figured that out yourself
HOWEVER
i feel this is redundant
the conditions for if and while are the same, is there some way i could make this better
this is how it looks like now
You could store result of the condition in a bool that is declared outside of the loop and then only calculate it once inside of the loop
Maybe even wrap the condition in a separate function
ValidateInput
or somethingValidateInput would just check if the bool is true and within range
Basically, yea
Thank you very much for the help man