Maha

Maha

  • 1.5k
  • 600
  • 56.6k

Constructor body

Dec 2 2014 8:06 AM
In this program statement in the constructor method body is expressed in a different way. I wish to know whether this is unique for the DateTime only. Problem is highlighted.

using System;

namespace MethodOverloading
{
public class Time1
{
// private member variables
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;

// public accessor methods
public void DisplayCurrentTime()
{
Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second);
}

// constructors
public Time1(DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}

public Time1(int Year, int Month, int Date, int Hour, int Minute, int Second)
{
this.Year = Year;
this.Month = Month;
this.Date = Date;
this.Hour = Hour;
this.Minute = Minute;
this.Second = Second;
}
}

public class MethodOverloadingTester
{
public void Run()
{
DateTime currentTime = DateTime.Now;

Console.WriteLine(currentTime);

Time1 time1 = new Time1(currentTime);

time1.DisplayCurrentTime();

Time1 time2 = new Time1(2000, 11, 18, 11, 03, 30);
time2.DisplayCurrentTime();
}

static void Main()
{
MethodOverloadingTester t = new MethodOverloadingTester();
t.Run();

Console.Read();
}
}
}
/*
9/17/2008 8:25:28
11/18/2000 11:3:30
*/


Answers (8)