DateTime

S

shapper

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
 
K

Karl Seguin [MVP]

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
 
M

Mark Rae

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top