-1

Hi I have this code and it is keep saying that

cannot implicitly convert type 'int' to 'bool' "

for every each "month = 1", "month = 2" etc... Can someone help?

public static string NumberToMonth(int month)
{
    if (month = 1)
        return "január";
    if (month = 2)
        return "február";
    if (month = 3)
        return "március";
    if (month = 4)
        return "április";
    if (month = 5)
        return "május";
    if (month = 6)
        return "június";
    if (month = 7)
        return "július";
    if (month = 8)
        return "augusztus";
    if (month = 9)
        return "szeptember";
    if (month = 10)
        return "október";
    if (month = 11)
        return "november";
    if (month = 12)
        return "december";        
    else
        return "";
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Gábor Stefler
  • 41
  • 1
  • 1
  • 7
  • 4
    `month ==` instead of `month =` for all your expressions. – Hatted Rooster May 06 '17 at 19:14
  • Now I have realised omfg and i didnt realised that. Shame on me :( But thank You. – Gábor Stefler May 06 '17 at 19:16
  • 2
    Completely orthogonal to the error message and your question but be aware that the globalization system in .NET can give you the names of the months without you hardcoding them in your program. In particular, your entire method can be replaced with this: `public static string NumberToMonth(int month) { return CultureInfo.GetCultureInfo("hu").GetMonthName(month); }`. If, on the other hand, you want the current regional setting month name, for display purposes to the user, simply use `CultureInfo.CurrentUICulture.DateTimeFormat.GetMonthName(month)` instead. – Lasse V. Karlsen May 06 '17 at 19:22
  • I realised that after I wrote my code but now I dont want to change it beause work was done into that code if you know what I mean. – Gábor Stefler May 06 '17 at 20:18
  • Does this answer your question? [Error on if statement - cannot implicitly convert type to 'bool'](https://stackoverflow.com/questions/871530/error-on-if-statement-cannot-implicitly-convert-type-to-bool) – Drag and Drop Oct 30 '19 at 10:50

3 Answers3

3

Use == and not =.

  1. The = is the assigning operator. In this line if(month = 1) you are assigning 1 into month.
  2. The == is the equality operator to check if two values/objects are equal.

Code should be:

if (month == 1)
        return "január";

When you use = then:

  • You assign the value to month so now month contains the value 1.
  • Then month is evaluated by the if statement.
  • if statement basically gets if(1) which expects a boolean and not an int.
  • There is no implicit conversion from int to bool and thus the compile error.

Instead of writing your own method to retrieve month names use the built in: See this answer

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(8);
Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • You should also mention that an "assignment expression", such as `month = 1`, also *evaluates to* the assigned value, which is why there isn't a completely different compiler message here, the expression has the value 1, which is an int, thus the error message. To be honest I think the C# compiler guys could've done a better job of identifying the *probable* underlying problem here and giving hints. – Lasse V. Karlsen May 06 '17 at 19:26
  • It doesn't throw an exception though, as your answer suggests. – Hatted Rooster May 06 '17 at 19:27
2

The solution to your problem is already answered.

The reason for the message “cannot implicitly convert type 'int' to 'bool' ” is that month = 1 is an expression that

  1. assigns the value 1 to the variable month, and
  2. returns the assigned value (1)

so the compiler sees if (1), which means that there is an integer value where a boolean value was expected. So it complains about that.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
1

= is the assignment operator, so the expression month = 1 is trying to assign the value of 1 to month. What you want is ==, which is the equality operator. So you'd change your if conditions to be of the following form:

if (month == 1)
{
    return "január";
}

As to the reason for the error message, this is because you're performing an assignment, instead of a comparison. In plain English, you're doing the following:

  1. The value 1 is assigned to month.
  2. The if condition now reads: if 1 is true.

So you're basically asking a question that cannot be answered. Programmatically, what's happening is the following:

  1. You're assigning the value of 1 to month.
  2. The if condition then becomes if (1), where 1 is an int, but a condition should evaluate to either true or false (i.e. a bool).

Therefore, the compiler is complaining because it expects a bool, but you're giving it an int.

John H
  • 14,422
  • 4
  • 41
  • 74