CSharp examples for Custom Type:Constructor
Create overloaded constructors.
using System; // for class ArgumentOutOfRangeException public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 // constructor can be called with zero, one, two or three arguments public Time2(int hour = 0, int minute = 0, int second = 0) {/*w w w. ja v a2 s .c o m*/ SetTime(hour, minute, second); // invoke SetTime to validate time } // Time2 constructor: another Time2 object supplied as an argument public Time2(Time2 time): this(time.Hour, time.Minute, time.Second) { } public void SetTime(int hour, int minute, int second){ Hour = hour; // set the Hour property Minute = minute; // set the Minute property Second = second; // set the Second property } public int Hour{ get{ return hour; } set{ if (value < 0 || value > 23){ throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(Hour)} must be 0-23"); } hour = value; } } // property that gets and sets the minute public int Minute { get { return minute; } set { if (value < 0 || value > 59) { throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(Minute)} must be 0-59"); } minute = value; } } // property that gets and sets the second public int Second { get { return second; } set { if (value < 0 || value > 59) { throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(Second)} must be 0-59"); } second = value; } } // convert to string in universal-time format (HH:MM:SS) public string ToUniversalString() => $"{Hour:D2}:{Minute:D2}:{Second:D2}"; // convert to string in standard-time format (H:MM:SS AM or PM) public override string ToString() => $"{((Hour == 0 || Hour == 12) ? 12 : Hour % 12)}:" + $"{Minute:D2}:{Second:D2} {(Hour < 12 ? "AM" : "PM")}"; } public class Time2Test { static void Main() { var t1 = new Time2(); // 00:00:00 var t2 = new Time2(2); // 02:00:00 var t3 = new Time2(21, 34); // 21:34:00 var t4 = new Time2(12, 25, 42); // 12:25:42 var t5 = new Time2(t4); // 12:25:42 Console.WriteLine("Constructed with:\n"); Console.WriteLine("t1: all arguments defaulted"); Console.WriteLine($" {t1.ToUniversalString()}"); // 00:00:00 Console.WriteLine($" {t1.ToString()}\n"); // 12:00:00 AM Console.WriteLine( "t2: hour specified; minute and second defaulted"); Console.WriteLine($" {t2.ToUniversalString()}"); // 02:00:00 Console.WriteLine($" {t2.ToString()}\n"); // 2:00:00 AM Console.WriteLine( "t3: hour and minute specified; second defaulted"); Console.WriteLine($" {t3.ToUniversalString()}"); // 21:34:00 Console.WriteLine($" {t3.ToString()}\n"); // 9:34:00 PM Console.WriteLine("t4: hour, minute and second specified"); Console.WriteLine($" {t4.ToUniversalString()}"); // 12:25:42 Console.WriteLine($" {t4.ToString()}\n"); // 12:25:42 PM Console.WriteLine("t5: Time2 object t4 specified"); Console.WriteLine($" {t5.ToUniversalString()}"); // 12:25:42 Console.WriteLine($" {t5.ToString()}"); // 12:25:42 PM // attempt to initialize t6 with invalid values try { var t6 = new Time2(27, 74, 99); // invalid values } catch (ArgumentOutOfRangeException ex) { Console.WriteLine("\nException while initializing t6:"); Console.WriteLine(ex.Message); } } }