I am creating my own DateTime class within C# and for some reason my checks are not working. When I run the program and enter a date it always stops and reaches the last line which is on the "Day" method and says "ArgumentOutOfException. Is there anyway to solve this issue and to make my checks called "Value" working?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace date
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t\t\t\tNextDate Application\n\t\t\t\t\t-------------------------------------");
Console.WriteLine("please enter date as dd/MM/yyyy");
int day;
int month;
int year;
string[] read = Console.ReadLine().Split('/');
day = int.Parse(read[0]);
month = int.Parse(read[1]);
year = int.Parse(read[2]);
Date date = new Date(day, month, year);
Console.WriteLine("{0}/{1}/{2}", date.Day, date.Month, date.Year);
Console.ReadLine();
}
class Date
{
private int _month; // 1-12
private int _day; // 1-31 depending on month
private int _year;
public Date(int day, int month, int year)
{
Day = day;
Month = month;
Year = year;
}
public int Year
{
get { return _year; }
set
{
if (value >= 1820 && value <= 2020)
_year = value;
else
throw new ArgumentOutOfRangeException("year", value, "year out of range");
}
}
public int Month
{
get { return _month; }
set
{
if (value > 0 && value <= 12)
_month = value;
else
throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12");
}
}
public int Day
{
get { return _day; }
set
{
int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (value > 0 && value <= days[_month])
_day = value;
else if (_month == 2 && value == 29 &&
_year % 400 == 0 || (_year % 4 == 0 && _year % 100 != 0))
_day = value;
else
throw new ArgumentOutOfRangeException("Day", value, "Day is out of range");
}
}
}
}
}