Passing a null value to a DateTime property

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

I have a class with several properties, one of which is of the DateTime
datatype. However, this property will not always have a value, and I need to
check later whether it has a value or not.

When I create a new instance of the class, this property acquires a default
value of {1/1/1}, which I don't want at all, as that is a perfectly valid
date.

Is there any way to cause a DateTime datatype to have no value at all, which
I can check later?

Or is the answer not to use a DataTime datatype and use a generic object
datatype instead?

Any assistance gratefully received.

Mark
 
Dates are value types, not reference types, so they can not be null, they
hold always a value. Could you use DateTime.MinValue as a special value to
denote no value?

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Mark,

In .NET 2.0, you would use the Nullable<T>, with the type parameter T
equal to DateTime. This will produce a structure which has a HasValue
property which is true if there is a value, false if it is null.

If you are using a version of .NET before 2.0, then you could use the
SqlDateTime, which supports null semantics.

Hope this helps.
 
In .NET 2.0, you would use the Nullable<T>, with the type parameter T
equal to DateTime. This will produce a structure which has a HasValue
property which is true if there is a value, false if it is null.

Still using v1.1
If you are using a version of .NET before 2.0, then you could use the
SqlDateTime, which supports null semantics.

I'll try that - thanks.
 
You could also write your own class containing a DateTime that would then be
nullable. This might be easier than having to reference System.Data to use
SqlDateTime.

class MyDateTime
{
public MyDateTime(DateTime dt) { this._dt = dt; }

private DateTime _dt;

public DateTime Value { get { return this._dt; } }
};
 
You could also write your own class containing a DateTime that would then
be
nullable. This might be easier than having to reference System.Data to use
SqlDateTime.

class MyDateTime
{
public MyDateTime(DateTime dt) { this._dt = dt; }

private DateTime _dt;

public DateTime Value { get { return this._dt; } }
};

Hmm - that looks rather good! Thanks very much :-)
 
Just for completeness, C# also allows declaring a nullable type like
this:

int? NullableInteger;

The ? denotes a nullable type.

VB does not have a similar syntax.
 
Just for completeness, C# also allows declaring a nullable type like
this:

int? NullableInteger;

The ? denotes a nullable type.

VB does not have a similar syntax.

Interesting - could I apply that here...?

public class CSecurityClassn
{
private DateTime pMaturityDate;
public DateTime MaturityDate
{
get {return pMaturityDate;}
set {pMaturityDate = value;}
}
}
 
If this were VS.2005 then you wouldn't need a special class. You could
just do this:

DateTime? pNullableDate;

if pNullableDate.HasValue .....
 
This applies only to VC#.2005

That's what I thought. I've been using 2005 for a while now at home, but
none of my clients is willing to upgrade to a beta product, unsurprisingly!
 

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

Back
Top