Reputation: 85
The part of this code where it says if (bar = true)
it can't find the variable "bar" that I create in if (foo == "True")
or if (foo == "False")
.
Code:
string foo = Console.ReadLine();
if (foo == "True") {
bool bar = true;
}
if (foo == "False") {
bool bar = false;
}
if (bar = true) {
Console.WriteLine("This is true");
}
else {
Console.WriteLine("This is false");
}
Upvotes: 1
Views: 612
Reputation: 6482
Bar is a local variable in your code. You need to take it out the if statement block
Upvotes: 0
Reputation: 149020
You're declaring a new bar
inside each if
block, so it's not a valid identifier outside of that scope. You need to declare it outside of your if
blocks, like this
bool bar = false;
if (foo == "True")
{
bar = true;
}
if (foo == "False")
{
bar = false;
}
if (bar == true)
{
Console.WriteLine("This is true");
}
else
{
Console.WriteLine("This is false");
}
Or for that matter, this would work just as well:
bool bar = (foo == "True");
if (bar == true)
{
Console.WriteLine("This is true");
}
else
{
Console.WriteLine("This is false");
}
Or even:
Console.WriteLine("This is {0}", foo == "True");
Upvotes: 3
Reputation: 51634
Two things:
First, you define bar
inside each if
-block. It is not visible outside the respective if
-blocks. They are said to have a local scope.
Second, you musst use bar == true
instead of bar = true
. =
is the assignment operator, ==
is the equality operator.
The following will work:
string foo = Console.ReadLine();
bool bar;
if (foo == "True")
bar = true;
if (foo == "False")
bar = false;
if (bar == true)
Console.WriteLine("This is true");
else
Console.WriteLine("This is false");
Upvotes: 0
Reputation: 56536
A better way to parse:
string foo = Console.ReadLine();
bool bar;
if (!bool.TryParse(foo, out bar))
// inform the user, maybe have them try again
Console.WriteLine("This is {0}", bar);
Upvotes: 2
Reputation: 24717
You're creating bar inside 2 different if statements. Their scope is limited to those ifs. When execution leaves the ifs, neither bar is visible (hence why they don't conflict with each other). Try something more like this:
bool bar = false;
if (foo == "True")
{
bar = true;
}
if (foo == "False")
{
bar = false;
}
if (bar == true)
{
Console.WriteLine("This is true");
}
else
{
Console.WriteLine("This is false");
}
Or for all around better code:
bool bar = foo == "True";
Console.WriteLine("This is " + bar);
Upvotes: 1
Reputation: 10789
Declare bar outside the scope of those ifs. When the block in which they are declared closes they are forgotten.
Upvotes: 0
Reputation: 7092
bar
variable should be declared out of scope of if
/else
statement
bool bar;
if (foo == "True")
{
bar = true;
}
else
{
bar = false;
}
p.s. you can also use bool.Parse
method for assign bool value.
Upvotes: 0
Reputation: 13877
You're messing up the scope of your variables.
bar
only exists within the scope of your if
statement.
Declare it outside the first if
statement.
Also, this is not a comparison: if (bar = true)
This is: `if (bar == true)
Upvotes: 1