1

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 assignment.

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?

Christopher Leach
  • 568
  • 1
  • 5
  • 16
  • If i try Ctype(object.DateStamp, DateTime?) = Now it still comes up as nothing on the WCF service. if I do it on my WPF application the property accepts the now value and doesn't display a null value like the WCF service – Christopher Leach Jul 27 '11 at 07:35
  • 1
    That expression is a boolean expression, not an assignment. Why not simply use object.DateStamp = entity.DateStamp.Value – Chris Dunaway Jul 27 '11 at 13:38

2 Answers2

1

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
atconway
  • 20,624
  • 30
  • 159
  • 229
0

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
amonroejj
  • 573
  • 4
  • 16