Error "Cannot implicitly convert type 'int' to 'string'"
I was doing this simple code snippet to generate the Month name by using the Day Number but I got the error Cannot implicitly convert type 'int' to 'string'
. I'm not trying to change MonthName into int!
class Module04
{
public static void Exercise01()
{
Console.WriteLine("Please Enter a day n开发者_开发百科umber between 1 and 365: ");
string line = Console.ReadLine();
int dayNum = int.Parse(line);
int monthNum = 0;
if (dayNum <= 31) // January
{
goto End;
}
else
{
dayNum -= 31;
monthNum++;
}
if (dayNum <= 28) // February
{
goto End;
}
else
{
dayNum -= 28;
monthNum++;
}
if (dayNum <= 31) // March
{
goto End;
}
else
{
dayNum -= 31;
monthNum++;
}
if (dayNum <= 30) // April
{ goto End; }
else
{
dayNum -= 30;
monthNum++;
}
if (dayNum <= 31) // May
{ goto End; }
else
{
dayNum -= 31;
monthNum++;
}
if (dayNum <= 30) // June
{ goto End; }
else
{
dayNum -= 30;
monthNum++;
}
if (dayNum <= 31) // July
{ goto End; }
else
{
dayNum -= 31;
monthNum++;
}
if (dayNum <= 31) // August
{ goto End; }
else
{
dayNum -= 31;
monthNum++;
}
if (dayNum <= 30) // September
{ goto End; }
else
{
dayNum -= 30;
monthNum++;
}
if (dayNum <= 31) // October
{ goto End; }
else
{
dayNum -= 31;
monthNum++;
}
if (dayNum <= 30) // November
{ goto End; }
else
{
dayNum -= 30;
monthNum++;
}
if (dayNum <= 31) // December
{ goto End; }
else
{
dayNum -= 31;
monthNum++;
}
End:
string monthName;
switch (monthName)
{
case 0:
monthName = "January"; break;
case 1:
monthName = "February"; break;
case 2:
monthName = "March"; break;
case 3:
monthName = "April"; break;
case 4:
monthName = "May"; break;
case 5:
monthName = "June"; break;
case 6:
monthName = "July"; break;
case 7:
monthName = "August"; break;
case 8:
monthName = "September"; break;
case 9:
monthName = "October"; break;
case 10:
monthName = "November"; break;
case 11:
monthName = "December"; break;
default:
monthName = "Not yet Done"; break;
}
Console.WriteLine("{0} {1}", dayNum, monthName);
}
}
It's the switch
where you're using it as an int - You probably meant to pass monthNum
to the switch statement.
(Also: Shouldn't you have monthNum == ...
in all of the if
s in the first part?)
Change the switch to
switch (monthNum)
You are doing a switch on monthName, with int cases, thus c# try's to convert the string to int to match the cases.
http://msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx
monthName is a string so your switch cases should be a string also.
string monthName;
switch (monthName)
{
case "0" :
monthName = "January"; break;
case "1" :
monthName = "February"; break;
case "2" :
monthName = "March"; break;
case "3" :
monthName = "April"; break;
case "4" :
monthName = "May"; break;
case "5" :
monthName = "June"; break;
case "6" :
monthName = "July"; break;
case "7" :
monthName = "August"; break;
case "8" :
monthName = "September"; break;
case "9" :
monthName = "October"; break;
case "10" :
monthName = "November"; break;
case "11" :
monthName = "December"; break;
default :
monthName = "Not yet Done"; break;
}
Console.WriteLine("{0} {1}", dayNum, monthName);
}
精彩评论