I'd like to code a program which user can input a month, then the program shows a number of days. Below is my code, but it shows an error " Class, struct, or interface method must have a return type". Please advise
class GetDays
{
string Month = 0;
public GetNumOfDay()
{
switch (Month)
{
case January: case March: case May: case July: case December:
Console.WriteLine("This month has 31 days {0}",Month);
break;
case April: case June: case September: case November:
Console.WriteLine("This month has 30 days {0}", Month);
break;
case Febuary:
if (Month / 400 != 0)
{
Console.WriteLine("This month has 28 days {0}", Month);
}
else Console.WriteLine("This month has 29 days {0}", Month);
default:
Console.WriteLine("Invalid month");
}
}
}
class Program
{
static void Main(string[] args)
{
GetDays obj = new GetDays();
obj.GetNumOfDay();
Console.ReadLine();
}
}
Loading
Jorge L FernandezPosted Nov 5, 2009, 11:27 AM
class GetDays
{
public string Month = "";
public GetDays(string Month)
{
this.Month = Month;
}
public void GetNumOfDay()
{
switch (this.Month)
{
case "January": case "March": case "May": case "July": case "December":
Console.WriteLine("This month has 31 days {0}",Month);
break;
case "April": case "June": case "September": case "November":
Console.WriteLine("This month has 30 days {0}", Month);
break;
case "February":
if (DateTime.Now.Year % 400 != 0)
{
Console.WriteLine("This month has 28 days {0}", Month);
}
else Console.WriteLine("This month has 29 days {0}", Month);
break;
default:
Console.WriteLine("Invalid month");
break;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the Month");
GetDays obj = new GetDays(Console.ReadLine());
obj.GetNumOfDay();
Console.ReadLine();
}
}
Jorge L FernandezPosted Nov 5, 2009, 2:03 PM
Example:
this.Month = Month;
the left part refers to the variable Month of your class (this.class) while the right refers to the variable Month passed as parameters. If you dont put "this" then the variable passed as parameters takes precedence. Also notice that this happens when they have the same name.
-> string Month = ""; means that when the struct is created this variable will have an empty string. Just for convenience with your code.
Keep coding and we'll keep learning. Thanks
Jorge
Kirtan PatelPosted Nov 5, 2009, 12:01 PM
you can get Days of Month in Just Two Lines of Code No need of So long Complex Code
using System.Threading;
int monthNum = 1;
int Days = Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar.GetDaysInMonth(DateTime.Now.Year, monthNum);
Console.WriteLine(Days.ToString());
KhoiPosted Nov 5, 2009, 11:42 AM
I'd like to ask some questions below:
1) public string Month = "";
-> why we use Month as blank?
2) this.Month = Month;
-> I don't' understand this. Please explain. Thanks
The rest of the code is ok :)
Please advise