C
C#2y ago
Yamiraj

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
HimmDawg
HimmDawg2y ago
HimmDawg
HimmDawg2y ago
This right there Check the length of your array and then check the indices again :)
Yamiraj
YamirajOP2y ago
bruhh, my bad on missing that out
HimmDawg
HimmDawg2y ago
Happens owo
Yamiraj
YamirajOP2y ago
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
HimmDawg
HimmDawg2y ago
Can you tell, what is exactly thrown and where?
Yamiraj
YamirajOP2y ago
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
HimmDawg
HimmDawg2y ago
Nice, that you figured that out yourself owo
Yamiraj
YamirajOP2y ago
HOWEVER i feel this is redundant the conditions for if and while are the same, is there some way i could make this better
Yamiraj
YamirajOP2y ago
this is how it looks like now
HimmDawg
HimmDawg2y ago
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 something
Yamiraj
YamirajOP2y ago
ValidateInput would just check if the bool is true and within range
HimmDawg
HimmDawg2y ago
Basically, yea
Yamiraj
YamirajOP2y ago
Thank you very much for the help man
Want results from more Discord servers?
Add your server