Callum Houghton
Callum Houghton

Reputation: 25

If statements in methods

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

Answers (9)

Ahmed ilyas
Ahmed ilyas

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

user1064248
user1064248

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);
}
  • The definition of a variable and the usage of a variable has to occur in the same scope which is defined with the curly brackets { }.
    So you can use m in the whole method mood but not outside the method mood in this case.
  • if, else if comes in handy if you want to check something first and only if this results to false you want to check something else as a fallback.

Upvotes: 0

madd0
madd0

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

Daniel Daranas
Daniel Daranas

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

oerkelens
oerkelens

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

R&#233;da Mattar
R&#233;da Mattar

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

Rob van Barneveld
Rob van Barneveld

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

Derek
Derek

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

Conrad Clark
Conrad Clark

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

Related Questions