---
title: "If statements in methods"  
description: "If statements in methods"  
author: "Anonymous User"  
published: 2013-11-13  
updated: 2013-11-13  
canonical: https://www.mindstick.com/forum/1714/if-statements-in-methods  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# If statements in methods

I have a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) set in C# that changes depending on which [If statement](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) is selected.

But when I try to Console.WriteLine, it tells me the variable does not exist in the current [context](https://www.mindstick.com/forum/23245/what-is-context-in-android), could [someone help me](https://answers.mindstick.com/qa/42311/can-someone-help-me-with-this-question-about-political-parties) 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);}
```

## Replies

### Reply by Anonymous User

Hi Ankit,\

You can try this:

```
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.


---

Original Source: https://www.mindstick.com/forum/1714/if-statements-in-methods

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
