Correct way of working with dates <Solved>

Hi,

This is probably a newb question but I am struggling with understanding some results I’m getting with dates. I’ve tried searching the forums but can’t seem to frame the search correctly.

As an example, I have a window with a date property and a date picker control which has its own date property. Both are Xojo Classic dates.

In the date control I have an event which is triggered by a change to the date control (“Updated”). In that event, I have code that checks for a difference between the date control’s date property and the windows date property; if they are different, I set the windows date property to the control’s date property. For example:

Sub Updated() Handles Updated
If mLoadingData Then Exit
’ WindowDateTime is the date property on the Window
If WindowDateTime <> me.m_DateTime Then
WindowDateTime = me.m_DateTime '<-- breakpoint set here
End If
End Sub

I then run this in debug mode and set a breakpoint inside the If…Then statement so I should get a break every time the two values are different.

The first time I change the date control, the debugger breaks but for every time after that it doesn’t. When I checked by breaking at the If statement, the window date property becomes the same as the control date property BEFORE I set it for every iteration after the first one. So I changed the code as follows to :

Sub Updated() Handles Updated
If mLoadingData Then Exit
’ WindowDateTime is the date property on the Window
If WindowDateTime <> me.m_DateTime Then
Dim theDateToSetTo As New Date(me.m_DateTime)
WindowDateTime = theDateToSetTo '<-- breakpoint set here
End If
End Sub

Now it works as expected; ie I get a debug break every time I change the date control. So it seems as if when I set the two dates equal to each other, the second date comes byRef the first date but if I use an intermediary date with the constructor the two dates are not linked. Is that as expected or is there a better way for me to set one date property equal to another? I know that I have a working answer in the second code but this has implications for any other dates I am working with and I need to know whether I have to change all my code to suit or whether there is perhaps a simpler change I can make.

Thanks in advance.

By the way, this is Xojo 2019 R1.1 debugging on a Mac running Mojave 10.14.6

Ok its solved for me. I need to set the dates equal to each other using .TotalSeconds. The code below works as I expect without the use of an intermediary date, I get a debug break every time I change the date control.

Sub Updated() Handles Updated
If mLoadingData Then Exit
If WindowDateTime <> me.m_DateTime Then
’ WindowDateTime is the date property on the Window
WindowDateTime.TotalSeconds = me.m_DateTime.TotalSeconds '<-- breakpoint set here
End If
End Sub

Probably somewhere in the documentation. :frowning:

Using DateTime is a bit misleading letting thik you are useing API 2.0…

Try something like m_FullDate for example.

API 1.0 Date LR entry:
http://documentation.xojo.com/api/deprecated/date.html