Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2010, 05:42 AM
Registered User
 
Join Date: Jan 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default DateTime type object : a doubt

Page 130 : we have

Code:
DateTime myTime = new DateTime();
this instruction is instantiating a DateTime object and initializating it via
the default constructor , right ?
And myTime is a reference to this particular object.
But in the second line we have

Code:
myTime = DateTime.Now ;
DateTime.Now is a property but also an object itself
and this is really confusing me : now myTime is a reference to another
object ? And if so , what happened to the first object ?


Thanks a lot for any explanation !
 
Old January 30th, 2010, 10:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

There are no objects involved at all, only values. Objects are instances of reference types. DateTime is a value type.

DateTime myTime = new DateTime();

loads the address of the local variable "myTime" onto the evaluation stack and initializes the value stored at that address to a DateTime struct whose fields are initialized to default values. The struct's static constructor is called.

myTime = DateTime.Now ;

pops the return value of the Now property off the top of the evaluation stack and stores it at the address of "myTime", overwriting the initial default values. "myTime" is just an alias for the address on the stack. Its value isn't a reference to any object on the heap. It's value is the struct itself. Assigning the return value of Now to that address simply overwrites any value already stored at that address.

You could just do:

DateTime myTime = DateTime.Now;

Last edited by Bob Bedell; January 30th, 2010 at 11:20 AM..
 
Old January 30th, 2010, 11:27 AM
Registered User
 
Join Date: Jan 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your kindness , Bob.
I was suspecting that the situation was a bit tricky
and at least now i'm SURE it is !

OOP concepts aren't really easy to grasp at the beginning .
However , your explanation is very clear.

Best regards

Piero ( pepitagold )
 
Old January 30th, 2010, 11:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Piero,

Yup, its tricky. Just stay on it.

Here's a couple of links you might find helpful that cover how reference types and values types get stored in managed memory. The Jon Skeet piece is a bit of a "classic".

http://www.yoda.arachsys.com/csharp/memory.html
http://www.c-sharpcorner.com/UploadF...rp_memory.aspx

HTH,

Bob

Last edited by Bob Bedell; January 30th, 2010 at 12:21 PM..
 
Old February 1st, 2010, 10:14 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default DateTime

Bob's correct in the way things get allocated on the stack. Also, if all you want is the current date and time, you don't even need to create myTime, just use the Now property.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
he conversion of a char data type to a datetime data type resulted in an out-of-range adamhw Classic ASP Basics 3 December 23rd, 2009 07:18 AM
sql datetime type angelboy C# 2005 0 April 16th, 2007 03:02 PM
Doubt in clone Method of Object Class pandian Java Basics 1 October 12th, 2006 03:29 PM
Problem with datetime type orange2p Crystal Reports 0 December 14th, 2005 12:44 PM
A doubt in DateTime field dhol General .NET 1 December 30th, 2004 01:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.