473,382 Members | 1,705 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,382 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 17684
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.