confused

  • Thread starter Robert Blackwell
  • Start date
R

Robert Blackwell

I don't understand this.

Example 8-4 declares a constructor for the Time class that accepts a single
argument, and object of type DateTime. DateTime is a type provided by the
..NET Framework Class Library.

it looks to me as though it's passing 6 single arguments all of type
int...it'd be crazy to believe that this is a typo, so please help me
understand

using System;

public class Time
{
// private member variables
int year;
int month;
int date;
int hour;
int minute;
int second;

// public method
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
month, date, year, hour, minute, second);
}

// constructor
public Time(int theYear, int theMonth, int theDate,
int theHour, int theMinute, int theSecond)
{
year = theYear;
month = theMonth;
date = theDate;
hour = theHour;
minute = theMinute;
second = theSecond;
}
}

public class Tester
{
static void Main()
{
Time timeObject = new Time(2005,3,25,9,35,20);
timeObject.DisplayCurrentTime();
}
}
 
M

Morten Wennevik

I don't understand this.

Example 8-4 declares a constructor for the Time class that accepts a single
argument, and object of type DateTime. DateTime is a type provided by the
.NET Framework Class Library.

Not sure what this example 8-4 is, but the Time class you show us takes 6 arguments.
It may be that it talks of an overloaded constructor taking a single DateTime parameter.
I added some code below to show this.
it looks to me as though it's passing 6 single arguments all of type
int...it'd be crazy to believe that this is a typo, so please help me
understand

using System;

public class Time
{
// private member variables
int year;
int month;
int date;
int hour;
int minute;
int second;

// public method
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
month, date, year, hour, minute, second);
}

// constructor
public Time(int theYear, int theMonth, int theDate,
int theHour, int theMinute, int theSecond)
{
year = theYear;
month = theMonth;
date = theDate;
hour = theHour;
minute = theMinute;
second = theSecond;
}

public Time(DateTime time)
{
year = time.Year;
month = time.Month;
date = time.Day; // ??? shouldn't this be day?
hour = time.Hour;
minute = time.Minute;
second = time.Second;
}
}

public class Tester
{
static void Main()
{
Time timeObject = new Time(2005,3,25,9,35,20);
timeObject.DisplayCurrentTime();

DateTime dateTimeObject = new DateTime(2005, 3, 25, 9, 35, 20);
Time timeObject2 = new Time(dateTimeObject);
timeObject2.DisplayCurrentTime();

Happy coding!
Morten Wennevik [C# MVP]
 
M

Mark Broadbent

You are correct the Time object is created by supplying 6 integer parameters
to the constructor.
Dont get confused with the legitimate DateTime datatype, what you have here
in the code sample is a "poor mans" date-time like brand new type (i.e. few
members and no validation on the inputs ..but it is only an example)

You are not using the DateTime data type anywhere in the code, all that is
being done (within Main) is declaring and creating an object of your
"custom" type {Time timeObject = new Time(2005,3,25,9,35,20);} and you are
simply calling this object instance's display method
{timeObject.DisplayCurrentTime();} which is writing the private objects
integer variables to the console (formatted as a string). Its quite simple
really.

Hope this helps/ Hope I explained it ok for you.



--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top