Wednesday, September 20, 2017

#1 2017-03-18 10:33:32 am

haolesurferdude
Member
Registered: 2010-02-19
Posts: 104

All Date Variables Modified When One Date Variable is Focus of Modific

I would greatly appreciate an explanation of why the following results in changes to all the date variables at the top of the following code.  I've tried many ways of finding out what is occurring, but I am stumped.

Thanks in advance!!!

Applescript:


set myDate to (current date)
set testDate to myDate
set testDate to my addMonths(testDate, 2)

set showDate to date string of myDate

on addMonths(modDate, theMonths)
   set totalMonths to ((month of modDate) + theMonths)
   if totalMonths > 12 then
       set excessMonths to totalMonths - (totalMonths div 12)
       set modDate's year to (totalMonths div 12)
       set modDate's month to excessMonths
   else
       set modDate's month to totalMonths
   end if
   return modDate
end addMonths

Browser: Firefox 50.0
Operating System: Mac OS X (10.10)

Offline

 

#2 2017-03-18 10:51:04 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4382

Re: All Date Variables Modified When One Date Variable is Focus of Modific

Hi.

When you pass a value to a handler, or set one variable to another, the variable which receives the value gets the very same object, not a copy. In other words, the date object in modDate is the same object as the one in testDate, which is the same one as in myDate. If you change the value of any of the object's properties, you'll see the effect whichever variable you use to look at it.

If you don't want this to happen, you should make a copy.

Applescript:

copy myDate to testDate -- Not set testDate to myDate.

Or you could do it in the handler:

Applescript:

on addMonths(modDate, theMonths)
   copy modDate to modDate -- Set the variable to a different copy of the date object.

Last edited by Nigel Garvey (2017-03-18 02:13:01 pm)


NG

Offline

 

#3 2017-03-18 02:02:54 pm

haolesurferdude
Member
Registered: 2010-02-19
Posts: 104

Re: All Date Variables Modified When One Date Variable is Focus of Modific

Thank you Nigel!  I appreciate the insight!  Works like a charm...

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)