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
ero2y ago
threadCounter is only assigned to when readLine2 was > 0 and < 100
TroflineBlack
TroflineBlackOP2y ago
Yes. I made a goto, if its below 0 and made threadCounter = readLine2 if its above 100
ero
ero2y ago
i'm not seeing any of that in what you provided
TroflineBlack
TroflineBlackOP2y ago
second
ero
ero2y ago
$code
MODiX
MODiX2y 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/
TroflineBlack
TroflineBlackOP2y ago
how can i share it with you? ill do it different
TroflineBlack
TroflineBlackOP2y ago
TroflineBlack
TroflineBlackOP2y ago
better? so why is it giving me an error :/
ero
ero2y 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
TheRanger2y ago
$codegif
TroflineBlack
TroflineBlackOP2y ago
but where? ohh, if its 0?
ero
ero2y ago
when readLine2 is == 0 or == 100
TroflineBlack
TroflineBlackOP2y ago
ohhhh i see. i covered them. but still giving me the error i did by <=0 and >=100
TheRanger
TheRanger2y ago
then theres another possibility u didnt cover yet
ero
ero2y ago
mhm
TroflineBlack
TroflineBlackOP2y ago
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
ero2y 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
TroflineBlack
TroflineBlackOP2y ago
I dont quite understand
ero
ero2y 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;
}
TroflineBlack
TroflineBlackOP2y ago
nope didnt fix it the error is: Use of the unassigned local variable "threadCounter".
TheRanger
TheRanger2y ago
oh i think i see the issue now
ero
ero2y 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
TroflineBlack
TroflineBlackOP2y ago
yes i have exactly that
ero
ero2y ago
so you need
if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
else { }
if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
else { }
TroflineBlack
TroflineBlackOP2y ago
TroflineBlack
TroflineBlackOP2y ago
TroflineBlack
TroflineBlackOP2y ago
TroflineBlack
TroflineBlackOP2y ago
got them all covered
ero
ero2y ago
please re-read what i said
TroflineBlack
TroflineBlackOP2y ago
ohh
Pascal
Pascal2y 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
ero2y ago
that would be another way to fix it, yes
TroflineBlack
TroflineBlackOP2y ago
seems to work
ero
ero2y ago
but i think it's good to understand branching it will become crucial for more complex logic for null handling especially
TroflineBlack
TroflineBlackOP2y ago
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
ero2y ago
well again, it needs that final else block
TroflineBlack
TroflineBlackOP2y ago
i have it like this now, and it works..
ero
ero2y ago
yup
TroflineBlack
TroflineBlackOP2y ago
ye. okay. so it gets confused there. i see
ero
ero2y ago
the final else block basically says "there cannot be any other possibility here" which means everything must be covered
TroflineBlack
TroflineBlackOP2y ago
okay good. Thanks mates! :3

Did you find this page helpful?