demonstrates a custom constructor function for a structure : struct : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » struct »

 

demonstrates a custom constructor function for a structure









    

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//  tm2.cs - demonstrates a custom constructor function for a structure
//           Compile this program using the following command line:
//               D:>csc tm2.cs
//
namespace nsStructure
{
    using System;
    using System.Globalization;
    struct tm
    {
        public tm (DateTime tmVal)
        {
            tm_sec = tmVal.Second;
            tm_min = tmVal.Minute;
            tm_hour = tmVal.Hour;
            tm_mday = tmVal.Day;
            tm_mon = tmVal.Month - 1;
            tm_year = tmVal.Year - 1900;
            tm_wday = (int) tmVal.DayOfWeek;
            tm_yday = tmVal.DayOfYear;
            TimeZone tz = TimeZone.CurrentTimeZone;
            tm_isdst = tz.IsDaylightSavingTime (tmVal) == true ? 1 : 0;
        }
        public int tm_sec;       // Seconds after the minute
        public int tm_min;       // Minutes after the hour 
        public int tm_hour;      // Hours since midnight
        public int tm_mday;      // The day of the month
        public int tm_mon;       // The month (January = 0)
        public int tm_year;      // The year (00 = 1900)
        public int tm_wday;      // The day of the week (Sunday = 0)
        public int tm_yday;      // The day of the year (Jan. 1 = 1)
        public int tm_isdst;     // Flag to indicate if DST is in effect
        public override string ToString()
        {
            const string wDays = "SunMonTueWedThuFriSat";
            const string months = "JanFebMarAprMayJunJulAugSepOctNovDec";
            return (String.Format ("{0} {1} {2,2:00} " + 
                            "{3,2:00}:{4,2:00}:{5,2:00} {6}\n", 
                             wDays.Substring (3 * tm_wday, 3),
                             months.Substring (3 * tm_mon, 3),
                             tm_mday, tm_hour, tm_min,
                             tm_sec, tm_year + 1900));
        }
    }
    public class tm2Demo
    {
        static public void Main()
        {
            DateTime timeVal = DateTime.Now;
            tm tmNow = new tm (timeVal);
            Console.WriteLine (tmNow);
        }
    }
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» struct