Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 15th, 2006, 9:09 AM   #1
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
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.
hoffmandirt is offline   Reply With Quote
Old May 15th, 2006, 1:11 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I set them to 0001-01-01 00:00:00 manually on creation (new DateTime(1, 1, 1);), and compare them against that.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 15th, 2006, 1:11 PM   #3
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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?
Booooze is offline   Reply With Quote
Old May 15th, 2006, 1:32 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 15th, 2006, 3:14 PM   #5
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
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."
hoffmandirt is offline   Reply With Quote
Old May 15th, 2006, 3:39 PM   #6
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
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
Kaja Fumei is offline   Reply With Quote
Old May 15th, 2006, 3:41 PM   #7
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
I haven't had time to check, but I am curious to know if its default value is System.DateTime.MinValue.
hoffmandirt is offline   Reply With Quote
Old May 15th, 2006, 5:33 PM   #8
BlazingWolf
Hobbyist Programmer
 
Join Date: Sep 2004
Posts: 207
Rep Power: 5 BlazingWolf is on a distinguished road
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
BlazingWolf is offline   Reply With Quote
Old May 16th, 2006, 1:09 AM   #9
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Quote:
Originally Posted by hoffmandirt
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."
That's what I figured. Like I said, I haven't had this problem, because I always set it to some date.
Booooze is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC