demonstrates overriding the ToString() method to provide a custom string output : ToString « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » ToStringScreenshots 
demonstrates overriding the ToString() method to provide a custom string output
demonstrates overriding the ToString() method to provide a custom string output

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

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//  tm1.cs - demonstrates overriding the ToString() method to provide a custom
//           string output.
//
//           Compile this program using the following command line:
//               D:>csc tm1.cs
//
namespace nsStructure
{
    using System;
    using System.Globalization;
    public struct tm
    {
        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 (* tm_wday, 3),
                             months.Substring (* tm_mon, 3),
                             tm_mday, tm_hour, tm_min,
                             tm_sec, tm_year + 1900));
        }
    }
    public class tm1
    {
        static public void Main()
        {
            DateTime timeVal = DateTime.Now;
            tm tmNow = LocalTime (timeVal);
            Console.WriteLine (tmNow);
        }
        static public tm LocalTime(DateTime tmVal)
        {
            tm time;
            time.tm_sec = tmVal.Second;
            time.tm_min = tmVal.Minute;
            time.tm_hour = tmVal.Hour;
            time.tm_mday = tmVal.Day;
            time.tm_mon = tmVal.Month - 1;
            time.tm_year = tmVal.Year - 1900;
            time.tm_wday = (inttmVal.DayOfWeek;
            time.tm_yday = tmVal.DayOfYear;
            TimeZone tz = TimeZone.CurrentTimeZone;
            time.tm_isdst = tz.IsDaylightSavingTime (tmVal== true 0;
            return (time);
        }
    }
}


           
       
Related examples in the same category
1.Illustrates how to override the ToString() methodIllustrates how to override the ToString() method
2.Demonstrate ToString()Demonstrate ToString()
3.class declaration maintains the time in 24-hour format and ToString Methodclass declaration maintains the time in 24-hour format and ToString Method
4.Format data in ToString method.Format data in ToString method.
5.Overriding the ToString() MethodOverriding the ToString() Method
6..NET Frameworks Overview:Custom Object Formatting.NET Frameworks Overview:Custom Object Formatting
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.