Reputation: 48568
My question is not about how to solve this error(I already solved it) but why is this error with boolean value.
My function is
private string NumberToString(int number, bool flag)
{
string str;
switch(flag)
{
case true:
str = number.ToString("00");
break;
case false:
str = number.ToString("0000");
break;
}
return str;
}
Error is Use of unassigned local variable 'str'
. Bool can only take true or false. So it will populate str
in either case. Then why this error?
Moreover this error is gone if along with true and false case I add a default
case, but still what can a bool hold apart from true and false?
Why this strange behaviour with bool variable?
Upvotes: 3
Views: 23368
Reputation: 5227
Well, there's been good explanation for causes of this issue, but there is one more solution to the problem, which was not mentioned. Just put default
instead of second case
:
private string NumberToString(int number, bool flag)
{
string str;
switch (flag)
{
case true:
str = number.ToString("00");
break;
default:
str = number.ToString("0000");
break;
}
return str;
}
It leaves compiler with no further thoughts about flag
variable value as we always assign a default
value in our switch
statement.
Also consider if-else statement equivalent solution:
private string NumberToString(int number, bool flag)
{
string str;
if (flag)
str = number.ToString("00");
else
str = number.ToString("0000");
return str;
}
I am not particularly fond of 'one-liners' (one line solutions), as they lack extendability and moreover - readability, but as the last resort just use ternary operators like this:
private string NumberToString(int number, bool flag)
{
return flag ? number.ToString("00") : number.ToString("0000");
}
While I am at it - consider using extension method + defaulting boolean to false
, for example:
public static class intExtensions
{
public static string NumberToString(this int number, bool flag = false)
{
return flag ? number.ToString("00") : number.ToString("0000");
}
}
and then in main class:
int intVal = 56;
string strVal1 = intVal.NumberToString(true);
string strVal2 = intVal.NumberToString();
Upvotes: 0
Reputation: 223267
I think, you are trying to ask, why str
variable is unassigned, as the switch statement's cases will assign it some value, but the compiler can't determine whether it will fall in any of the case statement, That is why you are getting this error on returning str
.
If you add a default case with string assignment, then the compiler will know for sure that, the str will hold some value and that is why you don't get error
Upvotes: 4
Reputation: 62256
The error you get is about string
variable and not boolean
possible values.
The fact that there is no way that noone of cases
run, is a true (in this case), but compiler doesn't go so far in analyzing the code. It just looks on variable that is not assigned and used in some conditions and there is not default one, so suppose that there could be some case when it remains unassigned.
Upvotes: 7
Reputation: 1038880
Writing a switch statement on a boolean variable seems kinda wasty to me. Why not use the conditional operator (?:
):
private string NumberToString(int number, bool flag)
{
return flag ? number.ToString("00") : number.ToString("0000");
}
The code seems a bit more concise and you don't need local variables.
But back to your question about why your code doesn't compile => it is because variables must always be assigned and this assignment should not happen inside conditional statements.
Upvotes: 9
Reputation: 3511
private string NumberToString(int number, bool flag)
{
string str = "";
switch(flag)
{
case true:
str = number.ToString("00");
break;
case false:
str = number.ToString("0000");
break;
}
return str;
}
write this string str = "";
- you should assign value
if you add default case there is no chance to fall through the switch cases without assigning. Now it is
Upvotes: 3