有关datetime的默认值的疑问.高手看过来..

gabriel1 2004-11-25 05:36:37
如何将null值赋给一个datetime变量..
因为在c#程序中声明一个DateTime变量,它总有初始值为"0001-1-1"
请问,该怎么解决.
...全文
3281 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
OriesMap 2004-11-29
  • 打赏
  • 举报
回复
不好意思,我借楼主的地方问自己一下各位高人,
我做的日期控件中的Value值(DateTime类型),为什么默认值总是为空,而不能象DateTimePicker一样是当前日期与时间呢?
如果默认值为当前日期与时间,DefaultValue(typeof(DateTime),"") 请问""里该怎样写,还是要为别的写法呀?
yellowhwb 2004-11-26
  • 打赏
  • 举报
回复
不敢相信楼主居然不知道这一点。
继承于System.ValueType的全是值(Struct)类型的:
System.ArgIterator
System.Boolean
System.Byte
System.Char
System.Collections.DictionaryEntry
System.Collections.Specialized.BitVector32
System.Collections.Specialized.BitVector32.Section
System.Configuration.Assemblies.AssemblyHash
System.Data.OracleClient.OracleBinary
System.Data.OracleClient.OracleBoolean
System.Data.OracleClient.OracleDateTime
System.Data.OracleClient.OracleMonthSpan
System.Data.OracleClient.OracleNumber
System.Data.OracleClient.OracleString
System.Data.OracleClient.OracleTimeSpan
System.Data.SqlTypes.SqlBinary
System.Data.SqlTypes.SqlBoolean
System.Data.SqlTypes.SqlByte
System.Data.SqlTypes.SqlDateTime
System.Data.SqlTypes.SqlDecimal
System.Data.SqlTypes.SqlDouble
System.Data.SqlTypes.SqlGuid
System.Data.SqlTypes.SqlInt16
System.Data.SqlTypes.SqlInt32
System.Data.SqlTypes.SqlInt64
System.Data.SqlTypes.SqlMoney
System.Data.SqlTypes.SqlSingle
System.Data.SqlTypes.SqlString
System.DateTime
System.Decimal
System.Diagnostics.CounterSample
System.Diagnostics.SymbolStore.SymbolToken
System.Double
System.Drawing.CharacterRange
System.Drawing.Color
System.Drawing.Point
System.Drawing.PointF
System.Drawing.Rectangle
System.Drawing.RectangleF
System.Drawing.Size
System.Drawing.SizeF
System.EnterpriseServices.BOID
System.EnterpriseServices.XACTTRANSINFO
System.Enum
System.Guid
System.Int16
System.Int32
System.Int64
System.IntPtr
System.IO.WaitForChangedResult
System.Reflection.Emit.EventToken
System.Reflection.Emit.FieldToken
System.Reflection.Emit.Label
System.Reflection.Emit.MethodToken
System.Reflection.Emit.OpCode
System.Reflection.Emit.ParameterToken
System.Reflection.Emit.PropertyToken
System.Reflection.Emit.SignatureToken
System.Reflection.Emit.StringToken
System.Reflection.Emit.TypeToken
System.Reflection.InterfaceMapping
System.Reflection.ParameterModifier
System.Runtime.InteropServices.ArrayWithOffset
System.Runtime.InteropServices.BIND_OPTS
System.Runtime.InteropServices.BINDPTR
System.Runtime.InteropServices.CONNECTDATA
System.Runtime.InteropServices.DISPPARAMS
System.Runtime.InteropServices.ELEMDESC
System.Runtime.InteropServices.ELEMDESC.DESCUNION
System.Runtime.InteropServices.EXCEPINFO
System.Runtime.InteropServices.FILETIME
System.Runtime.InteropServices.FUNCDESC
System.Runtime.InteropServices.GCHandle
System.Runtime.InteropServices.HandleRef
System.Runtime.InteropServices.IDLDESC
System.Runtime.InteropServices.PARAMDESC
System.Runtime.InteropServices.STATSTG
System.Runtime.InteropServices.TYPEATTR
System.Runtime.InteropServices.TYPEDESC
System.Runtime.InteropServices.TYPELIBATTR
System.Runtime.InteropServices.VARDESC
System.Runtime.InteropServices.VARDESC.DESCUNION
System.Runtime.Serialization.SerializationEntry
System.Runtime.Serialization.StreamingContext
System.RuntimeArgumentHandle
System.RuntimeFieldHandle
System.RuntimeMethodHandle
System.RuntimeTypeHandle
System.SByte
System.Security.Cryptography.DSAParameters
System.Security.Cryptography.RSAParameters
System.Single
System.Threading.LockCookie
System.Threading.NativeOverlapped
System.TimeSpan
System.TypedReference
System.UInt16
System.UInt32
System.UInt64
System.UIntPtr
System.Void
System.Web.UI.WebControls.FontUnit
System.Web.UI.WebControls.Unit
System.Windows.Forms.BindingMemberInfo
System.Windows.Forms.DataGridCell
System.Windows.Forms.LinkArea
System.Windows.Forms.Message
langmafeng 2004-11-26
  • 打赏
  • 举报
回复
值类型是不能赋值为null
theNull 2004-11-26
  • 打赏
  • 举报
回复
mark
bulong0721 2004-11-26
  • 打赏
  • 举报
回复
Struct(结构是不能赋予null的)
lhcoolhacker 2004-11-26
  • 打赏
  • 举报
回复
的确如此,我在开发论坛的时候经常碰到,目前的解决方法如下:
public DateTime pubTime;

public DateTime PubTime
{
get
{
if(pubTime.Year == "0001")
return null;
else
return pubTime;
}
}
北京的雾霾天 2004-11-26
  • 打赏
  • 举报
回复
只声明不给值不就是了么:)
DateTime tmpdt;

如果要是给数据库存值的话,如果数据库日期字段可以为空,则传DbNull.Value就行了.
如果只是想是不是给tmpdt给过值,这个还真的不好说.
gabriel1 2004-11-26
  • 打赏
  • 举报
回复
那我要不想给它初始值,该怎么办?
tonybaobao 2004-11-25
  • 打赏
  • 举报
回复
不行的。值类型没有null的。只有引用类型才有。
Eddie005 2004-11-25
  • 打赏
  • 举报
回复
不能
zhy0101 2004-11-25
  • 打赏
  • 举报
回复
DateTime是结构Struct不是类Class

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧