473,416 Members | 1,552 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,416 software developers and data experts.

Passing a null value to a DateTime property

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
Nov 17 '05 #1
11 16400
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 Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> escribió en el mensaje
news:ev**************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ev**************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #3
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:OK**************@tk2msftngp13.phx.gbl...
Could you use DateTime.MinValue as a special value to denote no value?


Possibly - thanks for the answer.
Nov 17 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uM*************@TK2MSFTNGP15.phx.gbl...
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.
Nov 17 '05 #5
KH
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; } }
};

"Mark Rae" wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uM*************@TK2MSFTNGP15.phx.gbl...
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.

Nov 17 '05 #6
"KH" <KH@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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 :-)
Nov 17 '05 #7
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.

Nov 17 '05 #8
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
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;}
}
}
Nov 17 '05 #9
This applies only to VC#.2005

Nov 17 '05 #10
If this were VS.2005 then you wouldn't need a special class. You could
just do this:

DateTime? pNullableDate;

if pNullableDate.HasValue .....

Nov 17 '05 #11
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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!
Nov 17 '05 #12

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

Similar topics

3
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I am developing an application that utilizes a separate class library for data access to a SQL Server database. One of the tables has several...
9
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
4
by: js | last post by:
I have a stored procedure named "processInventory" like the following. Depending on the passed in parameters, I would like to add a WHERE clause for "select" action. For example, if any varchar...
2
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime...
7
by: MP | last post by:
Hello, I am trying to write a class that will expose some properties. One of the property is extracted from a SQL database and can be NULL or some integer value. Is there a elegant way of...
6
by: Markus Eßmayr | last post by:
Hello, I'd like to consume a WebService, which returns an array of objects which include several members of type System.String, System.Decimal and System.DateTime. In the WSDL-file, the members...
2
by: Fabiano | last post by:
Please, i have an webform, that work with a object that receives a DateTime value. I don't want to save date into it. How can i verify if it's value is null or not? Tks Fabiano
5
by: lextendo | last post by:
Hi all, I have a problem which seems it has to have a simple solution, though I can't figure it out and I can't find a solution on usenet either. Two forms: Form A contains a textbox where the...
0
by: tschroeder250 | last post by:
Hi Everyone, There are so many Nullable DateTimePickers out there that this post may not even be found among all the others, but I wanted to try. First, I want to thank all the authors of the...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.