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
ero
ero10mo ago
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
ero
ero10mo ago
i'm not seeing any of that in what you provided
ero
ero10mo ago
$code
MODiX
MODiX10mo ago
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 :/
ero
ero10mo ago
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
TheRanger
TheRanger10mo ago
$codegif
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
but where? ohh, if its 0?
ero
ero10mo ago
when readLine2 is == 0 or == 100
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
ohhhh i see. i covered them. but still giving me the error i did by <=0 and >=100
TheRanger
TheRanger10mo ago
then theres another possibility u didnt cover yet
ero
ero10mo ago
mhm
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 > 0 && readLine2 < 100)
{
hesSure:
threadCounter = readLine2;
}
else if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key != ConsoleKey.Y && ignoreWarning.Key != ConsoleKey.N)
{
Console.Clear();
goto reask;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}

for (int i = threadCounter; threadCounter > 0; i--)
{
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 > 0 && readLine2 < 100)
{
hesSure:
threadCounter = readLine2;
}
else if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key != ConsoleKey.Y && ignoreWarning.Key != ConsoleKey.N)
{
Console.Clear();
goto reask;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}

for (int i = threadCounter; threadCounter > 0; i--)
{
ero
ero10mo ago
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) else
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
I dont quite understand
ero
ero10mo ago
if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}
if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
nope didnt fix it the error is: Use of the unassigned local variable "threadCounter".
TheRanger
TheRanger10mo ago
oh i think i see the issue now
ero
ero10mo ago
it's just the branching roslyn needs an else branch to know everything is covered if you have
if (readLine2 > 0 && readLine2 < 100) { }
else if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
if (readLine2 > 0 && readLine2 < 100) { }
else if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
then yes, everything is covered but the compiler doesn't understand that
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
yes i have exactly that
ero
ero10mo ago
so you need
if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
else { }
if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
else { }
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
got them all covered
ero
ero10mo ago
please re-read what i said
Pascal
Pascal10mo ago
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.
ero
ero10mo ago
that would be another way to fix it, yes
ero
ero10mo ago
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
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}

}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}
else
{
hesSure:
threadCounter = readLine2;
}


for (int i = threadCounter; threadCounter > 0; i--)
{
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}

}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}
else
{
hesSure:
threadCounter = readLine2;
}


for (int i = threadCounter; threadCounter > 0; i--)
{
ero
ero10mo ago
well again, it needs that final else block
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
i have it like this now, and it works..
ero
ero10mo ago
yup
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
ye. okay. so it gets confused there. i see
ero
ero10mo ago
the final else block basically says "there cannot be any other possibility here" which means everything must be covered
₮ⱤØ₣Ⱡł₦Ɇ_฿Ⱡ₳₵₭
okay good. Thanks mates! :3