473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

null DateTime variables

Hi:

Another C# newby question:

How do I declare a null DateTime variable:

DateTime myDate = null;

I get the error:

"Cannot convert null to 'System.DateTime' because it is a value type"

(If I don't set a date then the value defaults to "1/1/0001")

I'm converting a VB.NET app to C# and I was able to declare null dates in
that language. Is there another Date type that will allow this - if not
then what is the best way to handle this - should I keep checking for 1/1/1
and consider that to be "null".

Thanks for your help!

Fred
Nov 17 '05 #1
2 17686
On Thu, 19 May 2005 09:21:18 -0400, Fred Nelson wrote:

Hi Fred
How do I declare a null DateTime variable:

DateTime myDate = null;

I get the error:

"Cannot convert null to 'System.DateTime' because it is a value type"

(If I don't set a date then the value defaults to "1/1/0001")

I'm converting a VB.NET app to C# and I was able to declare null dates in
that language. Is there another Date type that will allow this - if not
then what is the best way to handle this - should I keep checking for 1/1/1
and consider that to be "null".


Well I don't know VB.NET really good, are you sure it is possible there?

Indeed as you already found out in C# that is not possible (it will be in
..NET 2.0 with nullable Types).

I'd say best way to do it is by treading DateTime.MinDate (what is 1/1/1)
as null value.

hth
--
Claudio Grazioli
http://www.grazioli.ch
http://www.grazioli.ch/HommingbergerGepardenforelle/
Nov 17 '05 #2
Claudio:

Thanks for your help - I am comparing against DateTime.MinDate and it's
working just fine!

Fred
"Claudio Grazioli" <ne********@gmx-ist-cool.de> wrote in message
news:1u******************************@40tude.net.. .
On Thu, 19 May 2005 09:21:18 -0400, Fred Nelson wrote:

Hi Fred
How do I declare a null DateTime variable:

DateTime myDate = null;

I get the error:

"Cannot convert null to 'System.DateTime' because it is a value type"

(If I don't set a date then the value defaults to "1/1/0001")

I'm converting a VB.NET app to C# and I was able to declare null dates in that language. Is there another Date type that will allow this - if not
then what is the best way to handle this - should I keep checking for 1/1/1 and consider that to be "null".


Well I don't know VB.NET really good, are you sure it is possible there?

Indeed as you already found out in C# that is not possible (it will be in
.NET 2.0 with nullable Types).

I'd say best way to do it is by treading DateTime.MinDate (what is 1/1/1)
as null value.

hth
--
Claudio Grazioli
http://www.grazioli.ch
http://www.grazioli.ch/HommingbergerGepardenforelle/

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Manish Jain | last post by:
Platform : ASP.Net/C# void SomeFunction(DateTime date) { string text = date.ToString(); //This crashes if date is null } I am using a construct similar to above. I want to check if the date...
5
by: Rene | last post by:
I have my object model thing going with some classes that contain properties that are saved into a database. Some of these properties are *numeric* properties that can be left Null in the database...
4
by: wapsiii | last post by:
I'm unsure how best to handle dates from my webforms to my database. Often I have a textbox and a datepicker on a webform. Upon postback dates are received in string format (like DD-MM-YYYY)....
1
by: GB | last post by:
Is there a function to set a null date in vb.net? Normally, I create a const NULLDATE = "1/1/1900", but that becomes a drag at times. Especially, handling a nulldate from SQL into a dataset. ...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
4
by: mflanagan | last post by:
I think I've figured out my interop problems, but I'd like some confirmation that I'm not going down the wrong path. I'm connecting to a web service over which I have no control, and it's...
2
by: Mark | last post by:
Hi. I am making a user control right now, and it looks something like this: <script runat="server"> public string SelectCommand { set { // see below for why the following line is here.
5
by: AAJ | last post by:
Hi Does anyone know of any good publically available set of standards for managing dates when dealing with a database server (in my case SQL Server 2000 and c# VS2005). At the moment, if I...
1
by: huks | last post by:
Hi, I have a PL1 program to load a DB2 table with datetime zone (null allowed) Usually when i have to load null values, I used nullindicator with -1 value but here, with a datetime variable i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.