For-Loop doesnt work.
I cant fully understand, why its not working? The variable is an integer. Why is it giving me an error?
43 Replies
threadCounter
is only assigned to when readLine2
was > 0
and < 100
Yes. I made a goto, if its below 0
and made threadCounter = readLine2 if its above 100
i'm not seeing any of that in what you provided
second
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/how can i share it with you?
ill do it different
better?
so why is it giving me an error :/
it's not better, no. the bot very clearly showed how you properly post code.
anyway, you're not covering all possibilities
there are times when
threadCounter
is still never assigned$codegif
but where?
ohh, if its 0?
when
readLine2
is == 0
or == 100
ohhhh
i see.
i covered them. but still giving me the error
i did by <=0 and >=100
then theres another possibility u didnt cover yet
mhm
though the branching might honestly just be too confusing for the compiler
it might work if you change the warning logic
to
if-else-if-else
if (N) else if (Y) elseI dont quite understand
nope
didnt fix it
the error is:
Use of the unassigned local variable "threadCounter".
oh i think i see the issue now
it's just the branching
roslyn needs an
else
branch to know everything is covered
if you have
then yes, everything is covered
but the compiler doesn't understand thatyes
i have exactly that
so you need
got them all covered
please re-read what i said
ohh
the issue with your code is that
threadCounter
, is sometimes unassigned. So the compiler does not know what to do when the value is unassigned and yet you are trying to access a value from it. you can fix this by assigning a default value to threadCounter
. Something like int threadCounter = 0
or whatever your default value is.that would be another way to fix it, yes
seems to work
but i think it's good to understand branching
it will become crucial for more complex logic
for null handling especially
i see
but even tho in any way i covered all possibilities, i can confuse the compiler with that?
because at the end it wouldve gotten an value anyways, right?
wait
well again, it needs that final
else
blocki have it like this now, and it works..
yup
ye. okay. so it gets confused there. i see
the final
else
block basically says "there cannot be any other possibility here"
which means everything must be coveredokay
good. Thanks mates! :3