Assigning Dates

Can someone please explain why I am observing this action.

Lets assume today is 30 May 2015

Dim myDate as New Date

my Date is 30 May 2015

Dim TestDate as Date = myDate

TestDate is 30 May 2015

TestDate.Day = 1

TestDate is 1 May 2015 BUT so is myDate. Why?
Doesn’t myDate copy to TestDate? It’s acting like a reference. How do I prevent myDate from changing on the assignment?
What am I missing?

Thanks

Think of Dates as a class. So you setting TestDate = Mydate makes them both the same object.

The documentation here:
http://documentation.xojo.com/index.php/Date.Constructor(CopyDate_as_Date)

Says you should do this:

dim testDate as New Date(MyDate)

Then they will be independent.

[quote=190837:@Michael Dosa]Can someone please explain why I am observing this action.

Lets assume today is 30 May 2015

Dim myDate as New Date

my Date is 30 May 2015

Dim TestDate as Date = myDate

TestDate is 30 May 2015

TestDate.Day = 1

TestDate is 1 May 2015 BUT so is myDate. Why?
Doesn’t myDate copy to TestDate? It’s acting like a reference. How do I prevent myDate from changing on the assignment?
What am I missing?

Thanks[/quote]
Try:

Dim TestDate As new Date TestDate.TotalSeconds = MyDate.TotalSeconds
Now you will have two separate dates.

Simon.

The copy constructor is better, imho.

Thanks Bob

dim testDate as New Date(MyDate) solves the issue.

md

Interesting. Where is that documented?

To make a proper copy, you’d have to copy the GMTOffset and then TotalSeconds. The constructor does that for you already (I think).

I was agreeing with you. What you posted is called a copy constructor.

Doh! Sorry. Kind of tired today.

All that partying in the mud.

Bob just wants to argue.

It’s red.

Right, red.

What?!? Prove it!

:slight_smile:

Not sure about the partying, but the standing all day, and walking through the mud, was exhausting.