![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
System.DateTime default value.
Does anyone happen to know off hand what the default value of a freshly instantiated System.DateTime value is? I always thought a DateTime value could be set to null but when I place checks for null, I receive warnings saying that it is unreachable code. So that also makes me wonder how you guys go about validating date/times.
|
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I set them to 0001-01-01 00:00:00 manually on creation (new DateTime(1, 1, 1);), and compare them against that.
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
Hmm. I have yet to actually validate or compare times, but I thought you had to use some other method or property with it. I been using System.DateTime. Now and .Year , month etc. DateTime is a type. Is it actually possible to have it set to null?
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I believe it is possible to set it to null - try it and see. If it is, I've been killing my processor without a reason.
![]() |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
I thought it was possible to, but I tried it and it is not.
"Error 1 Cannot convert null to 'System.DateTime' because it is a value type." |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
DateTime is a value type (therefore a struct) and structs can not normally be null. However there is a way in C# 2.0 to overcome this. Put a ? after the value type name and it will be nullable:
System.DateTime? time = null; int? x = null; float? f = null; However, there is no way to set a value type to null before C# 2.0 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
I haven't had time to check, but I am curious to know if its default value is System.DateTime.MinValue.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Sep 2004
Posts: 207
Rep Power: 5
![]() |
A quick check... 1/1/0001 12:00:00 AM
I'm thinking theres a better way to do what your trying to do though.
__________________
_______________________________ BlazingWolf |
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|