Reputation: 25
I have a variable set in C# that changes depending on which If statement is selected.
But when I try to Console.WriteLine the variable it tells me the variable does not exist in the current context, could someone help me with this please?
public void mood()
{
var unhappiness = Hunger + Boredom;
if (unhappiness < 5)
{
string m = "Happy";
}
if (unhappiness <= 5 && unhappiness <= 10)
{
string m = "Okay";
}
if (unhappiness <= 11 && unhappiness <= 15)
{
string m = "Frustrated";
}
if (unhappiness <= 16)
{
string m = "Mad";
}
Console.WriteLine(m);
}
The variable 'm' is the one I'm having trouble with.
Upvotes: 0
Views: 391
Reputation: 5822
you need to declare string m outside - after your method decleration:
public string mood()
{
var unhappiness = Hunger + Boredom;
string m = string.Empty;
if (unhappiness < 5)
{
m = "Happy";
}
if (unhappiness <= 5 &&
unhappiness <= 10)
{
m = "Okay";
}
if (unhappiness <= 11 &&
unhappiness <= 15)
{
m = "Frustrated";
}
if (unhappiness <= 16)
{
m = "Mad";
}
return m;
Upvotes: 3
Reputation:
To fix your issues you can do something like this:
public void mood()
{
var unhappiness = Hunger + Boredom;
string m = "Unknown";
if (unhappiness < 5)
{
m = "Happy";
}
else if (unhappiness >= 5 && unhappiness <= 10) // >= 5 not <= 5
{
m = "Okay";
}
else if (unhappiness > 11 && unhappiness <= 15) // > 10 or >= 11 not <= 11
{
m = "Frustrated";
}
else if (unhappiness >= 16) // assume it should be enything else
{
m = "Mad";
}
Console.WriteLine(m);
}
Upvotes: 0
Reputation: 9323
As it is written, the variable m
only exists within the scope of each if
statement.
You should declare m
within the global scope of the method:
public void mood()
{
string m = "default mood";
// ...
}
This will make it available to all child scopes (delimited by the curly braces), including each of the if
statements.
In the code that you wrote initially, each time you wrote string m
in a different scope, you were declaring a new variable called m
. Each time you reached the end of the scope, that variable was destroyed. So when you got to the WriteLine
statement, m
effectively did not exist.
Upvotes: 0
Reputation: 22624
m
doesn't exist. That's why you can't print it out or do anything with it. It doesn't exist. There is no such variable as m
.
In some inner scopes, there happened to be several variables which were called m
. But they were gone as long as the block in which they were defined was exited. Now they belong to a past which can not return. Their memories drifted away like drops in an ocean of garbage collection.
Upvotes: 0
Reputation: 5161
string m is defined inside your if-clauses. Also, you cannot return a string from your method, so i changed that :)
try
public string mood()
{
var unhappiness = Hunger + Boredom;
string m = string.Empty;
if (unhappiness < 5)
{
m = "Happy";
}
// etc
return m;
}
Upvotes: 0
Reputation: 4381
Try this instead :
public void mood()
{
var unhappiness = Hunger + Boredom;
string m = string.Empty;
if (unhappiness < 5)
{
m = "Happy";
}
if (unhappiness >= 6 && unhappiness <= 10)
{
m = "Okay";
}
if (unhappiness >= 11 && unhappiness <= 15)
{
m = "Frustrated";
}
if (unhappiness >= 16)
{
m = "Mad";
}
Console.WriteLine(m);
}
The problem was that m
was defined inside an if
statement, it scope was limited to that statement.
Upvotes: 1
Reputation: 59
Define string M before the if-statements. If you initialize it just after var unhappiness, and set it within the if-statements. You define string m within the if-statements, but when they end, string m is gone as well.
Upvotes: 1
Reputation: 8763
public void mood()
{
var unhappiness = Hunger + Boredom;
string m;
if (unhappiness < 5)
{
m = "Happy";
}
if (unhappiness <= 5 &&
unhappiness <= 10)
{
m = "Okay";
}
if (unhappiness <= 11 &&
unhappiness <= 15)
{
m = "Frustrated";
}
if (unhappiness <= 16)
{
m = "Mad";
}
Console.WriteLine(m);
Upvotes: 0
Reputation: 4526
This:
{
}
Is putting your variable inside that scope, so it won't be available outside of it. That's why you can declare 4 variables named m
like you just did. Each m
is a different guy, inside its little bracket world.
I think you may want to declare it beforehand:
string m="";
if (unhappiness < 5)
{
m = "Happy";
}
if (unhappiness <= 5 &&
unhappiness <= 10)
{
m = "Okay";
}
if (unhappiness <= 11 &&
unhappiness <= 15)
{
m = "Frustrated";
}
if (unhappiness <= 16)
{
m = "Mad";
}
Upvotes: 3