home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Console Thyself / CsDateConstructors / CsDateConstructors.cs next >
Encoding:
Text File  |  2001-01-15  |  2.9 KB  |  110 lines

  1. //-----------------------------------------------
  2. // CsDateConstructors.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------------
  4. using System;
  5.  
  6. class CsDateConstructors
  7. {
  8.      public static void Main()
  9.      {
  10.           try
  11.           {
  12.                Date mydate = new Date(2001, 8, 29);
  13.  
  14.                Console.WriteLine("Day of year = " + mydate.DayOfYear);
  15.           }
  16.           catch (Exception exc)
  17.           {
  18.                Console.WriteLine(exc);
  19.           }
  20.      }         
  21. }
  22. class Date
  23. {
  24.                                                             // Fields
  25.      int year;
  26.      int month;
  27.      int day;
  28.      static int[] MonthDays = new int[] {   0,  31,  59,  90, 120, 151,
  29.                                           181, 212, 243, 273, 304, 334 };
  30.  
  31.                                                             // Constructors
  32.      public Date()
  33.      {
  34.           Year = 1600;
  35.           Month = 1;
  36.           Day = 1;
  37.      }
  38.      public Date(int year, int month, int day)
  39.      {
  40.           if ( (month == 2 &&  IsLeapYear(year) && day > 29) ||
  41.                (month == 2 && !IsLeapYear(year) && day > 28) ||
  42.               ((month == 4 || month == 6 || 
  43.                               month == 9 || month == 11) && day > 30))
  44.           {
  45.                throw new ArgumentOutOfRangeException("Day");
  46.           }
  47.           else
  48.           {
  49.                Year  = year;
  50.                Month = month;
  51.                Day   = day;
  52.           }
  53.      }
  54.                                                             // Properties
  55.      public int Year
  56.      {
  57.           set
  58.           {
  59.                if (value < 1600)
  60.                     throw new ArgumentOutOfRangeException("Year");
  61.                else
  62.                     year = value;
  63.           }
  64.           get
  65.           {
  66.                return year;
  67.           }
  68.      }
  69.      public int Month
  70.      {
  71.           set
  72.           {
  73.                if (value < 1 || value > 12)
  74.                     throw new ArgumentOutOfRangeException("Month");
  75.                else
  76.                     month = value;
  77.           }
  78.           get
  79.           {
  80.                return month;
  81.           }
  82.      }
  83.      public int Day
  84.      {
  85.           set
  86.           {
  87.                if (value < 1 || value > 31)
  88.                     throw new ArgumentOutOfRangeException("Day");
  89.                else
  90.                     day = value;
  91.           }
  92.           get
  93.           {
  94.                return day;
  95.           }
  96.      }
  97.      public int DayOfYear
  98.      {
  99.           get
  100.           {
  101.                return MonthDays[month - 1] + day + 
  102.                                    (month > 2 && IsLeapYear(year) ? 1 : 0);
  103.           }
  104.      }
  105.                                                             // Method
  106.      public static bool IsLeapYear(int year)
  107.      {
  108.           return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
  109.      }
  110. }