将null值传递给DateTime属性 [英] Passing a null value to a DateTime property
问题描述
我有一个包含多个属性的类,其中一个属于DateTime
数据类型。但是,这个属性并不总是有价值,我需要
稍后检查它是否有值。
当我创建一个新的该类的实例,此属性获取{1/1/1}的默认值
,我根本不需要,因为这是完全有效的
date。
有没有办法让DateTime数据类型根本没有值,
我可以稍后查看?
或者是不使用DataTime数据类型并使用通用对象
数据类型的答案?
感谢任何帮助。
Mark
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
推荐答案
日期是值类型,而不是引用类型,因此它们不能为null ,他们
始终保持一个价值。你可以使用DateTime.MinValue作为特殊值吗
表示没有值?
-
Best问候,
Carlos J. Quintero
MZ-Tools:Visual Studio .NET,VB6,VB5和VBA的生产力加载项
您可以更快地编码,设计和记录。
加载项开发人员的免费资源:
http://www.mztools.com
" Mark Rae" <毫安** @ mark-N-O-S-P-A-M-rae.co.uk> escribióenel mensaje
news:ev ************** @ TK2MSFTNGP14.phx.gbl ...
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...
我有一个包含多个属性的类,其中一个是DateTime
数据类型。但是,此属性并不总是有值,我需要稍后检查它是否有值。
当我创建类的新实例时,此属性获取
默认值{1/1/1},我根本不需要,因为这是一个完全有效的日期。
有什么办法吗?导致DateTime数据类型根本没有值,
我可以稍后检查?
或者是不使用DataTime数据类型并使用通用对象的答案
数据类型相反?
感激不尽的任何帮助。
Mark
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
Mark,
在.NET 2.0中,您将使用Nullable< T>,类型参数T
等于DateTime。这将生成一个具有HasValue
属性的结构,如果有值则为true,如果为null则为false。
如果您使用的是在2.0之前的.NET版本,然后你可以使用
SqlDateTime,它支持null语义。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv *@spam.guard.caspershouse.com
" Mark Rae" <毫安** @ mark-N-O-S-P-A-M-rae.co.uk>在消息中写道
news:ev ************** @ TK2MSFTNGP14.phx.gbl ...
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...
>
我有一个包含多个属性的类,其中一个是DateTime
数据类型。但是,此属性并不总是有值,我需要稍后检查它是否有值。
当我创建类的新实例时,此属性获取
默认值{1/1/1},我根本不需要,因为这是一个完全有效的日期。
有什么办法吗?导致DateTime数据类型根本没有值,
我可以稍后检查?
或者是不使用DataTime数据类型并使用通用对象的答案
数据类型相反?
感激不尽的任何帮助。
Mark
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
" Carlos J. Quintero [ .NET MVP] < CA ***** @ NOSPAMsogecable.com>写在
消息新闻:OK ************** @ tk2msftngp13.phx.gbl ...
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:OK**************@tk2msftngp13.phx.gbl...
可以你使用DateTime.MinValue作为特殊值来表示没有值?
Could you use DateTime.MinValue as a special value to denote no value?
可能 - 谢谢你的回答。
Possibly - thanks for the answer.
这篇关于将null值传递给DateTime属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!