1

This isn't a sample of my code, but just a recreation of my problem. I'm very now to coding so it's probably just a dumb mistake, but I'm still posting this here just in case I can get an answer.

bool test;
int num;

num = Convert.ToInt32(Console.ReadKey());

if (num == 9) {
    test = true;
}

//Error on line below: "Use of unassigned local variable 'test' [error]csharp(CS0165)"
if (test == true) {
    Console.WriteLine("Random text");
}

I believe I get the error because I am trying to assign the variable within an if statement, but I do not know how to fix it or at the very least remove the boolean assignment outside the if statement.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Kyle 02
  • 13
  • 4

1 Answers1

1

Yes, you can't try to compare a boolean with no value. You could assign your test variable to false by default, or move your Console.WriteLine("Random text"); directly in your first if if possible.