473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

confused

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();
}
}
Nov 16 '05 #1
3 1390
On Tue, 1 Jun 2004 18:56:16 -0700, Robert Blackwell <ho********@heeta.ho> wrote:
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]
Nov 16 '05 #2
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
=============
"Robert Blackwell" <ho********@heeta.ho> wrote in message
news:uB*************@TK2MSFTNGP10.phx.gbl...
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();
}
}

Nov 16 '05 #3
Thanks
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
2
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: ree32 | last post by:
I am a bit confused with capabilities of XML. I have an XML document with information on images(photos). Is there way to use XSL/XSLT to create a page that will display the images as gallery. ...
5
by: Jeff Amiel | last post by:
Yes, I've read the FAQ's... I'm still confused. I'm trying to help out a buddy to extract data from an .mdb file that has special 'permissions' on it. If I try to open it with the standard...
10
by: Lauren Wilson | last post by:
Ok I have searched the MS website for info on this. I am totally confused. If I want to deploy an Access 2003 app and allow my users to run it using Access 2003 Runtime, where do I get the...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.