bjarke15
bjarke15

Reputation: 85

Can't access bool variables in c#

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

Answers (8)

Lee Brindley
Lee Brindley

Reputation: 6482

Bar is a local variable in your code. You need to take it out the if statement block

Upvotes: 0

p.s.w.g
p.s.w.g

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

Dennis Traub
Dennis Traub

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

Tim S.
Tim S.

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

Corey Ogburn
Corey Ogburn

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

scott_fakename
scott_fakename

Reputation: 10789

Declare bar outside the scope of those ifs. When the block in which they are declared closes they are forgotten.

Upvotes: 0

martavoi
martavoi

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

tnw
tnw

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

Related Questions