Problem with datetime constructor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Why this?
? new DateTime(2005, 1, 1).tostring
"01/01/0001 0.00.00"

I think the right output is "01/01/2005 0.00.00"...

I've some tests on a property set routine, as

If Value > New DateTime(2025, 1, 1) Then Throw New
ArgumentOutOfRangeException("registrationDateTime must be before gen 1, 2025")

and I cannot make work them!

thanks
 
Jon Skeet said:
And that's the output on my box.
:(((((

Could you post a short but complete program which demonstrates the
problem?

This is not simple, because the code is inside a buiness object used in a
3-tiers web-based application.

This is the property:
Public Property RegistrationDateTime() As DateTime
Get
Return _registrationDateTime
End Get
Set(ByVal Value As DateTime)

If Value = Nothing Then Throw New
ArgumentNullException("RegistrationDateTime")
If Value < New DateTime(2005, 1, 1) Then Throw New
ArgumentOutOfRangeException("registrationDateTime must be after gen, 1 2005")
If Value > New DateTime(2025, 1, 1) Then Throw New
ArgumentOutOfRangeException("registrationDateTime must be before gen, 1 2025")

_registrationDateTime = Value

Me.MarkAsModified()

End Set
End Property

Because value is #1/15/2004# I've an error on the second test. If I execute
on command window the code I previously posted, I've the wrong output. It
seems I've some very very strange problem with datetime behavior..

thanks
 
Only problem I see is that when I tried it with C#, the compiler told me that
I can't compare the DateTime value with null (nothing).

If yours compiles, then I don't know.
 
Trapulo said:
This is not simple, because the code is inside a buiness object used in a
3-tiers web-based application.

So take the code out of the business object, and try to show the
problem in a console app.

I can't see anything in your code which would stop it from being part
of a simple console app...

Because value is #1/15/2004# I've an error on the second test. If I execute
on command window the code I previously posted, I've the wrong output. It
seems I've some very very strange problem with datetime behavior..

I'd avoid using the command window for this kind of thing - try to
reproduce it in a standalone way which doesn't involve the debugger.
 
Oh my GOD!

I solved, and you are right.

The problem was a stupid error with data value. The error was raised by the
FIRST test, but the VS debugger reported the yellow-background line on the
second. So I was thinking the problem was on the second, but was on the
first test.

Anyway, the command window has this strange and wrong behavior. It reports a
bad value with my quick-test command, so I think there is some bug. At
runtime is all ok.

Thanks!
 
Trapulo,
In addition to the other comments.
on command window the code I previously posted, I've the wrong output. It

Seems to be one of many limitations of the Command Window, If you try the
code in VB.NET it performs as expected.


I would recommend you change to function to include the "value" parameter as
part of the exceptions,
allowing you can see exactly what the value of "value" is when it fails.


Something like:

Public Property RegistrationDateTime() As DateTime
Get
Return _registrationDateTime
End Get
Set(ByVal value As DateTime)

If value = Nothing Then Throw New ArgumentNullException("value",
"RegistrationDateTime")
If value < New DateTime(2005, 1, 1) Then Throw New
ArgumentOutOfRangeException("value", value, "registrationDateTime must be
after gen, 1 2005")
If value > New DateTime(2025, 1, 1) Then Throw New
ArgumentOutOfRangeException("value", value, "registrationDateTime must be
before gen, 1 2025")

_registrationDateTime = value

End Set
End Property

Hope this helps
Jay
 
Thanks for your suggestion!

Jay B. Harlow said:
Trapulo,
In addition to the other comments.
It

Seems to be one of many limitations of the Command Window, If you try the
code in VB.NET it performs as expected.


I would recommend you change to function to include the "value" parameter as
part of the exceptions,
allowing you can see exactly what the value of "value" is when it fails.


Something like:

Public Property RegistrationDateTime() As DateTime
Get
Return _registrationDateTime
End Get
Set(ByVal value As DateTime)

If value = Nothing Then Throw New ArgumentNullException("value",
"RegistrationDateTime")
If value < New DateTime(2005, 1, 1) Then Throw New
ArgumentOutOfRangeException("value", value, "registrationDateTime must be
after gen, 1 2005")
If value > New DateTime(2025, 1, 1) Then Throw New
ArgumentOutOfRangeException("value", value, "registrationDateTime must be
before gen, 1 2025")

_registrationDateTime = value

End Set
End Property

Hope this helps
Jay
 
Back
Top