开发者

DateTime? Cannot assign a value

I need to have a property that is a nullable date as the datestamp is used for when a process is completed.

If there is no date this is a way to determain if the process has occured.

I have created a Nuallable DateTime property (DateTime?) however when i try to assign a value from my database entity(when debugged has a date value) i am not thrown an exception however my property still reads a null value after assignmen开发者_如何学Pythont.

How can i get a DateTime? type to accept a DateTime value? i thought this would do the trick _object.DateStamp (type = DateTime?) = _entity.DateStamp (Type = DateTime?, Value = DateTime) or for more understandable syntax

Ctype(object.DateStamp, DateTime?) = Ctype(entity.DateStamp, DateTime?)

Strange thing is i can assign the properties value like this. Ctype(object.DateStamp, DateTime?) = Now

Oh btw im uisng LinQ Entities.

Any Help?


I had this same problem. I would assign a date from a user entered value and it would never assign to the Nullable Date property on my custom object. My solution was to assign the user entered value into a local Nullable date variable, and then assign that value to the property. Trying to cast the user entered value into a nullable type on a single line didn't work for me either. The solution that worked for me is below:

Dim MyDate As Date? = Date.Parse(Me.txtDate.Text.Trim())
MyObject.Date1 = AppearanceDateAlternate


Another way this can burn you is if you try to use the "<>" operator on a mixture of null and not-null.

    Public Property OwnerSignoffDate As Date?
        Get
            Return _ownerSignoffDate
        End Get
        Set(value As Date?)
            If value <> _ownerSignoffDate Then       'BAD!
                _ownerSignoffDate = value
                Dirty = True
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("OwnerSignoffDate"))
            End If
        End Set
    End Property

change to:

    Public Property OwnerSignoffDate As Date?
        Get
            Return _ownerSignoffDate
        End Get
        Set(value As Date?)
            If value <> _ownerSignoffDate OrElse value.HasValue <> _ownerSignoffDate.HasValue Then     'GOOD!
                _ownerSignoffDate = value
                Dirty = True
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("OwnerSignoffDate"))
            End If
        End Set
    End Property
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜