DateTime

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

Hi,

I have a class that has some fields. One of the fields is of type
DateTime. From the constructor of the class, I am passing
DateTime.Now; The value being stored is the following: 01/01/0001
00:00:00

Thats not the correct date and time.

Then when I try Console.WriteLine(DateTime.Now), the correct date and
time is given.


Can someone help me figure out the problem
Thanks in Advance
 
Sounds like you don't assign the DateTime to the member field in your
constructor. '01/01/0001 00:00:00' is the default value of the DateTime
struct.

/Johan
 
Sounds like you don't assign the DateTime to the member field in your
constructor. '01/01/0001 00:00:00' is the default value of the DateTime
struct.

/Johan

Deos your constructor look something like this?

class Foo
{
DateTime date;
public Foo(DateTime date)
{
date = date;
}
}

if thats the case you are assigning the parameter to itself. To set the
member field prefix the variable you are assigning to with "this." this
tells the compiler what you actually intend to do rather than it trying to
guess from the context - i.e.

class Foo
{
DateTime date;
public Foo(DateTime date)
{
this.date = date;
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
Back
Top