468,315 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

DateTime

Hello,

I defined a DateTime variable:

Dim dt As New DateTime

How can I check if it is empty?

Basically I want to check if it was given to it a DateTime value or
not.

Thanks,

Miguel

Feb 23 '07 #1
5 2484
You can't. DataTime is a ValueType..it's stored on the stack and always has
a default value (like an int).

a hack solution is to assign it a value that'll never get used:

dim dt as datetime = datetime.MinValue

but that's a hack.

In 2.0, you can use a nullable type...

dim dt as Nullable(Of DateTime)

which you can then compare against nothing and do other stuff...still not
great, but the best solution.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"shapper" <md*****@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
Hello,

I defined a DateTime variable:

Dim dt As New DateTime

How can I check if it is empty?

Basically I want to check if it was given to it a DateTime value or
not.

Thanks,

Miguel
Feb 23 '07 #2
"shapper" <md*****@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
I defined a DateTime variable:

Dim dt As New DateTime

How can I check if it is empty?

Basically I want to check if it was given to it a DateTime value or
not.
You can't, at least, not 100% reliably. When a DateTime variable is
instantiated, its default value is DateTime.MinValue, not null. You could
check whether dt = DateTime.MinValue, but it will never be null (or empty)
because it is a value type.

..NET 2 introduced the concept of nullable datatypes e.g. DateTime? as well
as DateTime etc. However, when a DateTime? variable is instantiated, it also
has a default value of DateTime.MinValue - you can, though explicitly set a
DateTime? variable to null, which you can't do with a DateTime variable...
Feb 23 '07 #3
"Karl Seguin [MVP]" <ka********@removeopenmymindremovemetoo.andmenetwr ote
in message news:C5**********************************@microsof t.com...
In 2.0, you can use a nullable type...

dim dt as Nullable(Of DateTime)
Yes, but you will have to explicitly set it to Nothing first, won't you...?
Feb 23 '07 #4
not in C#..maybe vb.net..dunno.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Karl Seguin [MVP]" <ka********@removeopenmymindremovemetoo.andmenet >
wrote in message
news:C5**********************************@microsof t.com...
>In 2.0, you can use a nullable type...

dim dt as Nullable(Of DateTime)

Yes, but you will have to explicitly set it to Nothing first, won't
you...?
Feb 23 '07 #5
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>"Karl Seguin [MVP]" <ka********@removeopenmymindremovemetoo.andmenet >
wrote in message
news:C5**********************************@microso ft.com...
>>In 2.0, you can use a nullable type...

dim dt as Nullable(Of DateTime)

Yes, but you will have to explicitly set it to Nothing first, won't
you...?
"Karl Seguin [MVP]" <ka********@removeopenmymindremovemetoo.andmenetwr ote
in message news:4A**********************************@microsof t.com...
not in C#..maybe vb.net..dunno.
Oh sure - not a problem at all in C#...

DateTime? dt = null;
Feb 24 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Max M | last post: by
15 posts views Thread by Fritz Switzer | last post: by
3 posts views Thread by Andrew S. Giles | last post: by
6 posts views Thread by Ante Perkovic | last post: by
5 posts views Thread by I am Sam | last post: by
26 posts views Thread by Reny J Joseph Thuthikattu | last post: by
11 posts views Thread by Cor Ligthert | last post: by
9 posts views Thread by Phil B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.